Click here to Skip to main content
14,739,978 members
Articles » General Reading » Uncategorised Technical Blogs » General
Technical Blog
Posted 18 Dec 2011

Tagged as


5 bookmarked

Object Cache

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
18 Dec 2011CPOL
Object Cache

What’s Object Cache

Object Cache is a cache system whose key is built on a complex object. The complex object helps us to define the key with multiple values. However, the feature of object cache is not limited by this. Here is the features list for the object cache.


  1. Cache the element with complex object
  2. The concurrency issue has been considered
  3. The lifetime of the cached item can be extended
  4. The caches can be categorised according to the business for managment purpose
  5. The cache system can run on .NET Framework 2.0 when 3.5 and popular nowadays

What 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 = 
        var employees = cache.GetWithCreate(new GetEmployeeKey(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:

  1. InstanceToken
  2. AbsoluteTimeToken
  3. 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.

InstanceTokenThe 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.

AbsoluteTimeTokenThe 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.

SlideTimeTokenThe 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 mechanisms that access 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 ( at CodePlex for more information about it.

Additionally, please go to our website novasoftware for more information about the team.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Program Manager Nova Software
China China
I am a programmer dedicating to make the complex stuff simple.

My Chinese blog can be accessed at

Comments and Discussions

GeneralMy vote of 5 Pin
Nitin Singh India17-Jun-14 20:05
MemberNitin Singh India17-Jun-14 20:05 
QuestionEquals method Pin
nancydotcom7-Jan-13 8:39
Membernancydotcom7-Jan-13 8:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.