2025-02-20, 03:24 PM
As mentioned, I pulled the Source code down. I am still running v 10.10 so this is the .Net 8 build. The LibraryManager.cs code is basically the same.
The subtle changes I have made are simple as a proof of concept. Adding a TTL to the Cache in LibraryManager.
here is my implementation :
Added this to the constructor:
Change the model being stored in Cach to :
Added a Cleanup to purge Expired entries :
An example use for get item :
I have also added some general logging in areas I felt needed a little boost.
So far my server is staying under 1 gig of ram. but I am going to let it keep running. I watch it bump when scans run and then settle back down a couple hours later. I have noticed no real performance issues as I thought it could take a small hit. But if you are using it. then the cache is saturated. If anything I think the overall performance is better especially on my resource starved backup server.
I will keep you updated as we could have other areas that could use a bump.
The subtle changes I have made are simple as a proof of concept. Adding a TTL to the Cache in LibraryManager.
here is my implementation :
Code:
private const string ShortcutFileExtension = ".mblink";
private static readonly TimeSpan DefaultTTL = TimeSpan.FromHours(2);
private static readonly TimeSpan CleanupInterval = TimeSpan.FromHours(1);
private readonly Timer _cleanupTimer;
Added this to the constructor:
Code:
_cache = new ConcurrentDictionary<Guid, CacheEntry>();
_cleanupTimer = new Timer(state => PurgeExpiredEntries(), null, CleanupInterval, CleanupInterval);
Change the model being stored in Cach to :
Code:
private class CacheEntry
{
public CacheEntry(BaseItem item, DateTime expiration)
{
Item = item;
Expiration = expiration;
}
public BaseItem Item { get; set; }
public DateTime Expiration { get; set; }
}
Added a Cleanup to purge Expired entries :
Code:
private void PurgeExpiredEntries()
{
var purgeCount = 0;
foreach (var kv in _cache)
{
if (kv.Value.Expiration <= DateTime.UtcNow)
{
_cache.TryRemove(kv.Key, out _);
purgeCount++;
}
}
_lastCleanup = DateTime.UtcNow;
_logger.LogInformation("Purged {Count} expired entries from the library cache", purgeCount);
_logger.LogInformation("Library cache contains {Count} entries", _cache.Count);
}
An example use for get item :
Code:
public BaseItem? GetItemById(Guid id)
{
if (id.IsEmpty())
{
throw new ArgumentException("Guid can't be empty", nameof(id));
}
if (_cache.TryGetValue(id, out var entry))
{
if (entry.Expiration <= DateTime.UtcNow)
{
_cache.TryRemove(id, out _);
}
else
{
if ((DateTime.UtcNow - _lastCleanup) > CleanupInterval)
{
PurgeExpiredEntries();
}
return entry.Item;
}
}
var item = RetrieveItem(id);
if (item is not null)
{
RegisterItem(item);
}
return item;
}
I have also added some general logging in areas I felt needed a little boost.
So far my server is staying under 1 gig of ram. but I am going to let it keep running. I watch it bump when scans run and then settle back down a couple hours later. I have noticed no real performance issues as I thought it could take a small hit. But if you are using it. then the cache is saturated. If anything I think the overall performance is better especially on my resource starved backup server.
I will keep you updated as we could have other areas that could use a bump.