一、定义一个缓存接口IChace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 缓存
{
public interface ICache
{
T Get<T>(string key);
void Add(string key,object data,int cacheTime=);
bool Contains(string keys);
void Remove(string key);
void RemoveAll();
object this[string key] { get;set; }
int Count { get; }
}
}
二、定义一个MyCache类用来封装MemoryCache的基本业务,实现缓存接口IChace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text; namespace 缓存
{
class MyCache : ICache
{
/// <summary>
/// 索引器,提供一个全局读写
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object this[string key] { get => Cache.Get(key); set => Add(key,value); } protected MemoryCache Cache
{
get
{
return MemoryCache.Default;
}
} public int Count =>(int)Cache.GetCount() ; /// <summary>
/// 添加缓存
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
/// <param name="cacheTime">分钟</param>
public void Add(string key, object data, int cacheTime = )
{
if (Cache.Contains(key))
{
this.Remove(key);
}
var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
} /// <summary>
/// 判断cache是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Contains(string key)
{
return Cache.Contains(key);
} /// <summary>
/// 读取缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
{
if (Cache.Contains(key))
{
return (T)Cache[key];
}
else
{
return default(T);
}
} /// <summary>
/// 删除缓存
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
Cache.Remove(key);
} /// <summary>
/// 删除所有缓存
/// </summary>
public void RemoveAll()
{
foreach (var item in Cache)
{
this.Remove(item.Key);
}
}
}
}
三、定义一个CacheManager类来管理不同的缓存对象,上端主程序直接调用该类进行业务缓存
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 缓存
{
public class CacheManager
{
private CacheManager()
{ }
private static ICache _cache = null;
static CacheManager()
{
_cache = Activator.CreateInstance(typeof(MyCache)) as ICache;
} /// <summary>
/// 获取缓存数量
/// </summary>
public static int Count => _cache.Count; /// <summary>
/// 判断缓存项是否存在
/// </summary>
/// <param name="key">缓存项</param>
/// <returns></returns>
public static bool Conatins(string key)
{
return _cache.Contains(key);
} /// <summary>
/// 判断是否存在缓存,存在返回结果,不存在返回T类型的默认值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key)
{
if(Conatins(key))
{
return _cache.Get<T>(key);
}
else
{
return default(T);
}
} /// <summary>
/// 判断是否存在缓存,如果没有则先增加再获取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">缓存项</param>
/// <param name="func">没有缓存时所增加的缓存项</param>
/// <param name="cacheTime">默认过期时间</param>
/// <returns></returns>
public static T Get<T>(string key,Func<T> func,int cacheTime=)
{
if (!_cache.Contains(key))
{
_cache.Add(key, func, cacheTime);
}
return _cache.Get<T>(key);
} /// <summary>
/// 删除缓存数据项
/// </summary>
/// <param name="key">缓存项</param>
public static void Remove(string key)
{
_cache.Remove(key);
} /// <summary>
/// 删除所有缓存数据项
/// </summary>
public static void RemoveAll()
{
_cache.RemoveAll();
}
}
}