Skip to content

Commit

Permalink
Update CacheNulls default to true (dotnetcore#508)
Browse files Browse the repository at this point in the history
* fix: update CacheNulls to true dotnetcore#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
  • Loading branch information
Memoyu authored Nov 20, 2023
1 parent 699742a commit 2c78256
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/EasyCaching.Core/Configurations/BaseProviderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class BaseProviderOptions
public string SerializerName { get; set; }

/// <summary>
/// Get or sets whether null values should be cached, default is false.
/// Get or sets whether null values should be cached, default is true.
/// </summary>
public bool CacheNulls { get; set; } = false;
public bool CacheNulls { get; set; } = true;
}
}
4 changes: 2 additions & 2 deletions src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ public override CacheValue<T> BaseGet<T>(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<T>.Null
: new CacheValue<T>(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(cacheItem.cachevalue), true);
}
Expand Down
22 changes: 11 additions & 11 deletions src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public DefaultSQLiteCachingProvider(
/// </summary>
/// <param name="dbProvider"></param>
private void InitDb(ISQLiteDatabaseProvider dbProvider)
{
{
var conn = dbProvider.GetConnection();

if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}

conn.Execute(ConstSQL.CREATESQL);
conn.Execute(ConstSQL.CREATESQL);
}

/// <summary>
Expand Down Expand Up @@ -159,16 +159,16 @@ public override CacheValue<T> BaseGet<T>(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<T>(dbResult);
return cacheValue == null
? CacheValue<T>.Null
: new CacheValue<T>(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(dbResult), true);
: new CacheValue<T>(cacheValue, true);
}
else
{
Expand Down Expand Up @@ -221,7 +221,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
expiration = expiration.Ticks / 10000000
});

}
}

/// <summary>
/// Removes cached item by cachekey's prefix.
Expand All @@ -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 });
}

/// <summary>
/// Sets all.
/// </summary>
Expand All @@ -276,7 +276,7 @@ public override void BaseSetAll<T>(IDictionary<string, T> values, TimeSpan expir
}

tran.Commit();
}
}

/// <summary>
/// Gets all.
Expand Down Expand Up @@ -339,7 +339,7 @@ public override IDictionary<string, CacheValue<T>> BaseGetByPrefix<T>(string pre

return GetDict<T>(list);
}

/// <summary>
/// Removes all.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.0" />
<PackageReference Include="EasyCaching.BaseTest" Version="1.9.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
Expand Down

0 comments on commit 2c78256

Please sign in to comment.