diff --git a/FastCache.InMemory/Drivers/MemoryCache.cs b/FastCache.InMemory/Drivers/MemoryCache.cs index 20b8951..8b52a77 100644 --- a/FastCache.InMemory/Drivers/MemoryCache.cs +++ b/FastCache.InMemory/Drivers/MemoryCache.cs @@ -167,13 +167,12 @@ public ConcurrentDictionary GetBuckets() 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); + _dist.AddOrUpdate(key, cacheItem, (k, v) => cacheItem); return Task.CompletedTask; } diff --git a/FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs b/FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs index 7c4adb7..17dcb30 100644 --- a/FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs +++ b/FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -9,6 +10,7 @@ using FastCache.Core.Entity; using FastCache.InMemory.Enum; using FastCache.InMemory.Extension; +using Newtonsoft.Json; namespace FastCache.InMemory.Drivers { @@ -48,8 +50,13 @@ public Task Set(string key, CacheItem cacheItem, long _ = 0) { ReleaseCached(bucket); } + + if (cacheItem.Value != null) + { + cacheItem.Value = JsonConvert.SerializeObject(cacheItem.Value); + } - bucket.TryAdd(key, cacheItem); + bucket.AddOrUpdate(key, cacheItem, (k, v) => cacheItem); return Task.CompletedTask; } @@ -64,9 +71,28 @@ public Task Get(string key) Delete(key); return Task.FromResult(new CacheItem()); } - + + if (cacheItem?.AssemblyName == null || cacheItem?.Type == null) return Task.FromResult(new CacheItem()); ++cacheItem.Hits; - return Task.FromResult(cacheItem); + object? value = null; + if (!string.IsNullOrWhiteSpace(cacheItem.Type)) + { + var assembly = Assembly.Load(cacheItem.AssemblyName); + var valueType = assembly.GetType(cacheItem.Type, true, true); + value = cacheItem.Value == null + ? null + : JsonConvert.DeserializeObject(cacheItem.Value as string, valueType); + } + + return Task.FromResult(new CacheItem() + { + CreatedAt = cacheItem.CreatedAt, + Value = value, + Expire = cacheItem.Expire, + Hits = cacheItem.Hits, + Type = cacheItem.Type, + AssemblyName = cacheItem.AssemblyName + }); } public Task Delete(string key, string prefix = "") diff --git a/UnitTests/MulitBucketsMemoryCacheTests.cs b/UnitTests/MulitBucketsMemoryCacheTests.cs index 94e6bd8..a6aef07 100644 --- a/UnitTests/MulitBucketsMemoryCacheTests.cs +++ b/UnitTests/MulitBucketsMemoryCacheTests.cs @@ -57,6 +57,8 @@ public async void TestMemoryCacheCanSet(string key, string value, string result) { await _memoryCache.Set(key, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = DateTime.UtcNow.AddSeconds(20).Ticks }); @@ -71,6 +73,8 @@ public async void TestMemoryCacheCanDelete(string key, string value, string resu { await _memoryCache.Set(key, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = DateTime.UtcNow.AddSeconds(20).Ticks }); @@ -86,6 +90,8 @@ public async void TestMemoryCacheCanDeleteByFirstPattern(string prefix, string k { await _memoryCache.Set(key, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = DateTime.UtcNow.AddSeconds(20).Ticks }); @@ -102,6 +108,8 @@ public async void TestMemoryCacheCanDeleteByFirstPatternWithPrefix(string prefix var fullKey = $"{prefix}:{key}"; await _memoryCache.Set(fullKey, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = DateTime.UtcNow.AddSeconds(20).Ticks }); @@ -118,6 +126,8 @@ public async void TestMemoryCacheCanDeleteByLastPattern(string prefix, string ke { await _memoryCache.Set(key, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = DateTime.UtcNow.AddSeconds(20).Ticks }); @@ -136,6 +146,8 @@ public async void TestMemoryCacheCanDeleteByLastPatternWithPrefix(string prefix, var fullKey = $"{prefix}:{key}"; await _memoryCache.Set(fullKey, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = DateTime.UtcNow.AddSeconds(20).Ticks }); @@ -151,6 +163,8 @@ public async void TestMemoryCacheCanExpire(string key, string value, string resu { await _memoryCache.Set(key, new CacheItem() { + Type = value.GetType().FullName, + AssemblyName = value.GetType().Assembly.FullName, Value = value, Expire = 1 });