Skip to content

3. Configuration

Vladyslav Lobyntsev edited this page Mar 5, 2024 · 10 revisions

The caching policy is defined per entity. When using cache for a particular entity, its policy is retrieved using entity type as a key. If it is needed to have multiple caching policies, named caching policies should be used. To define a new caching policy

  1. Create CacheBuilder
new CacheBuilder();

or invoke AddFluentCaching

AddFluentCaching(cacheBuilder => {/* your configuration here */});
  1. Call For with entity type as a type parameter to define a policy for a particular entity.
cachebuilder.For<User>(u => {/* your configuration here */});
  1. Call one of UseAsKey methods

If one of the entity properties should be used as a key, call

cacheBuilder => cachebuilder.For<User>(
    u => u.UseClassFullNameAsKey());

If a static string should be used as a key, call

cacheBuilder => cachebuilder.For<User>(
    u => u.UseAsKey("CurrentUser"));

If the entity class name should be used as a key, call

cacheBuilder => cachebuilder.For<User>(
    u => u.UseClassNameAsKey());

If the entity class full name should be used as a key, call

cacheBuilder => cachebuilder.For<User>(
    u => u.UseClassFullNameAsKey());

In case the key is composite, one of CombinedWith methods may be used with the same options.

cacheBuilder => cachebuilder.For<User>(
    u => u.UseAsKey(u => u.FirstName)
         .CombinedWith(u => u.LastName));
  1. Set expiration timeout and expiration type
cacheBuilder => cachebuilder.For<User>(
    u => u.UseAsKey(u => u.FirstName)
         .And().SetExpirationTimeoutTo(5).Minutes.And(6).Seconds
         .With().AbsoluteExpiration());

In case there is no expiration timeout, SetInfiniteExpirationTimeout should be used, in the case configuring expiration type is not required.

cacheBuilder => cachebuilder.For<User>(
    u => u.UseAsKey(u => u.FirstName)
         .And().SetInfiniteExpirationTimeout());

Clone this wiki locally