Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Object Cache

, 18 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

Features

  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 = 
	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:

  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.

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 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 (objectcahe.codeplex.com) at CodePlex for more information about it.

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

License

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

Share

About the Author

cao4zy
Program Manager Nova Software
China China
I am a programmer dedicating to make the complex stuff simple.
 
My Chinese blog can be accessed at http://www.cnblogs.com/czy/

Comments and Discussions

 
GeneralMy vote of 5 PinmemberNitinSingh17-Jun-14 19:05 
QuestionEquals method Pinmembernancydotcom7-Jan-13 7:39 
You are referencing "request" in your Equals method where it should be "other".

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 18 Dec 2011
Article Copyright 2011 by cao4zy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid