简体   繁体   English

这个界面怎么用?

[英]How to use this interface?

i found this interface and i want to use it.我找到了这个界面,我想使用它。 But i dont understand how to use the Create function...但我不明白如何使用 Create function ...

namespace Microsoft.Extensions.Caching.Memory
{
    public interface IMemoryCache : IDisposable
    {
        ICacheEntry CreateEntry(object key);
        void Remove(object key);
        bool TryGetValue(object key, out object value);
    }
}

How to store something in CreateEntry when there is only the key not the value in the function call?当 function 调用中只有键而不是值时,如何在 CreateEntry 中存储一些东西? How to store something in the key?如何在密钥中存储东西?

So i have this:所以我有这个:

class RedisObjectTestCache : IMemoryCache
    {
        public ICacheEntry CreateEntry(object key)
        {
            Console.WriteLine("Created key: " + key);
            return new CacheEntryTest() { };
        }

        public void Dispose()
        {
            Console.WriteLine("Dispose");
            return;
        }

        public void Remove(object key)
        {
            Console.WriteLine("Removed key: " + key);
            return;
        }

        public bool TryGetValue(object key, out object value)
        {
            Console.WriteLine("Requested key: " + key);
            value = "";
            return false;
        }
    }

and then i call it with the framework:然后我用框架调用它:

QueryCacheManager.Cache = new RedisObjectTestCache();

Can i somehow get the value?我能以某种方式获得价值吗?

The ICacheEntry instance returned from the CreateEntry method has a Value property which you can set to the value you want to cache, along with several other properties you can use to control the caching.CreateEntry方法返回ICacheEntry实例有一个Value属性,您可以将其设置为要缓存的值,以及可以用来控制缓存的其他几个属性。

There are also several extension methods for the IMemoryCache interface which provide shorthand ways of setting an item in the cache. IMemoryCache接口还有几个扩展方法,它们提供了在缓存中设置项目的简写方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM