diff --git a/Grand.Core/Caching/DistributedRedisCache.cs b/Grand.Core/Caching/DistributedRedisCache.cs index edb54179cf..2cc541e1bb 100644 --- a/Grand.Core/Caching/DistributedRedisCache.cs +++ b/Grand.Core/Caching/DistributedRedisCache.cs @@ -11,6 +11,7 @@ public partial class DistributedRedisCache : ICacheManager { private readonly IDatabase _distributedCache; private readonly IConnectionMultiplexer _connectionMultiplexer; + private static readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore }; public DistributedRedisCache(string redisConnectionString) { @@ -60,6 +61,7 @@ public T Get(string key) return (JsonConvert.DeserializeObject(res), true); } } + public (T Result, bool FromCache) TryGetValue(string key) { var res = _distributedCache.StringGet(key); @@ -73,7 +75,6 @@ public T Get(string key) } } - public virtual async Task RemoveAsync(string key) { await _distributedCache.KeyDeleteAsync(key, CommandFlags.PreferMaster); @@ -85,7 +86,7 @@ public virtual async Task SetAsync(string key, object data, int cacheTime) return; //serialize item - var serializedItem = JsonConvert.SerializeObject(data); + var serializedItem = JsonConvert.SerializeObject(data, _jsonSettings); //and set it to cache await _distributedCache.StringSetAsync(key, serializedItem, TimeSpan.FromMinutes(cacheTime), When.Always, CommandFlags.FireAndForget); @@ -97,11 +98,12 @@ public void Set(string key, object data, int cacheTime) return; //serialize item - var serializedItem = JsonConvert.SerializeObject(data); + var serializedItem = JsonConvert.SerializeObject(data, _jsonSettings); //and set it to cache _distributedCache.StringSet(key, serializedItem, TimeSpan.FromMinutes(cacheTime), When.Always, CommandFlags.FireAndForget); } + public bool IsSet(string key) { return _distributedCache.KeyExists(key); @@ -136,10 +138,5 @@ public virtual void Dispose() { //nothing special } - - - - - } } \ No newline at end of file