From 2c78256c030b7378eccfd4417a56b4c712f2b628 Mon Sep 17 00:00:00 2001 From: Memoyu Date: Mon, 20 Nov 2023 23:41:26 +0800 Subject: [PATCH] Update CacheNulls default to true (#508) * fix: update CacheNulls to true #506 * fix: update baset test version to 1.9.3 * fix: fixed cache unll value judgment of SQLLite BaseGet * fixed cache unll value judgment of LiteDB BaseGet --- .../Configurations/BaseProviderOptions.cs | 4 ++-- .../DefaultLiteDBCachingProvider.cs | 4 ++-- .../DefaultSQLiteCachingProvider.cs | 22 +++++++++---------- .../CachingTests/MemoryCachingProviderTest.cs | 5 ++--- .../EasyCaching.UnitTests.csproj | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/EasyCaching.Core/Configurations/BaseProviderOptions.cs b/src/EasyCaching.Core/Configurations/BaseProviderOptions.cs index 3ec88f96..b1d2dfe2 100644 --- a/src/EasyCaching.Core/Configurations/BaseProviderOptions.cs +++ b/src/EasyCaching.Core/Configurations/BaseProviderOptions.cs @@ -42,8 +42,8 @@ public class BaseProviderOptions public string SerializerName { get; set; } /// - /// Get or sets whether null values should be cached, default is false. + /// Get or sets whether null values should be cached, default is true. /// - public bool CacheNulls { get; set; } = false; + public bool CacheNulls { get; set; } = true; } } diff --git a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs index bf2e5809..19d277d4 100644 --- a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs +++ b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs @@ -166,14 +166,14 @@ public override CacheValue BaseGet(string cacheKey) var cacheItem = _cache.FindOne(c => c.cachekey == cacheKey && c.expiration > DateTimeOffset.UtcNow.ToUnixTimeSeconds()); - if (cacheItem != null || _options.CacheNulls) + if (cacheItem != null) { if (_options.EnableLogging) _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); CacheStats.OnHit(); - return string.IsNullOrWhiteSpace(cacheItem?.cachevalue) + return cacheItem.cachevalue == null ? CacheValue.Null : new CacheValue(Newtonsoft.Json.JsonConvert.DeserializeObject(cacheItem.cachevalue), true); } diff --git a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs index 95a76d68..3a47ca7b 100644 --- a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs +++ b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs @@ -85,7 +85,7 @@ public DefaultSQLiteCachingProvider( /// /// private void InitDb(ISQLiteDatabaseProvider dbProvider) - { + { var conn = dbProvider.GetConnection(); if (conn.State == System.Data.ConnectionState.Closed) @@ -93,7 +93,7 @@ private void InitDb(ISQLiteDatabaseProvider dbProvider) conn.Open(); } - conn.Execute(ConstSQL.CREATESQL); + conn.Execute(ConstSQL.CREATESQL); } /// @@ -159,16 +159,16 @@ public override CacheValue BaseGet(string cacheKey) name = _name }).FirstOrDefault(); - if (!string.IsNullOrWhiteSpace(dbResult) || _options.CacheNulls) + if (!string.IsNullOrWhiteSpace(dbResult)) { CacheStats.OnHit(); if (_options.EnableLogging) _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); - - return string.IsNullOrWhiteSpace(dbResult) + var cacheValue = Newtonsoft.Json.JsonConvert.DeserializeObject(dbResult); + return cacheValue == null ? CacheValue.Null - : new CacheValue(Newtonsoft.Json.JsonConvert.DeserializeObject(dbResult), true); + : new CacheValue(cacheValue, true); } else { @@ -221,7 +221,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati expiration = expiration.Ticks / 10000000 }); - } + } /// /// Removes cached item by cachekey's prefix. @@ -247,10 +247,10 @@ public override void BaseRemoveByPattern(string pattern) if (_options.EnableLogging) _logger?.LogInformation($"RemoveByPattern : pattern = {pattern}"); - + _cache.Execute(ConstSQL.REMOVEBYLIKESQL, new { cachekey = pattern.Replace('*', '%'), name = _name }); } - + /// /// Sets all. /// @@ -276,7 +276,7 @@ public override void BaseSetAll(IDictionary values, TimeSpan expir } tran.Commit(); - } + } /// /// Gets all. @@ -339,7 +339,7 @@ public override IDictionary> BaseGetByPrefix(string pre return GetDict(list); } - + /// /// Removes all. /// diff --git a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs index 0795a6ea..511e5f2d 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs @@ -1,8 +1,7 @@ -using EasyCaching.Core.Configurations; -using EasyCaching.Core.DistributedLock; - namespace EasyCaching.UnitTests { + using EasyCaching.Core.Configurations; + using EasyCaching.Core.DistributedLock; using EasyCaching.Core; using EasyCaching.InMemory; using Microsoft.Extensions.Configuration; diff --git a/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj b/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj index b1f6327f..f605ef48 100644 --- a/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj +++ b/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj @@ -17,7 +17,7 @@ - +