Skip to content

Commit

Permalink
clean up memory cache service - new API
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Oct 22, 2024
1 parent 970cdeb commit 9e6f820
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 79 deletions.
8 changes: 6 additions & 2 deletions ToSic.Eav.Apps/Eav.Apps.Internal/AppJson/AppJsonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ public AppJson GetAppJson(int appId, bool useShared = false)

appJson = GetAppJsonInternal(pathToAppJson);
if (appJson != null)
memoryCacheService.Set(cacheKey, appJson, filePaths: [pathToAppJson]); // cache appJson
memoryCacheService.SetNew(cacheKey, appJson, p => p.WatchFiles([pathToAppJson])); // cache appJson
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//memoryCacheService.Set(cacheKey, appJson, filePaths: [pathToAppJson]); // cache appJson
else
memoryCacheService.Set(cacheKey, new AppJson(), folderPaths: GetExistingParent(pathToAppJson).ToDictionary(p => p, p => true)); // cache null
memoryCacheService.SetNew(cacheKey, new AppJson(), p => p.WatchFolders(GetExistingParent(pathToAppJson).ToDictionary(p => p, _ => true))); // cache null
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//memoryCacheService.Set(cacheKey, new AppJson(), folderPaths: GetExistingParent(pathToAppJson).ToDictionary(p => p, p => true)); // cache null

return l.ReturnAsOk(appJson);
}
Expand Down
3 changes: 3 additions & 0 deletions ToSic.Eav.Core/Caching/CacheItemPolicyMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public IPolicyMaker SetSlidingExpiration(TimeSpan slidingExpiration)
p => p.SlidingExpiration = slidingExpiration
);

public IPolicyMaker SetSlidingExpiration(int seconds)
=> SetSlidingExpiration(new TimeSpan(0, 0, seconds));

public IPolicyMaker WatchFiles(IList<string> filePaths) =>
filePaths is not { Count: > 0 }
? this
Expand Down
10 changes: 9 additions & 1 deletion ToSic.Eav.Core/Caching/IPolicyMaker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.Caching;
using ToSic.Eav.Apps.State;

namespace ToSic.Eav.Caching;

Expand All @@ -8,6 +7,15 @@ public interface IPolicyMaker
CacheItemPolicy CreateResult();
IPolicyMaker SetAbsoluteExpiration(DateTimeOffset absoluteExpiration);
IPolicyMaker SetSlidingExpiration(TimeSpan slidingExpiration);

/// <summary>
/// Set sliding expiration in seconds.
/// Numbers bigger than 60 are fine, as the number will be converted to ticks.
/// </summary>
/// <param name="seconds"></param>
/// <returns>A new PolicyMaker with the updated expiration</returns>
IPolicyMaker SetSlidingExpiration(int seconds);

IPolicyMaker WatchFiles(IList<string> filePaths);
IPolicyMaker WatchFolders(IDictionary<string, bool> folderPaths);
IPolicyMaker WatchCacheKeys(IEnumerable<string> cacheKeys);
Expand Down
105 changes: 53 additions & 52 deletions ToSic.Eav.Core/Caching/MemoryCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,58 +84,59 @@ public void SetNew(string key, object value, IPolicyMaker policyMaker)
}
}

public void Set(string key, object value,
NoParamOrder protector = default,
DateTimeOffset? absoluteExpiration = null,
TimeSpan? slidingExpiration = null,
IList<string> filePaths = null,
IDictionary<string, bool> folderPaths = null,
IEnumerable<string> cacheKeys = null,
CacheEntryUpdateCallback updateCallback = null)
{
var l = Log.Fn($"key: '{key}'");
try
{
CacheItemPolicy policy = absoluteExpiration.HasValue
? new() { AbsoluteExpiration = absoluteExpiration.Value }
: new() { SlidingExpiration = slidingExpiration ?? DefaultSlidingExpiration };

if (filePaths is { Count: > 0 })
{
l.A($"Add {filePaths.Count} {nameof(HostFileChangeMonitor)}s");
policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
}

if (folderPaths is { Count: > 0 })
{
l.A($"Add {folderPaths.Count} {nameof(FolderChangeMonitor)}s");
policy.ChangeMonitors.Add(new FolderChangeMonitor(folderPaths));
}

if (cacheKeys != null)
{
var keysClone = new List<string>(cacheKeys);
if (keysClone.Count > 0)
{
l.A($"Add {keysClone.Count} Cache-Entry Change Monitors");
policy.ChangeMonitors.Add(CreateCacheEntryChangeMonitor(keysClone));
}
}

if (updateCallback != null)
{
l.A("Add UpdateCallback");
policy.UpdateCallback = updateCallback;
}

Cache.Set(new(key, value), policy);
l.Done();
}
catch (Exception ex)
{
l.Done(ex);
}
}
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//public void Set(string key, object value,
// NoParamOrder protector = default,
// DateTimeOffset? absoluteExpiration = null,
// TimeSpan? slidingExpiration = null,
// IList<string> filePaths = null,
// IDictionary<string, bool> folderPaths = null,
// IEnumerable<string> cacheKeys = null,
// CacheEntryUpdateCallback updateCallback = null)
//{
// var l = Log.Fn($"key: '{key}'");
// try
// {
// CacheItemPolicy policy = absoluteExpiration.HasValue
// ? new() { AbsoluteExpiration = absoluteExpiration.Value }
// : new() { SlidingExpiration = slidingExpiration ?? DefaultSlidingExpiration };

// if (filePaths is { Count: > 0 })
// {
// l.A($"Add {filePaths.Count} {nameof(HostFileChangeMonitor)}s");
// policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
// }

// if (folderPaths is { Count: > 0 })
// {
// l.A($"Add {folderPaths.Count} {nameof(FolderChangeMonitor)}s");
// policy.ChangeMonitors.Add(new FolderChangeMonitor(folderPaths));
// }

// if (cacheKeys != null)
// {
// var keysClone = new List<string>(cacheKeys);
// if (keysClone.Count > 0)
// {
// l.A($"Add {keysClone.Count} Cache-Entry Change Monitors");
// policy.ChangeMonitors.Add(CreateCacheEntryChangeMonitor(keysClone));
// }
// }

// if (updateCallback != null)
// {
// l.A("Add UpdateCallback");
// policy.UpdateCallback = updateCallback;
// }

// Cache.Set(new(key, value), policy);
// l.Done();
// }
// catch (Exception ex)
// {
// l.Done(ex);
// }
//}

//public bool Add(string key, object value, CacheItemPolicy policy) => Cache.Add(key, value, policy);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.Caching;
using ToSic.Eav.Caching;
using ToSic.Eav.Caching;

namespace ToSic.Eav.DataSource.Internal.Caching;

Expand All @@ -15,7 +14,6 @@ public class ListCacheItem: ICacheExpiring
/// </summary>
public IImmutableList<IEntity> List { get; set; }


/// <summary>
/// The age of the data - to see if it needs refreshing if the new source has a newer date
/// </summary>
Expand All @@ -26,21 +24,25 @@ public class ListCacheItem: ICacheExpiring

public bool RefreshOnSourceRefresh { get; }

public CacheItemPolicy Policy { get; }
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
// don't think it's in use any where
//public CacheItemPolicy Policy { get; }

/// <summary>
/// Initialize the object - ready to cache
/// </summary>
/// <param name="list">The list of items to put into the cache.</param>
/// <param name="cacheTimestamp">The timestamp of the source at the moment of cache-buildup, to later detect changes in the source.</param>
/// <param name="refreshOnSourceRefresh"></param>
/// <param name="policy"></param>
///// <param name="cacheKey">The cache key as it is stored in the cache</param>
public ListCacheItem(IImmutableList<IEntity> list, long cacheTimestamp, bool refreshOnSourceRefresh, CacheItemPolicy policy)
public ListCacheItem(IImmutableList<IEntity> list, long cacheTimestamp, bool refreshOnSourceRefresh)
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//public ListCacheItem(IImmutableList<IEntity> list, long cacheTimestamp, bool refreshOnSourceRefresh, CacheItemPolicy policy)
{
List = list;
CacheTimestamp = cacheTimestamp;
RefreshOnSourceRefresh = refreshOnSourceRefresh;
Policy = policy;
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//Policy = policy;
}
}
31 changes: 18 additions & 13 deletions ToSic.Eav.DataSources/DataSource/Internal/Caching/ListCacheSvc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.Caching;
using ToSic.Eav.Caching;
using ToSic.Eav.Caching;
using IEntity = ToSic.Eav.Data.IEntity;

namespace ToSic.Eav.DataSource.Internal.Caching;
Expand Down Expand Up @@ -110,17 +109,23 @@ public void Set(string key, IImmutableList<IEntity> list, long sourceTimestamp,
{
var l = Log.Fn($"key: {key}; sourceTime: {sourceTimestamp}; duration:{durationInSeconds}; sliding: {slidingExpiration}");
var duration = durationInSeconds > 0 ? durationInSeconds : DefaultDuration;
var expiration = new TimeSpan(0, 0, duration);
var absoluteExpiration = DateTime.Now.AddSeconds(duration);
var policy = slidingExpiration
? new CacheItemPolicy { SlidingExpiration = expiration }
: new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration };

// TOD: THIS LOOKS FISHY! Why is the policy added in a way 2x?
memoryCacheService.Set(key,
value: new ListCacheItem(list, sourceTimestamp, refreshOnSourceRefresh, policy),
absoluteExpiration: slidingExpiration ? null : absoluteExpiration,
slidingExpiration: slidingExpiration ? expiration : null);
//var expiration = new TimeSpan(0, 0, duration);
//var absoluteExpiration = DateTime.Now.AddSeconds(duration);
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//var policy = slidingExpiration
// ? new CacheItemPolicy { SlidingExpiration = expiration }
// : new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration };

// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
memoryCacheService.SetNew(key, value: new ListCacheItem(list, sourceTimestamp, refreshOnSourceRefresh), p => slidingExpiration
? p.SetSlidingExpiration(duration)
: p.SetAbsoluteExpiration(DateTime.Now.AddSeconds(duration)));
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
// TODO: THIS LOOKS FISHY! Why is the policy added in a way 2x?
//memoryCacheService.Set(key,
// value: new ListCacheItem(list, sourceTimestamp, refreshOnSourceRefresh, policy),
// absoluteExpiration: slidingExpiration ? null : absoluteExpiration,
// slidingExpiration: slidingExpiration ? expiration : null);
l.Done();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public List<DataSourceInfo> Get(int appId)
var (data, slidingExpiration, folderPaths, cacheKeys)
= appDataSourcesLoader.Value.CompileDynamicDataSources(appId);

memoryCacheService.Set(AppCacheKey(appId), data,
slidingExpiration: slidingExpiration,
folderPaths: folderPaths?.ToDictionary(p => p, p => true),
cacheKeys: cacheKeys);
memoryCacheService.SetNew(AppCacheKey(appId), data, p => p
.SetSlidingExpiration(slidingExpiration)
.WatchFolders(folderPaths?.ToDictionary(p => p, p => true))
.WatchCacheKeys(cacheKeys));
// Ported 2024-10-22 - remove old code ca. 2024-12 #MemoryCacheApiCleanUp
//memoryCacheService.Set(AppCacheKey(appId), data,
// slidingExpiration: slidingExpiration,
// folderPaths: folderPaths?.ToDictionary(p => p, p => true),
// cacheKeys: cacheKeys);

return data;
}
Expand Down

0 comments on commit 9e6f820

Please sign in to comment.