/// <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 CacheValue <T> Get <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) where T : class { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); var result = _cache.StringGet(cacheKey); if (!result.IsNull) { var value = _serializer.Deserialize <T>(result); return(new CacheValue <T>(value, true)); } var item = dataRetriever?.Invoke(); if (item != null) { Set(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } else { return(CacheValue <T> .NoValue); } }
/// <summary> /// Caches the delete action. /// </summary> /// <param name="channel">Channel.</param> /// <param name="value">Value.</param> private void CacheDeleteAction(RedisChannel channel, RedisValue value) { var message = _serializer.Deserialize <EasyCachingMessage>(value); //TODO : remove local cache _localCachingProvider.Remove(message.CacheKey); }
public void Deserialize_Object_Should_Succeed() { var bytes = _serializer.Serialize(new Model { Prop = "abc" }); var res = _serializer.Deserialize <Model>(bytes); Assert.Equal("abc", res.Prop); }
/// <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 CacheValue <T> Get <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) where T : class { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); var result = _cache.StringGet(cacheKey); if (!result.IsNull) { if (_options.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } var value = _serializer.Deserialize <T>(result); return(new CacheValue <T>(value, true)); } var item = dataRetriever?.Invoke(); if (item != null) { Set(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } else { if (_options.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } return(CacheValue <T> .NoValue); } }
/// <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.Get <byte[]>(cacheKey); if (result != null) { 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.Set($"{cacheKey}_Lock", 1, (int)TimeSpan.FromMilliseconds(_options.LockMs).TotalSeconds, RedisExistence.Nx)) { System.Threading.Thread.Sleep(_options.SleepMs); return(Get(cacheKey, dataRetriever, expiration)); } var item = dataRetriever(); if (item != null || _options.CacheNulls) { Set(cacheKey, item, expiration); //remove mutex key _cache.Del($"{cacheKey}_Lock"); return(new CacheValue <T>(item, true)); } else { //remove mutex key _cache.Del($"{cacheKey}_Lock"); return(CacheValue <T> .NoValue); } }
protected override void Exec(IEasyCachingSerializer serializer) { var data = serializer.Serialize(_list); var result = serializer.Deserialize <List <MyPoco> >(data); if (result == null) { throw new Exception(); } }
/// <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 CacheValue <T> Get <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 { return(CacheValue <T> .NoValue); } }
/// <summary> /// Subscribes the handle. /// </summary> /// <param name="channel">Channel.</param> /// <param name="value">Value.</param> private void SubscribeHandle(RedisChannel channel, RedisValue value) { var message = _serializer.Deserialize <EasyCachingMessage>(value); switch (message.NotifyType) { case NotifyType.Add: _localCachingProvider.Set(message.CacheKey, message.CacheValue, message.Expiration); break; case NotifyType.Update: _localCachingProvider.Refresh(message.CacheKey, message.CacheValue, message.Expiration); break; case NotifyType.Delete: _localCachingProvider.Remove(message.CacheKey); break; } }
/// <summary> /// Consumers the received. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">E.</param> private void OnConsumerReceived(object sender, BasicDeliverEventArgs e) { var message = _serializer.Deserialize <EasyCachingMessage>(e.Body); switch (message.NotifyType) { case NotifyType.Add: _localCachingProvider.Set(message.CacheKey, message.CacheValue, message.Expiration); break; case NotifyType.Update: _localCachingProvider.Refresh(message.CacheKey, message.CacheValue, message.Expiration); break; case NotifyType.Delete: _localCachingProvider.Remove(message.CacheKey); break; } }
private CacheValue <T> Deserialize <T>(string cacheKey, RedisValue redisValue) { if (redisValue.IsNull) { return(CacheValue <T> .NoValue); } try { var value = _serializer.Deserialize <T>(redisValue); if (value == null && !_options.CacheNulls) { return(CacheValue <T> .NoValue); } return(new CacheValue <T>(value, true)); } catch (Exception ex) { Logger?.LogWarning(ex, "Error while deserializing cache value with key '{0}'.", cacheKey); return(CacheValue <T> .NoValue); } }
/// <summary> /// Subscribes the handle. /// </summary> /// <param name="channel">Channel.</param> /// <param name="value">Value.</param> private void OnMessage(RedisChannel channel, RedisValue value) { var message = _serializer.Deserialize <EasyCachingMessage>(value); _handler?.Invoke(message); }
/// <summary> /// Ons the message. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">E.</param> private void OnMessage(object sender, BasicDeliverEventArgs e) { var message = _serializer.Deserialize <EasyCachingMessage>(e.Body.ToArray()); BaseOnMessage(message); }
/// <summary> /// Ons the message. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">E.</param> private void OnMessage(object sender, BasicDeliverEventArgs e) { var message = _serializer.Deserialize <EasyCachingMessage>(e.Body); _handler?.Invoke(message); }
public void Set_Value_Should_Succeed() { var cacheKey = Guid.NewGuid().ToString(); var cacheValue = "value"; var cacheBytes = new byte[] { 0x01 }; A.CallTo(() => _serializer.Serialize(cacheValue)).Returns(cacheBytes); A.CallTo(() => _serializer.Deserialize <string>(A <byte[]> .Ignored)).Returns(cacheValue); _provider.Set(cacheKey, cacheValue, _defaultTs); var val = _provider.Get <string>(cacheKey, null, _defaultTs); Assert.NotNull(val); Assert.Equal(cacheValue, val.Value); }
/// <inheritdoc/> public T Deserialize <T>(byte[] bytes) { var decompressedBytes = _compressor.Decompress(bytes); return(_easyCachingSerializer.Deserialize <T>(decompressedBytes)); }