Able
Askowl Base Library Enabler
Cache.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 // ReSharper disable ClassNeverInstantiated.Global
4 
5 using System;
6 
7 namespace Askowl {
8  /// <a href="http://bit.ly/2Rj0PEa">Instance caching</a>
9  public class Cache {
10  /// <a href="http://bit.ly/2Rj0Wj4">Boxed value items</a>
11  public interface Boxed { }
12 
13  /// <a href="http://bit.ly/2Rj0Wj4">Caching boxed value items</a> <inheritdoc cref="LinkedList{T}" />
14  public class Boxed<T> : LinkedList<T>.Node, Boxed {
15  /// <a href="http://bit.ly/2Rj0Wj4">Unboxed value storage</a>
16  // ReSharper disable once UnassignedField.Global
17  public T Value;
18 
19  /// <a href="http://bit.ly/2Rj0Wj4">Add a cache entry containing a new value item</a>
20  public static Boxed New(T item) => (Boxed) Cache<T>.Entries.Add(item);
21 
22  /// <a href="http://bit.ly/2Rj0Wj4">Create a new cache holding a copy of the value item.</a>
23  public static Boxed Clone(Boxed<T> item) => (Boxed<T>) Cache<T>.Entries.Add(item.Value);
24 
25  /// <inheritdoc />
26  public override string ToString() => Value.ToString();
27  }
28  }
29 
30  /// <a href="http://bit.ly/2Rj0PEa">The parent and worker for caching</a>
31  // ReSharper disable once ClassNeverInstantiated.Global
32  public class Cache<T> {
33  /// <a href="http://bit.ly/2OrE4it">A Cache is managed as a linked list of entries</a>
34  public static readonly LinkedList<T> Entries = new LinkedList<T>($"{typeof(T)} Cache");
35 
36  /// <a href="http://bit.ly/2NXgIlt">Get a node reference (from recycling if available)</a>
37  public static T Instance => Entries.GetRecycledOrNew().Item;
38 
39  /// <a href="">Get a node reference (from recycling if available)</a>
40  public static LinkedList<T>.Node NodeInstance => Entries.GetRecycledOrNew();
41 
42  /// <a href="http://bit.ly/2OrE4it">Get a new or recycled node and add an item reference</a>
43  public static T Add(T item) => Entries.Add(item).Item;
44 
45  /// <a href=""></a> //#TBD#//
46  public static T Value(object nodeReference) {
47  var node = (nodeReference as LinkedList<T>.Node);
48  return (node == null) ? default : node.Item;
49  }
50 
51  /// <a href="http://bit.ly/2OrE4it">Dispose cleanly of a node and the contained item</a>
52  public static void Dispose(T item) => Entries.Dispose(item);
53 
54  /// <a href="http://bit.ly/2Oq9upA">Dispose all active nodes - filling recycle bon</a>
55  public static void RecycleEverything() => Entries.Dispose();
56 
57  /// <a href="http://bit.ly/2NV8Ssx">A lambda to be called to create an item</a>
58  public static Func<T> CreateItem { set => Entries.CreateItem = value; }
59 
60  /// <a href="http://bit.ly/2Rh3gqG">A lambda to be called before sending an item to recycling for later use</a>
61  public static Action<T> DeactivateItem { set => Entries.DeactivateItem = (node) => value(node.Item); }
62 
63  /// <a href="http://bit.ly/2Riz7Hj">A lambda to be called before reusing an item from the recycle bin</a>
64  public static Action<T> ReactivateItem { set => Entries.ReactivateItem = (node) => value(node.Item); }
65 
66  /// <a href="http://bit.ly/2OrE4it">A disposable for `using` so items will be cleaned up</a>
67  public static IDisposable Disposable(T item) => new Disposable {Action = Entries.ReverseLookup(item).Dispose};
68  }
69 
70  /// <a href=""></a> //#TBD#//
71  public class Cached<T> : IDisposable where T : Cached<T> {
72  /// <a href=""></a> //#TBD#//
73  public static T Instance => Cache<T>.Instance;
74  protected Cached() { } // so we can't use new()
75 
76  /// <a href=""></a> //#TBD#//
77  public void Dispose() => Cache<T>.Dispose(this as T);
78  }
79 }
Definition: Clock.cs:3
Instance caching
Definition: Cache.cs:9
Node GetRecycledOrNew()
Get a node and item either recycled or created new.
static T Instance
Get a node reference (from recycling if available)
Definition: Cache.cs:37
static Action< T > ReactivateItem
A lambda to be called before reusing an item from the recycle bin
Definition: Cache.cs:64
static Func< T > CreateItem
A lambda to be called to create an item
Definition: Cache.cs:58
static Boxed New(T item)
Add a cache entry containing a new value item
void Dispose()
//#TBD#//
static LinkedList< T >.Node NodeInstance
Get a node reference (from recycling if available)
Definition: Cache.cs:40
override string ToString()
LinkedList - a different perspective
Definition: LinkedList.cs:13
Node GetRecycledOrNew()
Retrieve a node - either from recycling or creating it anew
Definition: LinkedList.cs:204
static T Instance
//#TBD#//
Definition: Cache.cs:73
static Boxed Clone(Boxed< T > item)
Create a new cache holding a copy of the value item.
LinkedList node
Definition: LinkedList.cs:15
static T Value(object nodeReference)
//#TBD#//
Definition: Cache.cs:46
static T Add(T item)
Get a new or recycled node and add an item reference
Node Add(T t)
Fetch an unused node and use it as the container for the provided new item
T Value
Unboxed value storage
Definition: Cache.cs:17
//#TBD#//
Definition: Cache.cs:71
Boxed value items
Definition: Cache.cs:11
static Action< T > DeactivateItem
A lambda to be called before sending an item to recycling for later use
Definition: Cache.cs:61
T Item
Item can be value type (int, float ... struct) or object (class instance)
Definition: LinkedList.cs:23
Simplified creation of an action that happens at the end of a using statement no matter what else hap...
Definition: Disposable.cs:7
static readonly LinkedList< T > Entries
A Cache is managed as a linked list of entries
Definition: Cache.cs:34