/// <summary> /// listRight的追加 /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <param name="value"></param> /// <returns></returns> public static long PushListRight(RedisCacheType Type, string Key, string value) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); return(db.ListRightPush(Key, value)); }
/// <summary> /// 设置Key的过期时间 /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> public static bool SetKeyExpire(RedisCacheType Type, string Key, int ExpireMinutes) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); return(db.KeyExpire(Key, TimeSpan.FromMinutes(ExpireMinutes))); }
/// <summary> /// 移除Key的过期时间 /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> public static bool RemoveKeyExpire(RedisCacheType Type, string Key) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); return(db.KeyPersist(Key)); }
/// <summary> /// Key是否存在 /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> public static bool ExistKey(RedisCacheType Type, string Key) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); return(db.KeyExists(Key)); }
/// <summary> /// 获取Key的Value /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> public static T GetValue <T>(RedisCacheType Type, string Key) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); string value = db.StringGet(Key); return(JsonHelper.Deserialize <T>(value)); }
/// <summary> /// 获取Key的Value /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> public static string GetValue(RedisCacheType Type, string Key) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); string value = db.StringGet(Key); return(value); }
/// <summary> /// listLeft的Pop /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> public static string PopListLeft(RedisCacheType Type, string Key) { try { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); return(db.ListLeftPop(Key)); } catch (Exception ex) { logger.Info(ex); return(null); } }
/// <summary> /// 设置Key /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expireSecond"></param> /// <returns></returns> public static bool SetKey(RedisCacheType Type, string Key, string Value, int ExpireMinutes = 0) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); if (ExpireMinutes > 0) { return(db.StringSet(Key, Value, TimeSpan.FromMinutes(ExpireMinutes))); } else { return(db.StringSet(Key, Value)); } }
/// <summary> /// 设置Key /// </summary> /// <typeparam name="T"></typeparam> /// <param name="Type"></param> /// <param name="Key"></param> /// <param name="Value"></param> /// <param name="ExpireMinutes"></param> /// <returns></returns> public static bool SetKey <T>(RedisCacheType Type, string Key, T Value, int ExpireMinutes = 0) { var db = RedisMananger.GetDatabase((int)Type); Key = GetRedisKey(Type, Key); string jsonStr = JsonHelper.ToJsonString(Value); if (ExpireMinutes > 0) { return(db.StringSet(Key, jsonStr, TimeSpan.FromMinutes(ExpireMinutes))); } else { return(db.StringSet(Key, jsonStr)); } }
/// <summary> /// 获取RedisKey /// </summary> /// <param name="Type"></param> /// <param name="Key"></param> /// <returns></returns> private static string GetRedisKey(RedisCacheType Type, string Key) { return(Type.ToString() + "_" + Key); }