Skip to content

Commit

Permalink
added ExecuteFactory
Browse files Browse the repository at this point in the history
added ProcessExpiredEviction
  • Loading branch information
JoelNygren-Norce committed Nov 20, 2024
1 parent 7cb7a15 commit 6c850df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion HzMemoryCache/Diagnostics/Activities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public static class Names
public const string RemoveByPattern = "remove by pattern";
public const string RemoveItem = "remove item";
public const string Clear = "clear";
public const string ExecuteFactory = "execute factory";

public const string NotifyItemChange = "notify item change";
public const string Subscribe = "subscribe";
public const string ValueChanged = "value changed";
public const string EvictExpired = "evict expired";
public const string GetStatistics = "get statistics";

public const string ProcessExpiredEviction = "process expired eviction";
}

public static class Project
Expand Down
18 changes: 17 additions & 1 deletion HzMemoryCache/HzMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public void RemoveByPattern(string pattern, bool sendNotification = true)

public void EvictExpired()
{
using var activity = Activities.Source.StartActivityWithCommonTags(Activities.Names.EvictExpired, Activities.Project.HzMemoryCache);

if (Monitor.TryEnter(cleanUpTimer)) //use the timer-object for our lock, it's local, private and instance-type, so its ok
{
try
Expand Down Expand Up @@ -102,6 +104,8 @@ public void Clear()
/// </summary>
public T? Get<T>(string key)
{
using var activity = Activities.Source.StartActivityWithCommonTags(Activities.Names.Get, Activities.Project.HzMemoryCache);

var defaultValue = default(T);

if (!dictionary.TryGetValue(key, out var ttlValue))
Expand Down Expand Up @@ -140,6 +144,8 @@ public void Set<T>(string key, T? value)
/// </summary>
public void Set<T>(string key, T? value, TimeSpan ttl)
{
using var activity = Activities.Source.StartActivityWithCommonTags(Activities.Names.Set, Activities.Project.HzMemoryCache);

var v = new TTLValue(key, value, ttl, updateChecksumAndSerializeQueue, options.notificationType,
(tv, objectData) => NotifyItemChange(key, CacheItemChangeType.AddOrUpdate, tv, objectData));
dictionary[key] = v;
Expand Down Expand Up @@ -168,9 +174,16 @@ public void Set<T>(string key, T? value, TimeSpan ttl)
throw new Exception($"Could not acquire lock for key {key}");
}


try
{
value = valueFactory(key);
using (var executeActivity =
Activities.Source.StartActivityWithCommonTags(Activities.Names.ExecuteFactory,
Activities.Project.HzMemoryCache, key: key))
{
value = valueFactory(key);
}

var ttlValue = new TTLValue(key, value, ttl, updateChecksumAndSerializeQueue, options.notificationType, (tv, objectData) =>
{
NotifyItemChange(key, CacheItemChangeType.AddOrUpdate, tv, objectData);
Expand Down Expand Up @@ -307,6 +320,7 @@ private void StartUpdateChecksumAndNotify()

private async Task ProcessExpiredEviction()
{
using var activity = Activities.Source.StartActivityWithCommonTags(Activities.Names.ProcessExpiredEviction, Activities.Project.HzMemoryCache);
await globalStaticLock.WaitAsync().ConfigureAwait(false);
try
{
Expand All @@ -317,6 +331,8 @@ private async Task ProcessExpiredEviction()

private bool RemoveItem(string key, CacheItemChangeType changeType, bool sendNotification, Func<string, bool>? areEqualFunc = null)
{
using var activity = Activities.Source.StartActivityWithCommonTags(Activities.Names.RemoveItem, Activities.Project.HzMemoryCache, key: key);

var result = !(!dictionary.TryGetValue(key, out TTLValue ttlValue) || (areEqualFunc != null && areEqualFunc.Invoke(ttlValue.checksum)));

if (result)
Expand Down
9 changes: 8 additions & 1 deletion HzMemoryCache/HzMemoryCacheAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ public async Task<IList<T>> GetOrSetBatchAsync<T>(IList<string> keys, Func<IList

var cachedItems = keys.Select(key => new KeyValuePair<string, T?>(key, Get<T>(key)));
var missingKeys = cachedItems.Where(kvp => IsNullOrDefault(kvp.Value)).Select(kvp => kvp.Key).ToList();
var factoryRetrievedItems = (await valueFactory(missingKeys)).ToDictionary(kv => kv.Key, kv => kv.Value);
Dictionary<string, T> factoryRetrievedItems;
using (var executeActivity =
Activities.Source.StartActivityWithCommonTags(Activities.Names.ExecuteFactory,
Activities.Project.HzMemoryCache, key: string.Join(",", missingKeys ?? new List<string>())))
{
factoryRetrievedItems =
(await valueFactory(missingKeys)).ToDictionary(kv => kv.Key, kv => kv.Value);
}

return cachedItems.Select(kv =>
{
Expand Down

0 comments on commit 6c850df

Please sign in to comment.