Skip to content

Commit

Permalink
Merge pull request #18 from sj-distributor/enhance-memory-serialize
Browse files Browse the repository at this point in the history
enhance
  • Loading branch information
SlothRemige authored May 27, 2024
2 parents 8cf6655 + 8f0fddb commit 2f6fa17
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
7 changes: 6 additions & 1 deletion FastCache.Core/Driver/IMemoryCache.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Threading.Tasks;
using FastCache.Core.Entity;

namespace FastCache.Core.Driver
{
public interface IMemoryCache : ICacheClient
{

Task SetValue(string key, CacheItem cacheItem, long _ = 0);
Task<CacheItem> GetValue(string key);
}
}
28 changes: 28 additions & 0 deletions FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,33 @@ public ConcurrentDictionary<string, CacheItem> GetBuckets()
{
return _dist;
}

public Task SetValue(string key, CacheItem cacheItem, long _ = 0)
{
if (_dist.ContainsKey(key)) return Task.CompletedTask;
if (_dist.Count >= _maxCapacity)
{
ReleaseCached();
}

_dist.TryAdd(key, cacheItem);

return Task.CompletedTask;
}

public Task<CacheItem> GetValue(string key)
{
if (!_dist.TryGetValue(key, out var cacheItem)) return Task.FromResult(new CacheItem());

if (cacheItem.Expire < DateTime.UtcNow.Ticks)
{
Delete(key);
return Task.FromResult(new CacheItem());
}

++cacheItem.Hits;

return Task.FromResult(cacheItem);
}
}
}
11 changes: 11 additions & 0 deletions FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,16 @@ private uint HashKey(string key)
{
return BitConverter.ToUInt32(SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key)), 0) % _buckets;
}

public Task SetValue(string key, CacheItem cacheItem, long _ = 0)
{
Set(key, cacheItem);
return Task.CompletedTask;
}

public Task<CacheItem> GetValue(string key)
{
return Get(key);
}
}
}
17 changes: 13 additions & 4 deletions UnitTests/MemoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async void TestMemoryCacheCanSet(string key, string value, string result)
{
await _memoryCache.Set(key, new CacheItem()
{
Type =value.GetType().FullName,
Type = value.GetType().FullName,
AssemblyName = value.GetType().Assembly.FullName,
Value = value,
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
Expand All @@ -80,6 +80,15 @@ public async void TestMemoryCacheCanDelete(string key, string value, string resu
await _memoryCache.Delete(key);
var s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);

await _memoryCache.SetValue(key, new CacheItem()
{
Value = value,
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});
await _memoryCache.Delete(key);
s = await _memoryCache.GetValue(key);
Assert.Equal(s.Value, result);
}


Expand Down Expand Up @@ -203,7 +212,7 @@ public async void TestMemoryCacheCanDeleteByGeneralMatchAsteriskWithPrefix(strin
var s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);
}

[Theory]
[InlineData("anson", "ansonExpire333", "*", "18", null)]
[InlineData("anson", "ansonExpire444", "*", "19", null)]
Expand All @@ -228,9 +237,9 @@ public async void TestMemoryCacheDoubleDelete(string prefix, string key,
Value = value,
Expire = DateTime.UtcNow.AddSeconds(20).Ticks
});

await Task.Delay(TimeSpan.FromSeconds(4));

s = await _memoryCache.Get(key);
Assert.Equal(s.Value, result);
}
Expand Down

0 comments on commit 2f6fa17

Please sign in to comment.