Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CacheNulls default to true #508

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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