Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this case, your cache shouldn't cache the <em>actual</em> object, but rather, a wrapper around the object being cached that also contains information about the state of the object in the cache.</p> <p>For example, you could have a simple class like so, representing the items in your cache:</p> <pre><code>public class CacheItem&lt;T&gt; { // Since the cache is the only thing // that should be making CacheItems, // make this internal to the assembly // that the cache is implemented in. // This constructor is called before // an add. internal CacheItem(T item) { // Set the property values. Item = item; } // Poor-man's immutability. public T Item { get; private set; } // The backing field for IsCached, it // is volatile so that it can serialize // access for single reads/writes, which is // what the property does. // Assume it is being added when constructed. private volatile bool _isCached = true; // Only able to be set by the cache. // The setter is set to false when the item // is stale (not in the cache any longer). public bool IsCached { get { return _isCached; } set { _isCached = value; } } } </code></pre> <p>The idea here is simple:</p> <ul> <li><p>When your cache is about to enter a new instance of an item into the cache, then it calls the constructor (the constructor should <em>only</em> be available to the cache, if necessary, you can make <code>CacheItem</code> a nested class with a private constructor instead of internal) which sets the <code>IsCached</code> property value to true. The item is then placed in the cache.</p></li> <li><p>When the item expires from the cache, then the cache sets the <code>IsCached</code> property to false (again, this property should only be accessible by the cache).</p></li> <li><p>The responsibility is moved to the <em>client</em> to check the <code>CacheItem&lt;T&gt;</code> to see if it is stale.</p></li> </ul> <p>Note that this is a pull operation. If you want, you could add <em>push</em> operation support by adding an event to the <code>CacheItem&lt;T&gt;</code> like so:</p> <pre><code>public class CacheItem&lt;T&gt; { // Since the cache is the only thing // that should be making CacheItems, // make this internal to the assembly // that the cache is implemented in. // This constructor is called before // an add. internal CacheItem(T item) { // Set the property values. Item = item; } // Poor-man's immutability. public T Item { get; private set; } // The lock for the event registrations. // Since everything else is immutable, this needs // to be as well. private readonly object _eventLock = new object(); // The backing field for the Expired // event. Since everything else is immutable // this needs to be as well. private readonly EventHandler _expiredHandlers; // The expires event. public event EventHandler Expired { add { lock (_eventLock) _expiredHandlers += value; } remove { lock (_eventLock) _expiredHandlers -= value; } } // The backing field for IsCached, it // is volatile so that it can serialize // access for single reads/writes, which is // what the property does. // Assume it is being added when constructed. private volatile bool _isCached = true; // The setter is set to false by the // Expire method (called by the cached) // when the item is stale // (not in the cache any longer). public bool IsCached { get { return _isCached; } } // Called internally by the cache. internal void Expire() { // Set _isCached to false. _isCached = false; // Get the event handlers and fire // the event. Getting the handlers // needs to be synchronized. EventHandler handlers; // Synchronize. lock (_eventLock) handlers = _expiredHandlers; // Fire if there are handlers. if (handlers != null) handlers(this, EventArgs.Empty); } } </code></pre> <p>Now, you can have your clients subscribe to the <code>Expired</code> event, which would indicate to clients when the cache item is invalidated. This is fired by the cache by calling the internal <code>Expire</code> event on the item when it is removed from the cache (and it will set the <code>IsCached</code> property to false as well).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload