public static class Cache
{
private static readonly Timer CleanupTimer = new Timer()
{
AutoReset = true, Enabled = true, Interval = 60000
};
private static readonly Dictionary <string, CacheItem> InternalCache = new Dictionary <string, CacheItem> ();
static Cache()
{
CleanupTimer.Elapsed += Clean;
CleanupTimer.Start();
}
private static void Clean(object sender, ElapsedEventArgs e)
{
InternalCache.Keys.ToList().ForEach(x =>
{
try
{
if(InternalCache[x].ExpireTime <= e.SignalTime)
{
Remove(x);
}
}
catch(Exception)
{ }
});
}
public static bool ContainsKey(string key)
{
return InternalCache.ContainsKey(key);
}
public static T Get <T> (string key, int expiresMinutes, Func <T> refreshFunction)
{
if(InternalCache.ContainsKey(key) && InternalCache[key].ExpireTime> DateTime.Now)
{
return(T) InternalCache[key].Item;
}
var result = refreshFunction();
Set(key, result, expiresMinutes);
return result;
}
public static void Set(string key, object item, int expiresMinutes)
{
Remove(key);
InternalCache.Add(key, new CacheItem(item, expiresMinutes));
}
public static void Remove(string key)
{
if(InternalCache.ContainsKey(key))
{
InternalCache.Remove(key);
}
}
private struct CacheItem
{
public CacheItem(object item, int expiresMinutes): this()
{
Item = item;
ExpireTime = DateTime.Now.AddMinutes(expiresMinutes);
}
public object Item
{
get;
private set;
}
public DateTime ExpireTime
{
get;
private set;
}
}
}
C# Dictionary实现缓存
public class CacheHelper
{
private static Dictionary <string, object> CacheDictionary = new Dictionary <string, object> ();
public static void Add(string key, object value)
{
CacheDictionary.Add(key, value);
}
public static T Get < T > (string key)
{
return(T) CacheDictionary[key];
}
public static void Set(string key, object value)
{
CacheDictionary[key] = value;
}
public static bool Exsits(string key)
{
return CacheDictionary.ContainsKey(key);
}
public static async Task SetDateTime(string key, int seconds)
{
ThreadPool.QueueUserWorkItem(o =>
{
Thread.Sleep(1000 * seconds);
CacheDictionary.Remove(key);
});
}
}