如何在asp.net核心中通过MemoryCache进行迭代? [英] How to iterate through MemoryCache in asp.net core?

查看:48
本文介绍了如何在asp.net核心中通过MemoryCache进行迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IMemoryCache中没有可用的方法可以循环访问每个缓存的项目.我的项目很小,我不想使用Redis等其他选项.

There's no available method in IMemoryCache that allows to iterate through each cached item. My project is small, I don't want to use other options like Redis.

namepsace    Microsoft.Extensions.Caching.Memory{
        public static class CacheExtensions
    {
        public static object Get(this IMemoryCache cache, object key);
        public static TItem Get<TItem>(this IMemoryCache cache, object key);
        public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, TItem> factory);
        [AsyncStateMachine(typeof(CacheExtensions.<GetOrCreateAsync>d__9<>))]
        public static Task<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, Task<TItem>> factory);
        public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value);
        public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, DateTimeOffset absoluteExpiration);
        public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, TimeSpan absoluteExpirationRelativeToNow);
        public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, IChangeToken expirationToken);
        public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options);
        public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out TItem value);
    }
}

https://github.com. com/aspnet/Caching/blob/dev/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs

推荐答案

您应该缓存两种类型的项目.

You should cache two type of items.

  1. 您按原样缓存属性,abc.xyz-{0}.
  2. 第二次在主键名称abc.xyz
  3. 下缓存属性列表
  1. You cache your properties as they are, abc.xyz-{0}.
  2. Second cache a list of property under the main key name, abc.xyz

示例代码:

cache.Set("abc.xyz-name", name, TimeSpan.FromMinutes(30));
cache.Set("abc.xyz-lastname", lastname, TimeSpan.FromMinutes(30));
cache.Set("abc.xyz-birthday", birthday, TimeSpan.FromMinutes(30));
cache.Set("abc.xyz", new List<string> { "abc.xyz-name", "abc.xyz-lastname", "abc.xyz-birthday" }, TimeSpan.FromMinutes(30));

以及删除时:

var keys = cache.Get<List<string>>("abc.xyz");
foreach(var key in keys)
    cache.Remove(key);
cache.remove("abc.xyz");

大多数服务使用IDistributedCache(在您的情况下为MemoryDistributedCache,在注册时会再次注入MemoryCache类的IMemoryCache).

Most of the services use IDistributedCache (in your case MemoryDistributedCache when registered - which again injects IMemoryCache which is MemoryCache class).

在分布式缓存中,您无法迭代所有键,因为可能有数百万个键,如果您可以/将要对其进行迭代,则这将大大降低缓存服务的性能.

In a distributed cache you can't iterate over all keys as there are potentially millions of keys and this would significantly reduce the performance of the cached service if you could/would iterate over it.

因此,当您用分布式缓存(例如Redis)替换内存缓存时,上述解决方案也很友好并且可以使用.

So the above solution is also friendly and ready for the case when you replace your memory cache with a distributed cache, such as Redis.

这篇关于如何在asp.net核心中通过MemoryCache进行迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆