开发者

Grouping similar objects in web cache (.NET)?

  • I have a bunch of objects all of one class which I want to store in the web cache.
  • There could be a few hundred of these objects.
  • I want objects to have their own individual expiry times, and expire from the cache individually.
  • I want to be able to tell how many of these objects are in the cache, and iterate over these objects alone (regardless of other types in the cache).

Initially I thought about storing a generic collection in the cache, and just inserting the objects into the collection when needed. This is nice and tidy, and would make iteration easy. However, this wouldn't allow each of the objects to expire individually (the whole collection would expire). I suppose I would have to create my own expiry mechanism and 'garbage collection' in this case.

My second thought was to just store each of the objects as first class citizens of the cache. This would obviously allow them to expire individually, but seems a bit messy. How to iterate over them? Perhaps just iterate over the whole cach开发者_运维知识库e, and look for the right types (not sure this would work), or by using a key prefix. It would be the easiest I suppose, but doesn't seem ideal.

I guess what I really want is a way to group the cache items together, but for them to remain cache item. Something like a sub-cache I suppose.

Can anyone suggest the best way forward?


You could create a wrapper class and reference the cached items that way.

Example:

public interface IExample
{
    string FirstProperty { get; set; }
    string SecondProperty { get; set; }
}

public class OriginalClass : IExample
{
    public string FirstProperty { get; set; }
    public string SecondProperty { get; set; }
}

public class CachedClass : IExample
{
    //initialize this somewhere
    private OriginalClass Wrapped { get; set;}

    public string FirstProperty
    {
        get
        {
            return Cache["FirstProperty"];
        }
        set
        {
            Wrapped.FirstProperty = value;
            Cache["FirstProperty"] = value;
        }
    }

    public string SecondProperty
    {
        get
        {
            return Cache["SecondProperty"];
        }
        set
        {
            Wrapped.SecondProperty= value;
            Cache["SecondProperty"] = value;
        }
    }
}

You would need to add some additional logic to check if the cache is empty, but you get the idea.


I've done something similar to this, by implementing a CacheManager class that manages a group of related items in the ASP.NET cache.

Each CacheManager instance uses a unique prefix prepended to the cache key that identifies items it manages. Thus you can identify and count items in a group by iterating over the whole cache and looking for your unique prefix. (However this could be costly, so if you want to frequently iterate over items in a group you might want to look at a different solution, such as a collection of weak references to cached items in the group).

Items in a group are stored individually in the cache so can have their own expiry times (though my implementation gives the same expiry time to each item in a group).

I also optionally implement a CacheDependency on a single cache key that is unique for each CacheManager. This makes it possible to easily clear all items in a group from the cache.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜