- Getting Started
- AgileFx Architecture
- Using Modeling Tools
- The Domain Model
- Accessing Data
- Saving Data
- Advanced Queries
- Caching
- Serialization
Caching
Adding To Cache and Retrieving objects from Cache
This example shows how to add objects to Cache. Here we are loading a list of users from database and adding them to cache.
public List<User> GetUsers(EntityContext entityContext, long tenantId)
{
var cacheParams = new CacheParams
{
ItemKey = "users_" + tenantId,
Timeout = new TimeSpan(0, 2, 0)
};
var users = entityContext.Cache().Invoke(
(tId, _context) => _context.CreateQuery<User>().Where(u => u.Id == tId).ToList(),
this, tenantId, cacheParams);
return users;
}
Lets take a look at the code above. Here the the results of the func is added to cache. here tenantId is a parameter passed into the func and this is an object identifier defining the scope of the cache. If you want to have the cache globally you can pass null. The CacheParameters are optional and can be used to specify custom item key or timeout.
Invalidating a Cache
A cache can be invalidated in 2 ways.- Timeout Expiry
- By Calling Invalidation Methods
The default timeout is 2 minutes and if you want to specify a different value, you can use the CacheParams while creating a cache.
These examples shows how to invalidate a cache item.
context.Cache().InvalidateItemWithKey("users_" + tenantId);
context.Cache().InvalidateItemWithKeyFragment(new[] { "_" + tenantId, "users" });
In the first method we are invalidating a cache item by directly specifying the Key we have given while creating the cache. While in the second method, we are clearing the cache items which match all the key fragments passed.
