Skip to content

Using LazyCache and Ninject

Alastair Crabtree edited this page May 16, 2018 · 2 revisions

NOTE: This quickstart is for LazyCache version 2.x

Using LazyCache with the Ninject dependency injection framework

Add LazyCache.Ninject to your project (LazyCache.Ninject has a dependency on LazyCache so it should get installed automatically)

dotnet add MyProject package LazyCache.Ninject

or in the Package Manager Console

Install-Package LazyCache.Ninject

Next we demonstrate how to create a kernel, and activate and use an instance of the CachingService by requesting an implementation of IAppCache:

class Program
{
    public static void Main() 
    {
        IKernel kernel = new StandardKernel(new LazyCacheModule());
        var cache = kernel.Get<IAppCache>();
        cache.GetOrAdd("get-blog-pages", () => GetPages());
    }

    List<BlogPages> GetPages() 
    {
        // ..... get pages from a DB .....
    }
}