This commit is contained in:
Zel
2025-02-23 18:47:21 +08:00
parent eaaffeeccb
commit e46a7ca31c
104 changed files with 2630 additions and 2516 deletions

View File

@@ -4,6 +4,9 @@ using System.Runtime.Caching;
namespace ZelWiki.Caching
{
/// <summary>
/// 缓存
/// </summary>
public class WikiCache
{
public enum Category
@@ -24,7 +27,7 @@ namespace ZelWiki.Caching
public static int CacheItemCount => MemCache.Count();
public static double CacheMemoryLimit => MemCache.CacheMemoryLimit;
public static MemoryCache MemCache => _memCache ?? throw new Exception("Cache has not been initialized.");
public static MemoryCache MemCache => _memCache ?? throw new Exception("缓存尚未初始化");
public static void Initialize(int cacheMemoryLimitMB, int defaultCacheSeconds)
{
@@ -40,7 +43,7 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Gets an item from the cache.
/// 获取缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
@@ -60,9 +63,8 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Determines if the cache contains a given key.
/// 确定缓存是否包含给定的key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool Contains(IWikiCacheKey cacheKey)
@@ -79,7 +81,7 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Gets an item from the cache.
/// 获取缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheKey"></param>
@@ -98,7 +100,7 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Adds an item to the cache. If the item is already in the cache, this will reset its expiration.
///添加缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="value"></param>
@@ -127,7 +129,7 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Removes all entries from the cache.
/// 清理
/// </summary>
public static void Clear()
{
@@ -144,9 +146,9 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Removes cache entries that begin with the given cache key.
/// 删除某个
/// </summary>
/// <param name="category"></param>
/// <param name="cacheKey"></param>
public static void ClearCategory(WikiCacheKey cacheKey)
{
var keys = new List<string>();
@@ -163,7 +165,7 @@ namespace ZelWiki.Caching
}
/// <summary>
/// Removes cache entries in a given category.
/// 删除给定类别中的缓存条目
/// </summary>
/// <param name="category"></param>
public static void ClearCategory(Category category)
@@ -183,4 +185,4 @@ namespace ZelWiki.Caching
keys.ForEach(o => MemCache.Remove(o));
}
}
}
}

View File

@@ -3,17 +3,17 @@
namespace ZelWiki.Caching
{
/// <summary>
/// Contains a verbatim cache key.
/// 包含逐字缓存键
/// </summary>
/// <param name="key"></param>
public class WikiCacheKey(string key) : IWikiCacheKey
{
public string Key { get; set; } = key;
public static WikiCacheKey Build(WikiCache.Category category, object?[] segments)
public static WikiCacheKey Build(Category category, object?[] segments)
=> new($"[{category}]:[{string.Join("]:[", segments)}]");
public static WikiCacheKey Build(WikiCache.Category category)
public static WikiCacheKey Build(Category category)
=> new($"[{category}]");
}
}

View File

@@ -3,22 +3,18 @@ using static ZelWiki.Caching.WikiCache;
namespace ZelWiki.Caching
{
/// <summary>
/// Contains a verbatim cache key which also includes the calling function name.
/// </summary>
/// <param name="key"></param>
public class WikiCacheKeyFunction(string key) : IWikiCacheKey
{
public string Key { get; set; } = key;
/// <summary>
/// Builds a cache key which includes the calling function name.
/// 生成一个包含调用函数名称的缓存键。
/// </summary>
public static WikiCacheKeyFunction Build(WikiCache.Category category, object?[] segments, [CallerMemberName] string callingFunction = "")
=> new($"[{category}]:[{string.Join("]:[", segments)}]:[{callingFunction}]");
/// <summary>
/// Builds a cache key which includes the calling function name.
/// 生成一个包含调用函数名称的缓存键。
/// </summary>
public static WikiCacheKeyFunction Build(WikiCache.Category category, [CallerMemberName] string callingFunction = "")
=> new($"[{category}]:[{callingFunction}]");