About the key

In the common sense, we can cache the items by key. However, what can the key could be?

The key could be a simple value, such as a long value or a string. The key could be a complex type, as the following.

class XKey


    public string Name{get;set;}

    public string Type{get;set;}



Someone may have different idea that how could a key be the complex type. Something must be wrong in the design. The the discussion of the key would be extended to more wider than my purpose in this article.


The reason why I call it object cache is that the key of the cache can be a complex object.


We could have the following situation when we programming with web service.

Let’s say, we have the following definition of the request in the web service.

class TestRequest


public long RevID{get;set;}

public long[] ItemIDs{get;set;}

public DateTime BusinessDate{get;set;}



The designer of the TestRequest just expect to publish the values with the TestRequest with RevID, ItemIDs and BusinessDate. However, it doesn’t mean the client end must give the three values to TestRequest. In the client end, only RevID or BusinessDate may be given to TestRequest.


So comparing  to the simple key cahce, the object cache can take the TestRequest as a key to cache the return items with whatever composite values in the request.



About the reset method

The ResetCaches on the CacheManager is designed to remove all the items in the cache.

It is not easy for us to determine what cached items will be removed when some business events happen if there are dozens of caches in the system. So a category value is introduced into the design in 2.x version as the assistance for the issue.

public enum CacheFilter
    Default = 1,
    BasicConfig = 2,
    Schedule = 4,
    Employee = 8,
    Unit = 16,

We can define the enumeration value as above for the category and assign it to the cache item token. When the cache is requested, you can set the reset filter for the instance of the cache. The only cached item with the filters will be removed when ResetCaches is invoked.

var cache = CacheManager.Instance.GetCache<int, string>((long)CacheFilter.BasicConfig);

CacheManager.Instance.ResetCaches((long)(CacheFilter.BasicConfig | CacheFilter.Schedule));

The scope of the cache

The scope of the cache is determined by the scope of either the type of the TKey, or the type of TItem. Let’s take the following code as example.

namespace XLibrary.Modules
    public class Unit

namespace ZLibrary.Business
    using XLibrary.Modules;
    public class XDataProvider
        private class LoadDataKey : IEquatable<LoadDataKey>
            public long DataID{get;set;}
            public long ReginID{get;set;}
            public bool Equals(LoadDataKey other)
               return DataID == other.DataID && ReginID == other.ReginID;
        public void LoadData(long dataID, long reginID)
             var cache = CacheManager.Instance.GetCache<LoadDataKey, Unit>();
             //use the cache.


From the example above, the ICache<LoadDataKey, Unit> is only available in ZLibrary.Business.XDataProvider as LoadDataKey is only available in the scope of XDataProvider. So the scope of the cache is determined by the type of the cache.

On the other hand,  if we say ICache<LoadDataKey, Unit> has scopes, that means the same LoadDataKey has different Unit that is conflict to the semantic of the key and value.

Last edited Sep 27, 2011 at 1:56 PM by czy, version 7


No comments yet.