Skip to content

Abstraction Layer

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

How to customize EasyCache?

Easy Cache use EasyCache.Core package for base operations. This package using can be customize Easy Cache.

Installation

Install EasyCache.Core from Nuget

Table of contents

This package includes;

  • Abstractions
    • IEasyCacheService.cs
  • Extensions
    • EasyCacheServiceExtension.cs

EasyCacheCore

Customization

You can customize it inherited from the IEasyCacheService interface.

Sample

For Example Memory Cache customization;

    public class SampleMemoryCacheManager : IEasyCacheService
    {
        private readonly IMemoryCache memoryCache;

        public SampleMemoryCacheManager(IMemoryCache memoryCache)
        {
            this.memoryCache = memoryCache;
        }
        
        public virtual T Get<T>(string key)
        {
            var data = memoryCache.Get<T>(key);
            if (data == null) return default(T);
            return data;
        }

        public virtual Task<T> GetAsync<T>(string key)
        {
            var data = Task.Run(() => Get<T>(key));
            return data;
        }

        public virtual void Remove<T>(string key)
        {
            memoryCache.Remove(key);
        }

        public virtual Task RemoveAsync<T>(string key)
        {
            Task.Run(() => Remove<T>(key));
            return Task.CompletedTask;
        }

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

        public virtual Task SetAsync<T>(string key, T value, TimeSpan expireTime)
        {
            Task.Run(() => Set<T>(key, value, expireTime));
            return Task.CompletedTask;
        }
    }

Finally configure DI container for IEasyCacheService object.

services.AddTransient<IEasyCacheService,SampleMemoryCacheManager>();

So can be use IEasyCacheSevrice object from Dependency Injection.

Clone this wiki locally