Skip to content
/ CacheIt Public

CacheIt makes caching in your application easier

Notifications You must be signed in to change notification settings

dvlkv/CacheIt

Repository files navigation

CacheIt

CacheIt makes caching in your app better - you don't need to write many lines of boilerplate

Usage

Default caching implementation

public interface IService
{
    string CachedFunction();
    Task<string> AsyncCachedFunction();
}

public class ServiceImplementation : IService
{    
    public string CachedFunction()
    {
        var entry = _cache.Get(GetKey());
        if (entry != null)
            return entry;
        
        var result = "test";
        _cache.Set(result);
        
        return result;
    }

    public async Task<string> AsyncCachedFunction()
    {
        var entry = _cache.Get(GetKey());
        if (entry != null)
            return entry;
        
        var result = "async test";
        _cache.Set(result);
        
        return result;
    }
}

Caching implementation with CacheIt

public interface IService
{
    [Cached]
    string CachedFunction();
    [Cached]
    Task<string> AsyncCachedFunction();
}

public class ServiceImplementation : IService
{    
   public string CachedFunction()
   {
        return "test";
   }

   public async Task<string> AsyncCachedFunction()
   {
       return "async test";
   }
}

Setup

Now CacheIt supports IDistributedCache and IMemoryCache To make it work, you should install CacheIt.DistributedCache or CacheIt.MemoryCache

Now you should add this to your Startup.cs:

services.AddDistributedCachable();

or

services.AddMemoryCachable();

If you use distributed cache, you need to add entry serializer:

services.AddJsonEntrySerializer();

or

services.AddMessagePackEntrySerializer();

Then you should decorate your services to enable caching in them, it's easy: Use

services.AddCachable<IService, ServiceImplementation>();

Instead of

services.AddScoped<IService, ServiceImplementation>();

Configuration

You can configure cache entries like

services.AddMemoryCachable(opts => 
{
    opts.ConfigureAll(entryOptions => { ... });
    opts.Configure<TResult>(entryOptions => { ... });
});

Releases

No releases published

Packages

No packages published

Languages