简体   繁体   中英

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...

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? 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.

There are also several extension methods for the IMemoryCache interface which provide shorthand ways of setting an item in the cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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