更新时间:2022-09-15 13:21:02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
namespace CNBlogs.Zzk.Domain.Entities
{ public class DictionaryCacheManager<TK, TV>
{
private ObjectCache memoryCache;
public DictionaryCacheManager(): this ( null ){}
public DictionaryCacheManager( string name)
{
memoryCache = new MemoryCache( string .Format( "{0}-{1}-{2}" , typeof (TK).Name, typeof (TV).Name, name));
}
public TV Get(TK key,Func<TV> getValue)
{
if (memoryCache.Contains(key.ToString()))
{
return (TV)memoryCache[key.ToString()];
}
else
{
var policy = new CacheItemPolicy();
var v = getValue();
object o = v;
memoryCache.Set(key.ToString(), o, policy);
return v;
}
}
public TV Get(TK key, Func<TV> getValue,DateTimeOffset dateTimeOffset)
{
if (memoryCache.Contains(key.ToString()))
{
return (TV)memoryCache[key.ToString()];
}
else
{
var v = getValue();
object o = v;
memoryCache.Set(key.ToString(), o, dateTimeOffset);
return v;
}
}
public void Clear()
{
memoryCache.ToList().ForEach(kv => memoryCache.Remove(kv.Key));
}
public void Clear(TK key)
{
memoryCache.Remove(key.ToString());
}
}
} |
本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2012/06/11/2545450.html,如需转载请自行联系原作者