Skip to content
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

Enhancement: Can update memory cache #21

Merged
merged 1 commit into from
Jun 5, 2024
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
3 changes: 1 addition & 2 deletions FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
var valueType = assembly.GetType(cacheItem.Type, true, true);
value = cacheItem.Value == null
? null
: JsonConvert.DeserializeObject(cacheItem.Value as string, valueType);

Check warning on line 71 in FastCache.InMemory/Drivers/MemoryCache.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()
Expand Down Expand Up @@ -167,13 +167,12 @@

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 @@
{
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 @@
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)'.
}

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
Loading