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

enhance #19

Merged
merged 1 commit into from
May 27, 2024
Merged
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
28 changes: 19 additions & 9 deletions FastCache.InMemory/Drivers/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
{
cacheItem.Value = JsonConvert.SerializeObject(cacheItem.Value);
}

_dist.AddOrUpdate(key, cacheItem, (k, v) => cacheItem);

return Task.CompletedTask;
Expand All @@ -57,14 +58,23 @@
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);
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 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()
{
CreatedAt = cacheItem.CreatedAt,
Value = cacheItem.Value == null ? null : JsonConvert.DeserializeObject(cacheItem.Value as string, valueType),
Value = value,
Expire = cacheItem.Expire,
Hits = cacheItem.Hits,
Type = cacheItem.Type,
Expand Down Expand Up @@ -162,24 +172,24 @@
{
ReleaseCached();
}

_dist.TryAdd(key, cacheItem);

return Task.CompletedTask;
}

public Task<CacheItem> GetValue(string key)
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);
}
}
Expand Down
Loading