Skip to content

Commit

Permalink
Merge pull request #21 from sj-distributor/Enhancement-Can-update-mem…
Browse files Browse the repository at this point in the history
…ory-cache

Enhancement: Can update memory cache
  • Loading branch information
SlothRemige authored Jun 5, 2024
2 parents 8c0c8f0 + 02754e8 commit 2d40751
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 1 addition & 2 deletions FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,12 @@ public ConcurrentDictionary<string, CacheItem> 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;
}
Expand Down
32 changes: 29 additions & 3 deletions FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
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;
using FastCache.Core.Driver;
using FastCache.Core.Entity;
using FastCache.InMemory.Enum;
using FastCache.InMemory.Extension;
using Newtonsoft.Json;

namespace FastCache.InMemory.Drivers
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -64,9 +71,28 @@ public Task<CacheItem> 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);

Check warning on line 84 in FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'object? JsonConvert.DeserializeObject(string value, Type type)'.

Check warning on line 84 in FastCache.InMemory/Drivers/MultiBucketsMemoryCache.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'object? JsonConvert.DeserializeObject(string value, Type type)'.
}

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 = "")
Expand Down
14 changes: 14 additions & 0 deletions UnitTests/MulitBucketsMemoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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
});
Expand All @@ -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
});
Expand All @@ -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
});
Expand All @@ -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
});
Expand All @@ -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
});
Expand All @@ -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
});
Expand Down

0 comments on commit 2d40751

Please sign in to comment.