public T Get <T>(string key) { var result = _cache.StringGet(key); if (result.HasValue && !result.IsNull) { return(_serializer.Deserialize <T>(result)); } return(default(T)); }
/// <summary> /// Get the specified cacheKey, dataRetriever and expiration. /// </summary> /// <returns>The get.</returns> /// <param name="cacheKey">Cache key.</param> /// <param name="dataRetriever">Data retriever.</param> /// <param name="expiration">Expiration.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> protected override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); var result = _redisDb.StringGet(cacheKey); if (!result.IsNull) { _cacheStats.OnHit(); if (_cacheOptions.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } var value = _serializer.Deserialize <T>(result); return(new CacheValue <T>(value, true)); } _cacheStats.OnMiss(); if (_cacheOptions.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } var flag = _redisDb.Lock(cacheKey, _cacheOptions.LockMs / 1000); if (!flag.Success) { System.Threading.Thread.Sleep(_cacheOptions.SleepMs); return(Get(cacheKey, dataRetriever, expiration)); } var item = dataRetriever(); if (item != null) { Set(cacheKey, item, expiration); //remove mutex key _redisDb.SafedUnLock(cacheKey, flag.LockValue); return(new CacheValue <T>(item, true)); } else { //remove mutex key _redisDb.SafedUnLock(cacheKey, flag.LockValue); return(CacheValue <T> .NoValue); } }
/// <summary> /// Deserializes the byte array into an instance of T /// </summary> /// <param name="serializer">Serializer</param> /// <param name="bytes">The data to deserialize</param> /// <typeparam name="T">The type to instantiate</typeparam> /// <returns>The deserialized object</returns> public static T Deserialize <T>(this ICachingSerializer serializer, byte[] bytes) { if (serializer == null) { throw new ArgumentNullException(nameof(serializer)); } return((T)serializer.Deserialize(typeof(T), bytes)); }
/// <summary> /// Get the specified cacheKey, dataRetriever and expiration. /// </summary> /// <returns>The get.</returns> /// <param name="cacheKey">Cache key.</param> /// <param name="dataRetriever">Data retriever.</param> /// <param name="expiration">Expiration.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); var result = _cache.StringGet(cacheKey); if (!result.IsNull) { CacheStats.OnHit(); if (_options.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } var value = _serializer.Deserialize <T>(result); return(new CacheValue <T>(value, true)); } CacheStats.OnMiss(); if (_options.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } if (!_cache.StringSet($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs), When.NotExists)) { System.Threading.Thread.Sleep(_options.SleepMs); return(Get(cacheKey, dataRetriever, expiration)); } var item = dataRetriever(); if (item != null) { Set(cacheKey, item, expiration); //remove mutex key _cache.KeyDelete($"{cacheKey}_Lock"); return(new CacheValue <T>(item, true)); } else { //remove mutex key _cache.KeyDelete($"{cacheKey}_Lock"); return(CacheValue <T> .NoValue); } }
public T Get <T>(string key) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } var result = _cache.StringGet(key); if (result.HasValue && !result.IsNull) { return(_serializer.Deserialize <T>(result)); } return(default);
public override object GetOrDefault(string key) { var objbyte = _database.StringGet(key); return(objbyte.HasValue ? _serializer.Deserialize <object>(objbyte) : null); }
/// <summary> /// Get the specified cacheKey, dataRetriever and expiration. /// </summary> /// <returns>The get.</returns> /// <param name="cacheKey">Cache key.</param> /// <param name="dataRetriever">Data retriever.</param> /// <param name="expiration">Expiration.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> protected override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); if (!_cacheOptions.PenetrationSetting.Disable) { var exists = _redisDb.BloomExistsAsync(_cacheOptions.PenetrationSetting.BloomFilterSetting.Name, cacheKey).GetAwaiter().GetResult(); if (!exists) { if (_cacheOptions.EnableLogging) { _logger?.LogInformation($"Cache Penetrated : cachekey = {cacheKey}"); } return(CacheValue <T> .NoValue); } } var result = _redisDb.StringGet(cacheKey); if (!result.IsNull) { _cacheStats.OnHit(); if (_cacheOptions.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } var value = _serializer.Deserialize <T>(result); return(new CacheValue <T>(value, true)); } _cacheStats.OnMiss(); if (_cacheOptions.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } var flag = _redisDb.Lock(cacheKey, _cacheOptions.LockMs / 1000); if (!flag.Success) { System.Threading.Thread.Sleep(_cacheOptions.SleepMs); return(Get(cacheKey, dataRetriever, expiration)); } try { var item = dataRetriever(); if (item != null) { Set(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } else { return(CacheValue <T> .NoValue); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } finally { //remove mutex key _redisDb.SafedUnLock(cacheKey, flag.LockValue); } }
public async Task <T> GetAsync <T>(string key, CancellationToken cancellationToken = default) { var bytes = await _cache.GetAsync(key, cancellationToken); return(_serializer.Deserialize <T>(bytes)); }