-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3897 from sbwalker/dev
improve caching for static rendering
- Loading branch information
Showing
3 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Oqtane.Extensions | ||
{ | ||
public static class CacheExtensions | ||
{ | ||
private static string _cachekeys = "cachekeys"; | ||
|
||
public static TItem GetOrCreate<TItem>(this IMemoryCache cache, string key, Func<ICacheEntry, TItem> factory, bool track) | ||
{ | ||
if (!cache.TryGetValue(key, out object result)) | ||
{ | ||
using ICacheEntry entry = cache.CreateEntry(key); | ||
result = factory(entry); | ||
entry.Value = result; | ||
|
||
if (track) | ||
{ | ||
// track the cache key | ||
List<string> cachekeys; | ||
if (!cache.TryGetValue(_cachekeys, out cachekeys)) | ||
{ | ||
cachekeys = new List<string>(); | ||
} | ||
if (!cachekeys.Contains(key)) | ||
{ | ||
cachekeys.Add(key); | ||
cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove }); | ||
} | ||
} | ||
} | ||
|
||
return (TItem)result; | ||
} | ||
|
||
public static void Remove(this IMemoryCache cache, string key, bool track) | ||
{ | ||
List<string> cachekeys; | ||
|
||
if (track && key.EndsWith("*")) | ||
{ | ||
// wildcard cache key removal | ||
var prefix = key.Substring(0, key.Length - 1); | ||
if (cache.TryGetValue(_cachekeys, out cachekeys) && cachekeys.Any()) | ||
{ | ||
for (var i = cachekeys.Count - 1; i >= 0; i--) | ||
{ | ||
if (cachekeys[i].StartsWith(prefix)) | ||
{ | ||
cache.Remove(cachekeys[i]); | ||
cachekeys.RemoveAt(i); | ||
} | ||
} | ||
cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove }); | ||
} | ||
} | ||
else | ||
{ | ||
cache.Remove(key); | ||
} | ||
|
||
// reconcile all tracked cache keys | ||
if (track && cache.TryGetValue(_cachekeys, out cachekeys) && cachekeys.Any()) | ||
{ | ||
for (var i = cachekeys.Count - 1; i >= 0; i--) | ||
{ | ||
if (!cache.TryGetValue(cachekeys[i], out _)) | ||
{ | ||
cachekeys.RemoveAt(i); | ||
} | ||
} | ||
cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters