开发者

How to Get Items From Cache by Tag Without Knowing The Region

We are using MS Velocity, and moving to AppFabric soon. We are inte开发者_运维技巧rested in possibly using tags to retrieve items from the cache in the future. The Velocity/App Fabric API includes an add method that look like

public DataCacheItemVersion Add(string key, 
                                object value, 
                                IEnumerable<DataCacheTag> tags);

However, there are no methods available for retrieving objects from the cache using tags that do not require a region to be specified.

The problem is that if you use Add() without specifying the region, you have no idea what region the cache manager put the object into; thus you cannot specify the region when retrieving the object. Am I missing something really simple here?


If you are using the DataCache class then there are several overloads available, including the first one which only specifies a key:

  • DataCache.Get (String) - Gets an object from the cache using the specified key.
  • DataCache.Get (String, out DataCacheItemVersion) - Gets an object from the cache using the specified key. You may also provide the version to obtain the specific version of a key, if that version is still the most current in the cache.
  • DataCache.Get (String, String) - Gets an object from the specified region by using the specified key.
  • DataCache.Get (String, out DataCacheItemVersion, String) - Gets an object from the specified region by using the specified key. You may also provide the version to obtain the specific version of a key, if that version is still the most current in the region.

from http://msdn.microsoft.com/en-us/library/microsoft.data.caching.datacache.get.aspx


I finally found the answer to my question in the MSDN. It can be seen here. As it turns out, when you call the overloaded Add() method (the one I originally posted) that accepts tags, you cannot retrieve the object based on the tags. The linked document states:

Tags may only be used to retrieve a cached object if that object is stored in a region. This overload does not store the object in a region.

So, using that overload, the tags essentially just become metadata.

Thanks for the other answers. I up-voted both of them, because they helped lead to me finding this answer.


You can iterate through all regions, but it is not a very optimized way of getting data.

foreach (string regionName in cache.GetSystemRegions())
{
    string value = cache.Get(regionName, key);
}

But Get(key) should work for Add(key). See this question and answer for more info on iterating regions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜