Object Cache
What’s object cache
Object Cache is a cache system which key is built on complex object. The complex object benefits us to define the key with multiple values. However, the features of object cache is not limitted by this. Here is the features list for the object cache.
Features
- Cache the element with complex object
- The concurrency issue has been considered
- The life time of the cached item can be extended
- The caches can be cateoried according to the business for managment purpose
- The cache system can run on .net framework 2.0 when 3.5 and popular nowadays.
What’re problems are addressed by the cache
1. Cache the element with the key having mutiple values
The feature is critical in working with returned records from the web service. In most cases, the returned records from the web service are determined by the request object having multiple values.
For example, we would like to request the schedule of employees’ by time range and departmentId. So the returned schedules would be determined by starttime, endtime and departmentId. It would be quite smooth to migrate the three values to the cache key to cache the schedules from the web service if we want.
//define the key public class GetEmployeeKey : GetEmployeeRequest , IEquatable<GetEmployeeKey> { public GetEmployeeKey(GetEmployeeRequest request) { this.EmployeeId = request.EmployeeId; this.DepartmentId = request.DepartmentId; this.CategoryId = request.CategoryId; } public override Equals(GetEmployeeKey other) { if(other == null) return false; return this.EmployeeId == other.EmployeeId && this.DepartmentId == request.DepartmentId && this.CategoryId == request.CategoryId; } } public class SomeBusiness { public void DoSomething(GetEmployeeKey request) { var cache = CacheManager.Instance.GetCache<GetEmployeeKey,IEnumerable<EmployeeType>>(); var employees = cache.GetWithCreate(new GetEmployeeKey(request), ()=>PortFactory.Create().GetEmployee(request), new InstanceToken(this)); //some code against employees } }
2. Lifetime of the cache item is flexible
There are 3 kinds of lifetime in object cache.
- InstanceToken
- AbsoluteTimeToken
- SlideTimeToken
However, the lifetime of the cache item can be extended by inheriting BaseToken.
If the lifetime of the cache item expires, the cache item will be removed and no longer available in the cache.
InstanceToken | The lifetime is determined by the referenced object. The lifetime expires if the referenced object is collected by the garbage collection mechanism (GC). However, if the referenced object is System.Windows.Forms.Control or its inheritance, the lifetime is expired if the value of Created property is changed to false. |
AbsoluteTimeToken | The lifetime expires if the pre-defined time elapses. Let’s say, the cache item is created at 8:05:00 and the lifetime is given to 5 seconds. Then the lifetime of the cache item expires at 8:05:05. |
SlideTimeToken | The lifetime expires if the pre-defined time elapses. However, if the accesses to the cache item happen in the duration of the lifetime, the expiring time of the cache item will be updated. Let’s say, the cache item is created at 8:05:00 and the lifetime is given to 5 seconds. If an access to the cache item happen at 8:05:04, the expiring time of the cache item will be updated to 8:05:09. |
3. The scope
The scope of the cahce is determined by the cache key. Accurately, it is the instinctive feature from the strong-typed cache key.
For example, we may expect to use the cache only within a class. The only thing to do is to define a private nested class as the cache key in the class.
public class BusinessClass { private class EmployeeCacheKey : IEquatable<EmployeeCacheKey> { public string EmployeeId{get;set;} public override Equals(EmployeeCacheKey other) { if(other == null) return false; return this.EmployeeId == other.EmployeeId; } } public void SomeMethod() { var cache = CacheManager.Instance.GetCache<EmployeeCacheKey, EmployeeType>(); //use the cache in the scope of the class } }
Then the cache is hidden behind the class. No worries about the cache within the class will be used incorrectly outside of the class.
This is an advantage to those cache mechanism that accesses the cache by name, a string value, anywhere. If you expect your cache can be accessed globally in the application, simply change the modifier of the cache key class from private to public.
Please go to ObjectCache(objectcahe.codeplex.com) at code plex for more information about it.
Additionally, please go to our website novasoftware for more information about the team.