public static void Init(string sHost, int iPort, string sPassword) { CacheDatabase._Host = sHost; CacheDatabase._Port = iPort; CacheDatabase._Password = sPassword; if (CacheDatabase._Password.Length > 0) { CacheDatabase._AuthRequired = true; } else { CacheDatabase._AuthRequired = false; } _oConectionOptions = new ConfigurationOptions(); if (CacheDatabase._AuthRequired) { CacheDatabase._oConectionOptions.Password = CacheDatabase._Password; } CacheDatabase._oConectionOptions.EndPoints.Add(CacheDatabase._Host + ":" + CacheDatabase._Port.ToString()); CacheDatabase._oCacheConnection = ConnectionMultiplexer.Connect(CacheDatabase._oConectionOptions); CacheDatabase._oCommand = CacheDatabase._oCacheConnection.GetDatabase(); //Check to make sure the Key Exists and if not then set it to 0 if (!_oCommand.KeyExists(CacheDatabase._ObjectCounterKeyName)) { CacheDatabase._oCommand.StringSet(CacheDatabase._ObjectCounterKeyName, 0); } }
/// <summary> /// 验证缓存项是否存在 /// </summary> /// <param name="key">缓存Key</param> /// <returns></returns> public bool Exists(string key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } return(_cache.KeyExists(key)); }
private async Task <List <TValue> > GetAllItemsFromHashAsync <TValue>(string hashCacheKey) where TValue : class { if (string.IsNullOrEmpty(hashCacheKey)) { return(new List <TValue>()); } if (!_redisDatabase.KeyExists(hashCacheKey)) { return(new List <TValue>()); } var hashMembers = await _redisDatabase.HashGetAllAsync(hashCacheKey).ConfigureAwait(false); if (hashMembers.Length < 1) { return(new List <TValue>()); } var resultList = new List <TValue>(); foreach (var hashMember in hashMembers) { // Get the json object out of the partition first var redisValForPartitionMember = await _redisDatabase.HashGetAsync(hashMember.Value.ToString(), hashMember.Name.ToString()).ConfigureAwait(false); if (redisValForPartitionMember.IsNullOrEmpty) { await _redisDatabase.HashDeleteAsync(hashCacheKey, hashMember.Name).ConfigureAwait(false); continue; } // Since we have a json value we deserialize it and add it to the list of return items. var tValueItem = JsonConvert.DeserializeObject <TValue>(redisValForPartitionMember); if (tValueItem == default(TValue)) { await _redisDatabase.HashDeleteAsync(hashCacheKey, hashMember.Name).ConfigureAwait(false); continue; } resultList.Add(tValueItem); } return(resultList); }
public static Int64 GetObjectIDForSpotifyID(string sSpotifyID) { Int64 iID = 0; if (_oCommand.KeyExists(sSpotifyID)) { iID = Convert.ToInt64(CacheDatabase._oCommand.StringGet(sSpotifyID)); } else { iID = CacheDatabase._oCommand.StringIncrement(CacheDatabase._ObjectCounterKeyName); CacheDatabase._oCommand.StringSet(sSpotifyID, iID); } return(iID); }
/// <summary> /// 判断key是否存储 /// </summary> /// <param name="key">redis key</param> /// <returns></returns> public static bool KeyExists(string key) { return(RedisBase.KeyExists(key)); }