开发者

ConcurrentDictionary as static cache

I'm thinking about using class (singleton) with collections implemented using ConcurrentDictionary. This class will be used as a cache implementation (asp.net / wcf).

What do you think about exposing these collections explicitely from such class vs exposing just e.g. 3 methods (get,add,clear) for each of them (using safe methods fr开发者_JAVA百科om CD) ?


As you're implementing a cache then I'd suggest only exposing those methods that you need to the outside world, to prevent any unexpected side-effects that result if a.n.other user fiddles with the dictionary.


A cache without an expiration policy is a memory leak. Whatever policy you come up with is going to affect the Add() method. You have to wrap that method to initialize stuff that lets your expiration logic work. You can't just let the client code manipulate the collection directly.

So, yes, create a class wrapper.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜