Skip to content

[pull] dev from dotnetcore:dev #14

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

Merged
merged 2 commits into from
Oct 13, 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
2 changes: 1 addition & 1 deletion src/EasyCaching.CSRedis/EasyCaching.CSRedis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CSRedisCore" Version="3.6.9" />
<PackageReference Include="CSRedisCore" Version="3.8.671" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EasyCaching.Core\EasyCaching.Core.csproj" />
Expand Down
12 changes: 6 additions & 6 deletions src/EasyCaching.InMemory/Internal/InMemoryCaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void Clear(string prefix = "")
public int GetCount(string prefix = "")
{
return string.IsNullOrWhiteSpace(prefix)
? _memory.Count
: _memory.Count(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow).Count()
: _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow).Count();
}

internal void RemoveExpiredKey(string key)
Expand Down Expand Up @@ -283,7 +283,7 @@ public int RemoveByPattern(string searchKey, SearchKeyPattern searchPattern)
public IEnumerable<string> GetAllKeys(string prefix)
{
return _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow)
.Select(x=> x.Key).ToList();
.Select(x => x.Key).ToList();
}

private static bool FilterByPattern(string key, string searchKey, SearchKeyPattern searchKeyPattern)
Expand Down Expand Up @@ -314,10 +314,10 @@ public IDictionary<string, CacheValue<T>> GetAll<T>(IEnumerable<string> keys)

public IDictionary<string, CacheValue<T>> GetAll<T>(string prefix = "")
{
var values = string.IsNullOrEmpty(prefix)
? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow)
var values = string.IsNullOrEmpty(prefix)
? _memory.Values.Where(x => x.ExpiresAt > SystemClock.UtcNow)
: _memory.Values.Where(x => x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && x.ExpiresAt > SystemClock.UtcNow);

return values.ToDictionary(k => k.Key, v => new CacheValue<T>(v.GetValue<T>(_options.EnableReadDeepClone), true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ protected override IEasyCachingProvider CreateCachingProvider(Action<BaseProvide
return serviceProvider.GetService<IEasyCachingProvider>();
}

/*[Fact]
public async void Use_Redis6_ACL_Should_Succeed()
{
IServiceCollection services = new ServiceCollection();
services.AddEasyCaching(x =>
x.UseCSRedis(options =>
{
options.DBConfig = new CSRedisDBOptions
{
ConnectionStrings = new System.Collections.Generic.List<string>
{
"127.0.0.1:6388,user=user,password=userpwd,defaultDatabase=13,poolsize=10"
}
};
}).UseCSRedisLock().WithJson(EasyCachingConstValue.DefaultCSRedisName));
IServiceProvider serviceProvider = services.BuildServiceProvider();
var provider = serviceProvider.GetService<IEasyCachingProvider>();
var key = Guid.NewGuid().ToString();
var value = "value";
await provider.SetAsync(key, value, TimeSpan.FromSeconds(20));

var getValue = await provider.GetAsync<string>(key);

Assert.Equal(value, getValue?.Value);
}*/

[Fact]
public void GetDatabase_Should_Succeed()
{
Expand Down Expand Up @@ -148,7 +174,7 @@ public CSRedisCachingProviderWithKeyPrefixTest()
config.SerializerName = "json";

}, "WithKeyPrefix");

x.WithJson("json");
});

Expand Down Expand Up @@ -194,7 +220,7 @@ public void RemoveByPrefixTest()
Assert.False(val3.HasValue);
Assert.False(val4.HasValue);
}

[Theory]
[InlineData("WithKeyPrefix")]
[InlineData("NotKeyPrefix")]
Expand All @@ -219,7 +245,7 @@ public void RemoveByKeyPatternTest(string provider)
var val6 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val7 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val8 = WithKeyPrefix.Get<string>("akey");

Assert.True(val1.HasValue);
Assert.True(val2.HasValue);
Assert.True(val3.HasValue);
Expand All @@ -231,15 +257,15 @@ public void RemoveByKeyPatternTest(string provider)

// contains
WithKeyPrefix.RemoveByPattern("*:pots:*");

// postfix
WithKeyPrefix.RemoveByPattern("*foo");

// prefix
WithKeyPrefix.RemoveByPattern("sky*");
WithKeyPrefix.RemoveByPattern("sky*");

// exact
WithKeyPrefix.RemoveByPattern("akey");
WithKeyPrefix.RemoveByPattern("akey");

var val9 = WithKeyPrefix.Get<string>("garden:pots:flowers");
var val10 = WithKeyPrefix.Get<string>("garden:pots:flowers:test");
Expand All @@ -249,7 +275,7 @@ public void RemoveByKeyPatternTest(string provider)
var val14 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val15 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val16 = WithKeyPrefix.Get<string>("akey");

Assert.False(val9.HasValue);
Assert.False(val10.HasValue);
Assert.True(val11.HasValue);
Expand All @@ -259,8 +285,8 @@ public void RemoveByKeyPatternTest(string provider)
Assert.False(val15.HasValue);
Assert.False(val16.HasValue);
}
[Theory]

[Theory]
[InlineData("WithKeyPrefix")]
[InlineData("NotKeyPrefix")]
public async Task RemoveByKeyPatternAsyncTest(string provider)
Expand All @@ -284,7 +310,7 @@ public async Task RemoveByKeyPatternAsyncTest(string provider)
var val6 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val7 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val8 = WithKeyPrefix.Get<string>("akey");

Assert.True(val1.HasValue);
Assert.True(val2.HasValue);
Assert.True(val3.HasValue);
Expand All @@ -296,15 +322,15 @@ public async Task RemoveByKeyPatternAsyncTest(string provider)

// contains
await WithKeyPrefix.RemoveByPatternAsync("*:pots:*");

// postfix
await WithKeyPrefix.RemoveByPatternAsync("*foo");

// prefix
await WithKeyPrefix.RemoveByPatternAsync("sky*");
await WithKeyPrefix.RemoveByPatternAsync("sky*");

// exact
await WithKeyPrefix.RemoveByPatternAsync("akey");
await WithKeyPrefix.RemoveByPatternAsync("akey");

var val9 = WithKeyPrefix.Get<string>("garden:pots:flowers");
var val10 = WithKeyPrefix.Get<string>("garden:pots:flowers:test");
Expand All @@ -314,7 +340,7 @@ public async Task RemoveByKeyPatternAsyncTest(string provider)
var val14 = WithKeyPrefix.Get<string>("sky:birds:bar");
var val15 = WithKeyPrefix.Get<string>("sky:birds:test:bar");
var val16 = WithKeyPrefix.Get<string>("akey");

Assert.False(val9.HasValue);
Assert.False(val10.HasValue);
Assert.True(val11.HasValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ public void Evicted_Event_Should_Trigger_When_GetExpiredItems()
_provider.Get<string>(key1);
System.Threading.Thread.Sleep(500);
}

[Fact]
public async void Issues497_GetCountAsync_Check_Expires_Test()
{
for (int i = 0; i < 9; i++)
{
await _provider.SetAsync(Guid.NewGuid().ToString(), $"value-{i}", TimeSpan.FromSeconds(5));
}

Assert.Equal(9, await _provider.GetCountAsync());

await Task.Delay(5000);

Assert.Equal(0, await _provider.GetCountAsync());
}
}

public class MemoryCachingProviderWithFactoryTest : BaseCachingProviderWithFactoryTest
Expand Down