c# - 如何获取包含某些关键字的所有缓存数据
问题描述
我正在使用 c# (Microsoft.Extensions.Caching.Memory) 中的缓存,因为我将多个数据存储到缓存中,如下所示。
IMemoryCache cache;
cache.Set("xxxx_1", "xxxx");
cache.Set("xxxx_2", "xxxx");
cache.Set("xxxx_3", "xxxx");
也可以一一获取数据。
Getcachedata = (string)cache.Get("xxxx_1")
但我想获取包含像 xxxx 这样的 Key 的数据完整数据。我浏览了很多文章,但没有得到帮助。
解决方案
尝试编写CacheExtensions
类来自定义GetCacheData
如下方法:
public static class CacheExtensions
{
public static Dictionary<TKey,TValue> GetCacheData<TKey, TValue>(this IMemoryCache cache, TKey key)
{
var data = new Dictionary<TKey, TValue>();
var value = (TValue)(cache.Get(key) ?? default(TValue));
data.Add(key, value);
return data;
}
}
推荐阅读
- npm - 如何验证 x 范围的 semver
- c++ - CAP_PROP_WB_TEMPERATURE 不会对 OpenCV 进行任何更改
- python-2.7 - 在 python (ROS Kinetic) 中通过 OpenCV 访问图像
- quantitative-finance - 聚合生成的输出表的区别是keyedTable和keyedStreamTable
- java - 将十六进制字符串与双精度字符串进行比较
- java - Gridview背景颜色在Android中滚动时改变颜色
- reactjs - 在 React 中将值从 Timer 传递给它的 Parent
- binary - “字节”数据类型大小 = 1 字节,而它可以有 3 位数字?
- html - 如何让最后一个孩子不褪色?
- typescript - 有没有一种方法可以直接从 Typescript 中的联合类型中排除类型?