Skip to content

Commit

Permalink
Merge pull request #16 from sj-distributor/enhance-memory-serialize
Browse files Browse the repository at this point in the history
enhance memory serialize
  • Loading branch information
SlothRemige authored May 27, 2024
2 parents ae08690 + 0a57126 commit e9f40cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
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 @@ -36,7 +38,8 @@ public Task Set(string key, CacheItem cacheItem, long _ = 0)
{
ReleaseCached();
}


cacheItem.Value = JsonConvert.SerializeObject(cacheItem.Value);
_dist.TryAdd(key, cacheItem);

return Task.CompletedTask;
Expand All @@ -51,9 +54,11 @@ 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;

var assembly = Assembly.Load(cacheItem.AssemblyName);
var valueType = assembly.GetType(cacheItem.Type, true, true);
cacheItem.Value = JsonConvert.DeserializeObject(cacheItem.Value as string, valueType);

Check warning on line 61 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(cacheItem);
}

Expand Down
2 changes: 2 additions & 0 deletions UnitTests/MemoryCacheTests.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 Down

0 comments on commit e9f40cb

Please sign in to comment.