Question about the caching behaviour

Topics: Caching Application Block
Feb 14, 2007 at 2:03 PM
I wrote a simple testing program using singlecall remoting, the client can connect to the server save or request cached item. First I try to defined the cache manager as static variable in the sever because I want to ensure each remote client will share the same cache. This works as expected.

public static CacheManager m_cacheManager = CacheFactory.GetCacheManager();

public bool Add(string _key, Object _value)
{
mcacheManager.Add(key,_value, CacheItemPriority.Normal, null);
return true;
}

public Object Get(string _key){
return mcacheManager.GetData(key);
}

However, I delete the static modifier, so the CacheManager is no longer static. So the remote object looks like below:

public class CacheSystem : MarshalByRefObject, ICacheSystem
{
public CacheManager m_cacheManager = CacheFactory.GetCacheManager();

public CacheSystem()
{
}

public bool Add(string _key, Object _value)
{
mcacheManager.Add(key,_value, CacheItemPriority.Normal, null);
return true;
}

public Object Get(string _key){
return mcacheManager.GetData(key);
}

}

Singlecall remote object will create a new instance of CacheSystem for each remote client. I expect a different cache will be used for each instance. But the result shows that they still share the same cache. For example, client 1 save a cached item, client 2 save a cached item, each client can get the cache item that saved by the other client. So seems they are sharing the same cache.

My question is, what circumstance the cache will be shared? when a cache won't be shared? For example, is the scope is application based, app domain based, machine based or others? Hope someone can give me some ideas.

Thanks in advanced
Feb 14, 2007 at 2:45 PM
You are using the same cache manager in both instances, and each cache manager has its own in-memory cache and persistent backing store. From a cache perspective, it does not matter if you declare the variable as a static or instantiate a new variable. If they represent the same cache manager, they share the same cache.

If you want to have multiple caches, you would need to implement multiple cache managers. Each cache manager will then have its own in-memory cache and separate persistent storage.

A cache manager cannot be shared among different applications, only within a single application. The reason is because as mentioned above, the cache manager keeps an in-memory representation of the cache.

The documentation is really good if you want to get more information about how to use the block and various key scenarios.

Regards,

Dave

______________________

David Hayden
Microsoft MVP C#
Feb 14, 2007 at 5:26 PM
Edited Feb 14, 2007 at 5:26 PM
Thank you very much for your reply. I actually only want one cache manager. So I raise the question here to help me understand how the caching block behave, incase I misunderstand and happily thinking my instances are sharing the same cache while they are not :)

Just to confirm that I understand your answer correctly. Do you mean when I specify the cache manager, say the default one in the config as blow:
<cacheManagers>
<add name="Default Cache Manager"
expirationPollFrequencyInSeconds = "60"
maximumElementsInCacheBeforeScavenging ="10000"
numberToRemoveWhenScavenging="10"
backingStoreName="inMemory" />
</cacheManagers>
Then in the same application, only one default cache manager is exists?

Thank you for pointing out the documentation, can't believe I forget about it totally :) I will have a read now.

Cheers