-
Couldn't load subscription status.
- Fork 1
3. Configuration
The caching policy is defined per entity. A caching policy consists of entity type, the caching key, expiration timeout, and cache storage implementation. 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
new CacheBuilder();or invoke AddFluentCaching for ASP.NET application
AddFluentCaching(cacheBuilder => {/* your configuration here */});cachebuilder.For<User>(u => {/* your configuration here */});If one of the entity properties should be used as a key, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(s => $"User:{s.Id}"));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));If it is needed to define multiple configurations for the same entity, use a named policy.
cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And("User").PolicyName());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());Currently, in-memory and distributed cache storages are supported. Also, it is possible to use custom storage implementation. To configure in-memory storage, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().SetInfiniteExpirationTimeout()
.And().StoreInMemory());To configure distributed storage, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().SetInfiniteExpirationTimeout()
.And().StoreInDistributedCache());To configure custom caching storage, call
cacheBuilder => cachebuilder.For<User>(
u => u.UseAsKey(u => u.FirstName)
.And().SetInfiniteExpirationTimeout()
.And().StoreIn(new CustomStorageImplementation()));In the examples above, the storage implementation is configured for a specific entity, but it can also be configured globally. Global storage configuration will be used only when storage configuration is not configured for a particular entity. To configure global in-memory storage, call
cacheBuilder => cachebuilder.SetInMemoryAsDefaultCache();To configure global distributed storage, call
cacheBuilder => cachebuilder.SetDistributedAsDefaultCache();To configure global custom storage, call
cacheBuilder => cachebuilder.SetGenericCache(new CustomStorageImplementation());