Skip to content

Implementation Layer

Furkan Güngör edited this page Jan 2, 2021 · 2 revisions

How to customize EasyCache in Implementation Layer?

Firstly Choice a provider. For Example EasyCache.Memory, EasyCache.Redis or EasyCache.MemCache.

You can customize it inherited from the provider class.

Example for Memory Cache

    public class SampleMemoryCacheManager : EasyMemoryCacheManager
    {
        public SampleMemoryCacheManager(IMemoryCache memoryCache)
            :base(memoryCache)
        {

        }
        public override T Get<T>(string key)
        {
            return base.Get<T>(key);
        }

        public override Task<T> GetAsync<T>(string key)
        {
            return base.GetAsync<T>(key);
        }

        public override void Remove<T>(string key)
        {
            base.Remove<T>(key);
        }

        public override Task RemoveAsync<T>(string key)
        {
            return base.RemoveAsync<T>(key);
        }

        public override void Set<T>(string key, T value, TimeSpan expireTime)
        {
            base.Set(key, value, expireTime);
        }

        public override Task SetAsync<T>(string key, T value, TimeSpan expireTime)
        {
            return base.SetAsync(key, value, expireTime);
        }
    }

Finally configure DI container for IEasyCacheService object.

services.AddEasyMemoryCache();
services.AddTransient<IEasyCacheService,SampleMemoryCacheManager>();

Use EasyCacheRedisManager or EasyCacheMemCacheManager for other providers.

Clone this wiki locally