patterncsharpMinor
C# small caching API
Viewed 0 times
cachingsmallapi
Problem
public interface ICacheable
{
TV Get(TK key);
void Add(TK key, TV val);
}
public abstract class BaseCache
{
protected static Dictionary> data = new Dictionary>();
private Guid StorageNameSpage;
protected BaseCache(Guid storageNameSpage)
{
StorageNameSpage = storageNameSpage;
}
protected BaseCache()
{
StorageNameSpage = Guid.NewGuid();
}
public TV Get(TK key)
{
return data[StorageNameSpage][key];
}
public void Add(TK key, TV val)
{
data.Add(....);
}
}2 different ways to implement it:
"mulityTon" or what ever you want to call it.
public class KeyValCacheStorage : BaseCache, ICacheable
{
}SingleTome
public class KeyValCacheStorageSingleTon : BaseCache, ICacheable
{
private static Guid storageNameSpage = Guid.NewGuid();
public KeyValCacheStorageSingleTon():base(storageNameSpage)
{
}
}usage:
KeyValCacheStorageSingleTon storageSingleTon1 = new KeyValCacheStorageSingleTon();
storageSingleTon1.Add("storageSingleTon1", "KeyValCacheStorageSingleTon");
storageSingleTon1.Add("bla", "yada");
KeyValCacheStorageSingleTon storageSingleTon2 = new KeyValCacheStorageSingleTon();
storageSingleTon2.Add("storageSingleTon2", "KeyValCacheStorageSingleTon2");Solution
So I've made a few modifications; here are the explanations:
The code:
- Used interfaces a bit more liberally (
IDictionary)
- Made
BaseClassimplementICacheableso the subclasses don't explicitly have to
- Made fields
readonlyas appropriate to declare intent
- Made field
dataprivateand exposed asprotectedby way of a property
- Chained the
BaseCacheconstructors
- Created the storage dictionary in the constructor (could do this in
Addif you were looking for lazy creation)
sealedsubclasses (if they're intended to be non-inheritable, of course)
The code:
public interface ICacheable
{
TV Get(TK key);
void Add(TK key, TV val);
}
public abstract class BaseCache : ICacheable
{
private static readonly IDictionary> data = new Dictionary>();
private readonly Guid storageNameSpace;
protected BaseCache(Guid storageNameSpace)
{
this.storageNameSpace = storageNameSpace;
data[this.storageNameSpace] = new Dictionary();
}
protected BaseCache() : this (Guid.NewGuid())
{
}
protected static IDictionary> Data
{
get
{
return data;
}
}
public TV Get(TK key)
{
return data[this.storageNameSpace][key];
}
public void Add(TK key, TV val)
{
data[this.storageNameSpace].Add(key, val);
}
}
public sealed class KeyValCacheStorage : BaseCache
{
}
public sealed class KeyValCacheStorageSingleton : BaseCache
{
private static readonly Guid storageNameSpace = Guid.NewGuid();
public KeyValCacheStorageSingleton() : base(storageNameSpace)
{
}
}Code Snippets
public interface ICacheable<TK, TV>
{
TV Get(TK key);
void Add(TK key, TV val);
}
public abstract class BaseCache<TK, TV> : ICacheable<TK, TV>
{
private static readonly IDictionary<Guid, IDictionary<TK, TV>> data = new Dictionary<Guid, IDictionary<TK, TV>>();
private readonly Guid storageNameSpace;
protected BaseCache(Guid storageNameSpace)
{
this.storageNameSpace = storageNameSpace;
data[this.storageNameSpace] = new Dictionary<TK, TV>();
}
protected BaseCache() : this (Guid.NewGuid())
{
}
protected static IDictionary<Guid, IDictionary<TK, TV>> Data
{
get
{
return data;
}
}
public TV Get(TK key)
{
return data[this.storageNameSpace][key];
}
public void Add(TK key, TV val)
{
data[this.storageNameSpace].Add(key, val);
}
}
public sealed class KeyValCacheStorage<TK, TV> : BaseCache<TK, TV>
{
}
public sealed class KeyValCacheStorageSingleton<TK, TV> : BaseCache<TK, TV>
{
private static readonly Guid storageNameSpace = Guid.NewGuid();
public KeyValCacheStorageSingleton() : base(storageNameSpace)
{
}
}Context
StackExchange Code Review Q#10146, answer score: 4
Revisions (0)
No revisions yet.