最近项目要做优化,想到 使用缓存,时间少数据库的频繁访问:Redis是首选,可以做分布式缓存来用。
内存缓存也有设计,没启用,怕服务器内容过大。
先定义一个缓存接口:含有Redis生成数据表自增主键方法
/// <summary>
/// 缓存供应器接口
/// </summary>
public interface ICacheProvider
{
string Get(string key);
T Get<T>(string key) where T : class;
void Set(string key, string value);
void Set(string key, object value);
void Delete(string key);
}
下面是实现类:
/// <summary>
/// Redis缓存操作类
/// </summary>
public class RedisCache : ICacheProvider
{
/// <summary>
/// Redis缓存操作类
/// </summary>
public RedisCache()
{
}
public string ServerIP { get; set; } = "127.0.0.1";
public string ServerPort { get; set; } = "6379";
public string PassWord { get; set; } = "123456";
public string DefDB { get; set; } = "0";
private CSRedisClient redisClient { get; set; }
public void Connect()
{
string connStr = this.ServerIP + ":" + this.ServerPort+ ",charset=\"utf-8\", decode_responses=True";
if(string.IsNullOrEmpty(this.PassWord)==false)
{
connStr += ",password=" + this.PassWord;
}
if (string.IsNullOrEmpty(this.DefDB) == false)
{
connStr += ",defaultDatabase=" + this.DefDB;
}
redisClient = new CSRedis.CSRedisClient(connStr);
}
#region ICacheProvider
public void Delete(string key)
{
this.redisClient.Del(key);
}
public string Get(string key)
{
return this.redisClient.Get(key);
}
public T Get<T>(string key) where T : class
{
var rt = this.redisClient.Get(key);
if (rt != null)
{
return rt.FromJson<T>();
}
else
{
return null;
}
}
public void Set(string key, string value)
{
this.redisClient.Set(key, value);
}
public void Set(string key, object value)
{
string rt= Newtonsoft.Json.JsonConvert.SerializeObject(value);
this.redisClient.Set(key, rt);
}
#endregion ICacheProvider
#region PK
/// <summary>
/// 创建自增主键(字段)
/// </summary>
/// <param name="key">PK-表名-字段名</param>
/// <returns></returns>
public long CreatePK(string key)
{
return this.redisClient.IncrBy(key);
if (this.redisClient.Exists(key)==false)
{
this.redisClient.Set(key, 0);
return 0;
}
else
{
//自增主键
return this.redisClient.IncrBy(key);
}
}
/// <summary>
/// 获取当前主键值(字段)
/// </summary>
/// <param name="key">PK-表名-字段名</param>
/// <returns></returns>
public long GetCurPK(string key)
{
return this.redisClient.Get<long>(key);
}
#endregion PK
}
/// <summary>
/// 内存缓存
/// </summary>
public class MemoryCache:ICacheProvider
{
/// <summary>
/// 内存缓存
/// </summary>
public MemoryCache() { }
/// <summary>
/// 字符串字典
/// </summary>
private Dictionary<string, string> dicStr = new Dictionary<string, string>();
/// <summary>
/// 对象字典
/// </summary>
private Dictionary<string,object> dicObject = new Dictionary<string,object>();
#region ICacheProvider
public void Delete(string key)
{
dicStr.Remove(key);
dicObject.Remove(key);
}
public string Get(string key)
{
string vl = "";
if(dicStr.TryGetValue(key,out vl))
{
return vl;
}
else
{
return null;
}
}
public T Get<T>(string key) where T:class
{
if(dicObject.ContainsKey(key))
{
return (T)dicObject[key];
}
else
{
return null;
}
}
public void Set(string key, string value)
{
this.dicStr[key] = value;
}
public void Set(string key, object value)
{
this.dicObject[key] = value;
}
#endregion ICacheProvider
}
JSON扩展方法:
public static class ExtMethod
{
/// <summary>
/// JSON序列化
/// </summary>
/// <param name="obj">对象</param>
/// <returns>JSON字符串</returns>
public static string ToJson(this object obj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
}
/// <summary>
/// JSON反序列化
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="str">JSON字符串</param>
/// <returns>对象</returns>
public static T FromJson<T>(this string str) where T : class
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
}
}
缓存管理类:
/// <summary>
/// 缓存管理
/// </summary>
public class CacheManage
{
/// <summary>
/// 缓存管理
/// </summary>
public CacheManage()
{
}
public ICacheProvider RedisCache;
public ICacheProvider MemoryCache;
public void CreateCacheProvider(CacheType cacheType)
{
}
public void CreateCacheProvider()
{
RedisCache = new RedisCache();
MemoryCache = new MemoryCache();
}
/// <summary>
/// 获取缓存
/// 1.先从Redis读;
/// 2.Redis读不到,从数据库读
/// 3.数据库没有的,使用默认值
/// 4.Redis没有 更新Redis
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">缓存关键字</param>
/// <param name="fromDB">从数据库读取的方法</param>
/// <returns>缓存内容</returns>
public T GetCache<T>(string key, Func<string, T> fromDB) where T : class
{
//1.先从Redis读
var rt = this.RedisCache.Get<T>(key);
bool inRedis = true;
//2.Redis读不到,从数据库读
if (rt == null)
{
inRedis = false;
if(fromDB != null)
{
rt = fromDB(key) as T;
}
}
//3.数据库没有的,使用默认值
if (rt == null)
{
rt = default(T);
}
//4.Redis没有 更新Redis
if (inRedis == false)
{
this.RedisCache.Set(key, rt);
}
return rt;
}
public void DeleteCache(string key)
{
this.RedisCache.Delete(key);
}