Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: some typos

Caching is about improve improving applications performance. The most expensive performance costs of the applications are related with the data retrieving, typically when this data requires to be moved a cross across the network or loaded from disk. But some data have an a slow changing behavior (a.k.a non-volatile) and doesn't requires require to be re-read with the same frequency of the volatile data.

So, to improve your application's performance and handling this "nonvolatile" data from a pretty clean approach, Catel comes with a CacheStorage<TKey, TValue> class. Notice that the first generic parameter is the type of the key and the second the type of the value the will be storestored, just like a Dictionary<TKey, TValue> but CacheStorage isn't it just a Dictionary. This class allows you to retrieve data and storing store it into the cache with single statement and also helps you to handle expiration policy if you need it.

...

To initialize a cache storage field into your class use the follow following code:

Code Block
private readonly CacheStorage<string, Person> _personCache = new CacheStorage<string, Person>(storeNullValues: true);

...

Retrieving data and storing into cache with single statement

To retrieve data and storing store into a cache with a single statement use the follow following code:

Code Block
var person = _personCache.GetFromCacheOrFetch(Id, () => service.FindPersonById(Id));

When this statement is executed more than once times with the with the same keykey more than once , the value will be retrieved from the cache storage instead from of the service call. The service call will be executed just the first time or if the item is removed from the cache manually or automatically due by to the expiration policy.

Using cache expiration policies

The cache expiration policies adds add a removal behavior to the cache storage items. A policy signals that an item is expired to makes make that cache storage removes remove the item automatically.

A default cache expiration policy initialization code can be specified during cache storage initialization constructor:

Code Block
CacheStorage<string, Person> _personCache = new CacheStorage<string, Person>(() => ExpirationPolicy.Duration(TimeSpan.FromMinutes(5)), true);

You can specify an a specific expiration policy for an item when it's storing:

...

The default cache policy specified at cache storage initialization will be used if during the item storing the expiration policy is not specified.

...

Expiration policyTypeDescriptionInitialization code sample
AbsoluteExpirationPolicyTime-baseThe cache item will expire on the absolute expiration DateTime
Code Block
ExpirationPolicy.Absolute(new DateTime(21, 12, 2012))
DurationExpirationPolicyTime-baseThe cache item will expire using the duration TimeSpan to calculate the absolute expiration from DateTime.Now
Code Block
ExpirationPolicy.Duration(TimeSpan.FromMinutes(5))
SlidingExpirationPolicyTime-baseThe cache item will expire using the duration TimeSpan to calculate the absolute expiration from DateTime.Now, but everytime the item is requested, it is expanded again with the specified TimeSpan
Code Block
ExpirationPolicy.Sliding(TimeSpan.FromMinutes(5))
CustomExpirationPolicyCustomThe cache item will expire using the expire function and execute the reset action if is specified. The example shows how to create an a sliding expiration policy with a custom expiration policy.
Code Block
var startDateTime = DateTime.Now;
var duration = TimeSpan.FromMinutes(5);

ExpirationPolicy.Custom(() => DateTime.Now > startDateTime.Add(duration), () => startDateTime = DateTime.Now);
CompositeExpirationPolicyCustomCombines several expiration policy into a single one. It can be configured to expires expire when any policy expires or when all policies expiresexpire.
Code Block
new CompositeExpirationPolicy().Add(ExpirationPolicy.Sliding(
TimeSpan.FromMinutes(5))).Add(ExpirationPolicy.Custom(()=>...))

...

If the CustomExpirationPolicy is not enough, you can implement you own expiration policy to makes make that cache item expires expire triggered from a custom event. You are also able to add some code to reset the expiration policy if the item is read from the cache before it expires (just like SlidingExpirationPolicy does).

To implement an expiration cache policy use the follow following code template:

Code Block
public class MyExpirationPolicy : ExpirationPolicy
{
   public MyExpirationPolicy():base(true)
   {
   }

   public override bool IsExpired
   {
      get
      {
         // Add your custom expiration code to detect if the item expires
      }
   }

   public override void OnReset()
   {
      // Add your custom code to reset the policy if the item is read.
   }
}

...