提问人:Abed Shabahang 提问时间:12/17/2022 最后编辑:marc_sAbed Shabahang 更新时间:12/26/2022 访问量:183
通过 IDistributedCache 中的自定义接口进行缓存
Caching by a custom interface from IDistributedCache
问:
我想从 IDistributedCache 实现自定义接口,并为我的服务中的所有查询创建一个动态键,但我遇到了一个问题。
当我有一个 in 时,我可以通过 创建一个唯一的键,但是当我的查询是 时,我没有创建唯一键的输入。Id
GET(Id)
Id
Getall()
如何在不对项目进行硬编码的情况下为所有查询创建唯一键?
请帮帮我
我使用这个接口,但我不知道它是如何工作的!!!!
/// <summary>
/// Represents a manager for caching between HTTP requests (long term caching)
/// </summary>
public interface IStaticCacheManager : IDisposable
{
/// <summary>
/// Refreshes a value in the cache based on its key, resetting its sliding expiration timeout (if any).
/// </summary>
/// <param name="key">A string identifying the requested value.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
Task RefreshAsync(CacheKey key);
/// <summary>
/// Refreshes a value in the cache based on its key, resetting its sliding expiration timeout (if any).
/// </summary>
/// <param name="key">A string identifying the requested value.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
void Refresh(CacheKey key);
/// <summary>
/// Get a cached item. If it's not in the cache yet, then load and cache it
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Cache key</param>
/// <param name="acquire">Function to load item if it's not in the cache yet</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the cached value associated with the specified key
/// </returns>
Task<T> GetAsync<T>(CacheKey key, Func<Task<T>> acquire);
/// <summary>
/// Get a cached item. If it's not in the cache yet, then load and cache it
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Cache key</param>
/// <param name="acquire">Function to load item if it's not in the cache yet</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task r
///
///
/// esult contains the cached value associated with the specified key
/// </returns>
Task<T> GetAsync<T>(CacheKey key, Func<T> acquire);
/// <summary>
/// Get a cached item. If it's not in the cache yet, then load and cache it
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Cache key</param>
/// <param name="acquire">Function to load item if it's not in the cache yet</param>
/// <returns>The cached value associated with the specified key</returns>
T Get<T>(CacheKey key, Func<T> acquire);
/// <summary>
/// Remove the value with the specified key from the cache
/// </summary>
/// <param name="cacheKey">Cache key</param>
/// <param name="cacheKeyParameters">Parameters to create cache key</param>
/// <returns>A task that represents the asynchronous operation</returns>
Task RemoveAsync(CacheKey cacheKey, params object[] cacheKeyParameters);
/// <summary>
/// Remove the value with the specified key from the cache
/// </summary>
/// <param name="cacheKey">Cache key</param>
/// <param name="cacheKeyParameters">Parameters to create cache key</param>
/// <returns>A task that represents the asynchronous operation</returns>
void Remove(CacheKey cacheKey, params object[] cacheKeyParameters);
/// <summary>
/// Add the specified key and object to the cache
/// </summary>
/// <param name="key">Key of cached item</param>
/// <param name="data">Value for caching</param>
/// <returns>A task that represents the asynchronous operation</returns>
Task SetAsync(CacheKey key, object data);
/// <summary>
/// Add the specified key and object to the cache
/// </summary>
/// <param name="key">Key of cached item</param>
/// <param name="data">Value for caching</param>
/// <returns>A task that represents the asynchronous operation</returns>
void Set(CacheKey key, object data);
/// <summary>
/// Remove items by cache key prefix
/// </summary>
/// <param name="prefix">Cache key prefix</param>
/// <param name="prefixParameters">Parameters to create cache key prefix</param>
/// <returns>A task that represents the asynchronous operation</returns>
Task RemoveByPrefixAsync(string prefix, params object[] prefixParameters);
/// <summary>
/// Clear all cache data
/// </summary>
/// <returns>A task that represents the asynchronous operation</returns>
Task ClearAsync();
/// <summary>
/// Clear all cache data
/// </summary>
/// <returns>A task that represents the asynchronous operation</returns>
void Clear();
#region Cache key
/// <summary>
/// Create a copy of cache key and fills it by passed parameters
/// </summary>
/// <param name="cacheKey">Initial cache key</param>
/// <param name="cacheKeyParameters">Parameters to create cache key</param>
/// <returns>Cache key</returns>
CacheKey PrepareKey(CacheKey cacheKey, params object[] cacheKeyParameters);
/// <summary>
/// Create a copy of cache key using the default cache time and fills it by passed parameters
/// </summary>
/// <param name="cacheKey">Initial cache key</param>
/// <param name="cacheKeyParameters">Parameters to create cache key</param>
/// <returns>Cache key</returns>
CacheKey PrepareKeyForDefaultCache(CacheKey cacheKey, params object[] cacheKeyParameters);
/// <summary>
/// Create a copy of cache key using the short cache time and fills it by passed parameters
/// </summary>
/// <param name="cacheKey">Initial cache key</param>
/// <param name="cacheKeyParameters">Parameters to create cache key</param>
/// <returns>Cache key</returns>
CacheKey PrepareKeyForShortTermCache(CacheKey cacheKey, params object[] cacheKeyParameters);
#endregion
}
/// <summary>
/// Represents key for caching objects
/// </summary>
public partial class CacheKey
{
#region Ctor
/// <summary>
/// Initialize a new instance with key and prefixes
/// </summary>
/// <param name="key">Key</param>
/// <param name="prefixes">Prefixes for remove by prefix functionality</param>
public CacheKey(string key, params string[] prefixes)
{
Key = key;
Prefixes.AddRange(prefixes.Where(prefix => !string.IsNullOrEmpty(prefix)));
}
#endregion
#region Methods
/// <summary>
/// Create a new instance from the current one and fill it with passed parameters
/// </summary>
/// <param name="createCacheKeyParameters">Function to create parameters</param>
/// <param name="keyObjects">Objects to create parameters</param>
/// <returns>Cache key</returns>
public virtual CacheKey Create(Func<object, object> createCacheKeyParameters, params object[] keyObjects)
{
var cacheKey = new CacheKey(Key, Prefixes.ToArray());
if (!keyObjects.Any())
return cacheKey;
cacheKey.Key = string.Format(cacheKey.Key, keyObjects.Select(createCacheKeyParameters).ToArray());
for (var i = 0; i < cacheKey.Prefixes.Count; i++)
cacheKey.Prefixes[i] = string.Format(cacheKey.Prefixes[i], keyObjects.Select(createCacheKeyParameters).ToArray());
return cacheKey;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets a cache key
/// </summary>
public string Key { get; protected set; }
/// <summary>
/// Gets or sets prefixes for remove by prefix functionality
/// </summary>
public List<string> Prefixes { get; protected set; } = new List<string>();
/// <summary>
/// Gets or sets a cache time in minutes
/// </summary>
public DistributedCacheEntryOptions CacheTime { get; set; } //= Singleton<AppSettings>.Instance.Get<CacheConfig>().DefaultCacheTime;
#endregion
}
答: 暂无答案
评论