using System; using System.Collections.Generic; using System.Threading.Tasks; namespace learun.cache { /// /// /// public class CacheByRedis : ICache { #region Key-Value /// /// 读取缓存 /// /// 类型 /// 键 /// 指定库ID,默认0 /// public T Read(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).StringGet(cacheKey); } /// /// 写入缓存 /// /// 类型 /// 键 /// 对象数据 /// 指定库ID,默认0 public void Write(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).StringSet(cacheKey, value); } /// /// 写入缓存 /// /// 类型 /// 对象数据 /// 键 /// 到期时间 /// 指定库ID,默认0 public void Write(string cacheKey, T value, TimeSpan expireTime, int dbId = 0) where T : class { new RedisCache(dbId, null).StringSet(cacheKey, value, expireTime); } /// /// 移除指定数据缓存 /// /// 键 /// 指定库ID,默认0 public void Remove(string cacheKey, int dbId = 0) { new RedisCache(dbId, null).KeyDelete(cacheKey); } /// /// 移除全部缓存 /// /// 指定库ID,默认0 public void RemoveAll(int dbId = 0) { new RedisCache().FlushDatabase(dbId); } #endregion Key-Value #region Key-Value 异步 /// /// 读取缓存 /// /// 类型 /// 键 /// 指定库ID,默认0 /// public Task ReadAsync(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).StringGetAsync(cacheKey); } /// /// 写入缓存 /// /// 类型 /// 对象数据 /// 键 /// 指定库ID,默认0 public async Task WriteAsync(string cacheKey, T value, int dbId = 0) where T : class { await new RedisCache(dbId, null).StringSetAsync(cacheKey, value); } /// /// 写入缓存 /// /// 对象数据 /// 键 /// 到期时间 /// 指定库ID,默认0 public async Task WriteAsync(string cacheKey, T value, TimeSpan timeSpan, int dbId = 0) where T : class { await new RedisCache(dbId, null).StringSetAsync(cacheKey, value, timeSpan); } /// /// 移除指定数据缓存 /// /// 键 /// 指定库ID,默认0 public async Task RemoveAsync(string cacheKey, int dbId = 0) { await new RedisCache(dbId, null).KeyDeleteAsync(cacheKey); } /// /// 移除全部缓存 /// /// 指定库ID,默认0 public async Task RemoveAllAsync(int dbId = 0) { await new RedisCache().FlushDatabaseAsync(dbId); } #endregion Key-Value 异步 #region List #region 同步方法 /// /// 移除指定ListId的内部List的值 /// /// 键值 /// 对象数据 /// 指定库ID,默认0 public void ListRemove(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).ListRemove(cacheKey, value); } /// /// 获取指定key的List /// /// 键值 /// 指定库ID,默认0 /// public List ListRange(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).ListRange(cacheKey); } /// /// 入队 /// /// /// /// 指定库ID,默认0 public void ListRightPush(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).ListRightPush(cacheKey, value); } /// /// 出队 /// /// /// /// 指定库ID,默认0 /// public T ListRightPop(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).ListRightPop(cacheKey); } /// /// 入栈 /// /// /// /// /// 指定库ID,默认0 public void ListLeftPush(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).ListLeftPush(cacheKey, value); } /// /// 出栈 /// /// /// /// 指定库ID,默认0 /// public T ListLeftPop(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).ListLeftPop(cacheKey); } /// /// 获取集合中的数量 /// /// /// 指定库ID,默认0 /// public long ListLength(string cacheKey, int dbId = 0) { return new RedisCache(dbId, null).ListLength(cacheKey); } #endregion 同步方法 #endregion List } }