Skip to content

Commit

Permalink
Merge pull request #14 from sj-distributor/enhance-expire-ticks
Browse files Browse the repository at this point in the history
bugfix: expire time
  • Loading branch information
pygzfei authored May 8, 2024
2 parents 2ab8683 + 4e0299d commit ae08690
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
8 changes: 4 additions & 4 deletions FastCache.Core/Attributes/CacheableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ static CacheableAttribute()
.First(p => p.Name == "FromResult" && p.ContainsGenericParameters);
}

public CacheableAttribute(string key, string expression, long expire = 0)
public CacheableAttribute(string key, string expression, long expireSeconds = 0)
{
_key = key;
_expression = expression;
_expire = expire;
_expire = expireSeconds;
Order = 2;
}

Expand Down Expand Up @@ -110,8 +110,8 @@ public override async Task Invoke(AspectContext context, AspectDelegate next)
await cacheClient.Set(key, new CacheItem

Check warning on line 110 in FastCache.Core/Attributes/CacheableAttribute.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
Value = value,
CreatedAt = DateTime.Now.Ticks,
Expire = _expire > 0 ? DateTime.Now.AddSeconds(_expire).Ticks : DateTime.Now.AddYears(1).Ticks,
CreatedAt = DateTime.UtcNow.Ticks,
Expire = _expire > 0 ? DateTime.UtcNow.AddSeconds(_expire).Ticks : DateTime.UtcNow.AddYears(1).Ticks,
AssemblyName = returnType?.Assembly?.GetName()?.FullName ?? typeof(string).Assembly.FullName,
Type = returnType?.FullName ?? string.Empty,
}, _expire);
Expand Down
2 changes: 1 addition & 1 deletion FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Task Set(string key, CacheItem cacheItem, long _ = 0)
{
ReleaseCached();
}

_dist.TryAdd(key, cacheItem);

return Task.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Task<CacheItem> Get(string key)
var bucket = GetBucket(HashKey(key));

if (!bucket.TryGetValue(key, out var cacheItem)) return Task.FromResult(new CacheItem());
if (cacheItem.Expire < DateTime.Now.Ticks)
if (cacheItem.Expire < DateTime.UtcNow.Ticks)
{
Delete(key);
return Task.FromResult(new CacheItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ static MultiSourceCacheableAttribute()
.First(p => p.Name == "FromResult" && p.ContainsGenericParameters);
}

public MultiSourceCacheableAttribute(string key, string expression, Target target, long expire = 0)
public MultiSourceCacheableAttribute(string key, string expression, Target target, long expireSeconds = 0)
{
_key = key;
_expression = expression;
_target = target;
_expire = expire;
_expire = expireSeconds;
Order = 2;
}

Expand Down Expand Up @@ -125,8 +125,8 @@ public override async Task Invoke(AspectContext context, AspectDelegate next)
await cacheClient.Set(key, new CacheItem
{
Value = value,
CreatedAt = DateTime.Now.Ticks,
Expire = _expire > 0 ? DateTime.Now.AddSeconds(_expire).Ticks : DateTime.Now.AddYears(1).Ticks,
CreatedAt = DateTime.UtcNow.Ticks,
Expire = _expire > 0 ? DateTime.UtcNow.AddSeconds(_expire).Ticks : DateTime.UtcNow.AddYears(1).Ticks,
AssemblyName = returnType.Assembly.GetName().FullName,
Type = returnType.FullName ?? string.Empty,
}, _expire);
Expand Down
12 changes: 6 additions & 6 deletions IntegrationTests/MultiSourceApiRequestCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ public MultiSourceApiRequestCacheTests(WebApplicationFactory<Program> factory)
[InlineData("/MultiSourceInMemory")]
public async void RequestCanCache(string baseUrl)
{
var start = DateTime.Now.Ticks;
var start = DateTime.UtcNow.Ticks;
var resp1 = await _httpClient.GetAsync($"{baseUrl}/?id=1");
Assert.True(resp1.StatusCode == HttpStatusCode.OK);
await resp1.Content.ReadAsStringAsync();
var end = DateTime.Now.Ticks;
var end = DateTime.UtcNow.Ticks;

Assert.True(end - start > 1000000);

var start1 = DateTime.Now.Ticks;
var start1 = DateTime.UtcNow.Ticks;
var resp2 = await _httpClient.GetAsync($"{baseUrl}?id=1");
Assert.True(resp2.StatusCode == HttpStatusCode.OK);
await resp2.Content.ReadAsStringAsync();
var end1 = DateTime.Now.Ticks;
var end1 = DateTime.UtcNow.Ticks;

var result = end1 - start1;
Assert.True(result < 400000);
Expand Down Expand Up @@ -125,12 +125,12 @@ public async void CacheAndEvictOther(string baseUrl)

var result3 = await resp3.Content.ReadAsStringAsync();

var start = DateTime.Now.Ticks;
var start = DateTime.UtcNow.Ticks;
var resp4 = await _httpClient.GetAsync($"{baseUrl}/users?page=1");
Assert.True(resp4.StatusCode == HttpStatusCode.OK);

var result4 = await resp4.Content.ReadAsStringAsync();
var end = DateTime.Now.Ticks;
var end = DateTime.UtcNow.Ticks;

Assert.NotEqual(result1, result3);
Assert.Equal(result3, result4);
Expand Down
12 changes: 6 additions & 6 deletions IntegrationTests/UserApiRequestCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ public UserApiRequestCacheTests(WebApplicationFactory<Program> factory)
[Fact]
public async void RequestCanCache()
{
var start = DateTime.Now.Ticks;
var start = DateTime.UtcNow.Ticks;
var resp1 = await _httpClient.GetAsync("/user?id=1");
Assert.True(resp1.StatusCode == HttpStatusCode.OK);

await resp1.Content.ReadAsStringAsync();
var end = DateTime.Now.Ticks;
var end = DateTime.UtcNow.Ticks;

Assert.True(end - start > 1000000);

var start1 = DateTime.Now.Ticks;
var start1 = DateTime.UtcNow.Ticks;
var resp2 = await _httpClient.GetAsync("/user?id=1");
Assert.True(resp2.StatusCode == HttpStatusCode.OK);

await resp2.Content.ReadAsStringAsync();
var end1 = DateTime.Now.Ticks;
var end1 = DateTime.UtcNow.Ticks;

var result = end1 - start1;
Assert.True(result < 400000);
Expand Down Expand Up @@ -123,12 +123,12 @@ public async void CacheAndEvictOther()

var result3 = await resp3.Content.ReadAsStringAsync();

var start = DateTime.Now.Ticks;
var start = DateTime.UtcNow.Ticks;
var resp4 = await _httpClient.GetAsync("/user/users?page=1");
Assert.True(resp4.StatusCode == HttpStatusCode.OK);

var result4 = await resp4.Content.ReadAsStringAsync();
var end = DateTime.Now.Ticks;
var end = DateTime.UtcNow.Ticks;

Assert.NotEqual(result1, result3);
Assert.Equal(result3, result4);
Expand Down
24 changes: 12 additions & 12 deletions UnitTests/MemoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void TestWhenTheMemoryIsFull_EliminatedSuccess(MaxMemoryPolicy maxMemoryP
memoryCache.Set($"{i}", new CacheItem()
{
Value = i,
CreatedAt = DateTime.Now.AddSeconds(i).Ticks,
Expire = DateTime.Now.AddHours(20).Ticks,
CreatedAt = DateTime.UtcNow.AddSeconds(i).Ticks,
Expire = DateTime.UtcNow.AddHours(20).Ticks,
Hits = (ulong)i + 1
});
for (var j = 0; j < i; j++)
Expand All @@ -58,7 +58,7 @@ public async void TestMemoryCacheCanSet(string key, string value, string result)
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
var s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);
Expand All @@ -72,7 +72,7 @@ public async void TestMemoryCacheCanDelete(string key, string value, string resu
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(key);
var s = await _memoryCache.Get(key);
Expand All @@ -88,7 +88,7 @@ public async void TestMemoryCacheCanDeleteByPattern(string prefix, string key, s
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete("anson*", prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -105,7 +105,7 @@ public async void TestMemoryCacheCanDeleteByPatternWithPrefix(string prefix, str
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete("anson*", prefix);
var s = await _memoryCache.Get(key);
Expand Down Expand Up @@ -137,7 +137,7 @@ public async void TestMemoryCacheCanDeleteAsterisk(string prefix, string key, st
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(deleteKey, prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -156,7 +156,7 @@ public async void TestMemoryCacheCanDeleteAsteriskWithPrefix(string prefix, stri
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(deleteKey, prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -174,7 +174,7 @@ public async void TestMemoryCacheCanDeleteByGeneralMatchAsterisk(string prefix,
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(deleteKey, prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -194,7 +194,7 @@ public async void TestMemoryCacheCanDeleteByGeneralMatchAsteriskWithPrefix(strin
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(deleteKey, prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -214,7 +214,7 @@ public async void TestMemoryCacheDoubleDelete(string prefix, string key,
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(deleteKey, prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -223,7 +223,7 @@ public async void TestMemoryCacheDoubleDelete(string prefix, string key,
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});

await Task.Delay(TimeSpan.FromSeconds(4));
Expand Down
16 changes: 8 additions & 8 deletions UnitTests/MulitBucketsMemoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void TestWhenTheMemoryIsFull_EliminatedSuccess(MaxMemoryPolicy maxMemoryP
memoryCache.Set($"{i}", new CacheItem()
{
Value = i,
CreatedAt = DateTime.Now.AddSeconds(i).Ticks,
Expire = DateTime.Now.AddHours(20).Ticks,
CreatedAt = DateTime.UtcNow.AddSeconds(i).Ticks,
Expire = DateTime.UtcNow.AddHours(20).Ticks,
Hits = (ulong)i + 1
});
for (var j = 0; j < i; j++)
Expand Down Expand Up @@ -58,7 +58,7 @@ public async void TestMemoryCacheCanSet(string key, string value, string result)
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
var s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);
Expand All @@ -72,7 +72,7 @@ public async void TestMemoryCacheCanDelete(string key, string value, string resu
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(key);
var s = await _memoryCache.Get(key);
Expand All @@ -87,7 +87,7 @@ public async void TestMemoryCacheCanDeleteByFirstPattern(string prefix, string k
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete("anson*", prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -103,7 +103,7 @@ public async void TestMemoryCacheCanDeleteByFirstPatternWithPrefix(string prefix
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete("anson*", prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -119,7 +119,7 @@ public async void TestMemoryCacheCanDeleteByLastPattern(string prefix, string ke
await _memoryCache.Set(key, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete("*Joe", prefix);
var s = await _memoryCache.Get(key);
Expand All @@ -137,7 +137,7 @@ public async void TestMemoryCacheCanDeleteByLastPatternWithPrefix(string prefix,
await _memoryCache.Set(fullKey, new CacheItem()
{
Value = value,
Expire = DateTime.Now.AddSeconds(20).Ticks
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete("*Joe", prefix);
var s = await _memoryCache.Get(key);
Expand Down

0 comments on commit ae08690

Please sign in to comment.