The first Step to use the cache

 

Although it is acceptable to use a simple type, such as string, long as a key of the cache, it is recommended to define class for the key to identify the cahe.

class SomeKey :IEquatable<SomeKey>
{
    public long ID{get;set;}
    public bool Equals(SomeKey other)
    {
        return ID == other.ID;
    }
}The benifit of this kind of key is that it is strong typed, having a definite scope and accurate meaning in key-value  semantic.

When you have a key and a known type of object, you are ready to use the cache. The code simply goes as following.

//get the object
var unit = CacheManager.Instance.GetCache<SomeKey, Unit>().Get(new SomeKey{ID=3});
//get the object and create the object if it is not existing
var unit = CacheManager.Instance.GetCache<SomeKey, Unit>().GetWithCreate(new SomeKey{ID=3},
    ()=>{
             //some code the get the unit object with object of somekey
             return unit;
            }
    .new AbsoluteTimeToken(5));

For more information about the functions defined in the cache 2.x, please see 2.0.1 API Introduction

Last edited Sep 23, 2011 at 2:10 PM by czy, version 7

Comments

No comments yet.