/// <summary> /// 删除指定的key /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool Delete(string key) { return(SuccessOrFail(() => { if (RedisConfigHelper.GetEnableMemoryCache()) { GetCache().Remove(key); } RedisHelper.Delete(key); })); }
/// <summary> /// 缓存中是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool Exist(string key) { if (RedisConfigHelper.GetEnableMemoryCache()) { object cache; if (GetCache().TryGetValue(key, out cache)) { return(true); } } return(RedisHelper.IsExist(key)); }
/// <summary> /// 保存缓存 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expiresencends">缓存有效时间,单位秒</param> /// <returns></returns> public static bool Set(string key, object value, int expiresencends) { IsNullOrEmptyString(key); return(SuccessOrFail(() => { if (RedisConfigHelper.GetEnableMemoryCache()) { GetCache().Set(key, value, TimeSpan.FromSeconds(expiresencends)); } RedisHelper.Set(key, value, expiresencends); })); }
/// <summary> /// 清空缓存 /// </summary> /// <returns></returns> public static bool Clear() { return(SuccessOrFail(() => { List <string> keys = RedisHelper.GetAllKeys(); if (RedisConfigHelper.GetEnableMemoryCache()) { foreach (var key in keys) { cache.Remove(key); } } RedisHelper.DeleteAllKeys(); })); }
/// <summary> /// 保存缓存 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expireTime">失效时间</param> /// <returns></returns> public static bool Set(string key, object value, DateTime expireTime) { IsNullOrEmptyString(key); return(SuccessOrFail(() => { if (RedisConfigHelper.GetEnableMemoryCache()) { GetCache().Set(key, value, new MemoryCacheEntryOptions() { AbsoluteExpiration = expireTime }); } RedisHelper.Set(key, value, expireTime); })); }
/// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static T Get <T>(string key) { try { IsNullOrEmptyString(key); if (RedisConfigHelper.GetEnableMemoryCache()) { T cache; if (GetCache().TryGetValue <T>(key, out cache)) { return(cache); } } return(RedisHelper.Get <T>(key)); } catch (Exception ex) { return(default(T)); } }