IamDeveloper
IamDeveloper

Reputation: 5226

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 from CD) ?

Upvotes: 2

Views: 2602

Answers (2)

Hans Passant
Hans Passant

Reputation: 941605

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.

Upvotes: 4

Will A
Will A

Reputation: 24988

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.

Upvotes: 5

Related Questions