如何从缓存中删除特定键的特定值

本文关键字:删除 缓存 | 更新日期: 2023-09-27 18:33:28

我想从特定缓存中删除特定的缓存值。

例:

Cache.Insert("TestCacheKey", "111", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High,null); 
Cache.Insert("TestCacheKey", "222", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
Cache.Insert("TestCacheKey", "333", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, null);

因此,我在与密钥关联的缓存中添加了一些数据,即TestCacheKey如上所示。现在,我想从该键中删除一个特定值,即"111",即TestCacheKey。删除该特定值后,当我检索该缓存键(TestCacheKey)时,我应该只获取与该键关联的值为"222"和"333"的剩余两条记录。

那么,如何从缓存中删除特定值。

如何从缓存中删除特定键的特定值

Cache.Insert方法的第一个参数是键。在您的代码中,似乎键是相同的,并且可以通过键访问Cache对象的值。如果我没有错,那么像这样修改你的代码

Cache.Insert("111","TestCacheKey",  null,DateTime.Now.AddSeconds(60),Cache.NoSlidingExpiration,CacheItemPriority.High,null); 
Cache.Insert("222","TestCacheKey" , null,DateTime.Now.AddSeconds(60),Cache.NoSlidingExpiration, CacheItemPriority.High, null);
Cache.Insert("333","TestCacheKey",  null, DateTime.Now.AddSeconds(60),Cache.NoSlidingExpiration, CacheItemPriority.High, null);

对于删除

Cache.remove("111")