Skip to content

Commit 2c78256

Browse files
authored
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
1 parent 699742a commit 2c78256

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

src/EasyCaching.Core/Configurations/BaseProviderOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class BaseProviderOptions
4242
public string SerializerName { get; set; }
4343

4444
/// <summary>
45-
/// Get or sets whether null values should be cached, default is false.
45+
/// Get or sets whether null values should be cached, default is true.
4646
/// </summary>
47-
public bool CacheNulls { get; set; } = false;
47+
public bool CacheNulls { get; set; } = true;
4848
}
4949
}

src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ public override CacheValue<T> BaseGet<T>(string cacheKey)
166166

167167
var cacheItem = _cache.FindOne(c => c.cachekey == cacheKey && c.expiration > DateTimeOffset.UtcNow.ToUnixTimeSeconds());
168168

169-
if (cacheItem != null || _options.CacheNulls)
169+
if (cacheItem != null)
170170
{
171171
if (_options.EnableLogging)
172172
_logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");
173173

174174
CacheStats.OnHit();
175175

176-
return string.IsNullOrWhiteSpace(cacheItem?.cachevalue)
176+
return cacheItem.cachevalue == null
177177
? CacheValue<T>.Null
178178
: new CacheValue<T>(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(cacheItem.cachevalue), true);
179179
}

src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ public DefaultSQLiteCachingProvider(
8585
/// </summary>
8686
/// <param name="dbProvider"></param>
8787
private void InitDb(ISQLiteDatabaseProvider dbProvider)
88-
{
88+
{
8989
var conn = dbProvider.GetConnection();
9090

9191
if (conn.State == System.Data.ConnectionState.Closed)
9292
{
9393
conn.Open();
9494
}
9595

96-
conn.Execute(ConstSQL.CREATESQL);
96+
conn.Execute(ConstSQL.CREATESQL);
9797
}
9898

9999
/// <summary>
@@ -159,16 +159,16 @@ public override CacheValue<T> BaseGet<T>(string cacheKey)
159159
name = _name
160160
}).FirstOrDefault();
161161

162-
if (!string.IsNullOrWhiteSpace(dbResult) || _options.CacheNulls)
162+
if (!string.IsNullOrWhiteSpace(dbResult))
163163
{
164164
CacheStats.OnHit();
165165

166166
if (_options.EnableLogging)
167167
_logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}");
168-
169-
return string.IsNullOrWhiteSpace(dbResult)
168+
var cacheValue = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(dbResult);
169+
return cacheValue == null
170170
? CacheValue<T>.Null
171-
: new CacheValue<T>(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(dbResult), true);
171+
: new CacheValue<T>(cacheValue, true);
172172
}
173173
else
174174
{
@@ -221,7 +221,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
221221
expiration = expiration.Ticks / 10000000
222222
});
223223

224-
}
224+
}
225225

226226
/// <summary>
227227
/// Removes cached item by cachekey's prefix.
@@ -247,10 +247,10 @@ public override void BaseRemoveByPattern(string pattern)
247247

248248
if (_options.EnableLogging)
249249
_logger?.LogInformation($"RemoveByPattern : pattern = {pattern}");
250-
250+
251251
_cache.Execute(ConstSQL.REMOVEBYLIKESQL, new { cachekey = pattern.Replace('*', '%'), name = _name });
252252
}
253-
253+
254254
/// <summary>
255255
/// Sets all.
256256
/// </summary>
@@ -276,7 +276,7 @@ public override void BaseSetAll<T>(IDictionary<string, T> values, TimeSpan expir
276276
}
277277

278278
tran.Commit();
279-
}
279+
}
280280

281281
/// <summary>
282282
/// Gets all.
@@ -339,7 +339,7 @@ public override IDictionary<string, CacheValue<T>> BaseGetByPrefix<T>(string pre
339339

340340
return GetDict<T>(list);
341341
}
342-
342+
343343
/// <summary>
344344
/// Removes all.
345345
/// </summary>

test/EasyCaching.UnitTests/CachingTests/MemoryCachingProviderTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using EasyCaching.Core.Configurations;
2-
using EasyCaching.Core.DistributedLock;
3-
41
namespace EasyCaching.UnitTests
52
{
3+
using EasyCaching.Core.Configurations;
4+
using EasyCaching.Core.DistributedLock;
65
using EasyCaching.Core;
76
using EasyCaching.InMemory;
87
using Microsoft.Extensions.Configuration;

test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.0" />
20+
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.3" />
2121
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
2222
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.0" />
2323
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />

0 commit comments

Comments
 (0)