Link Search Menu Expand Document

MemoryCache


This class gives your application the maximum performance as it eliminates the round-trips towards the database for the next 180 minutes (overridable). It implements the ICache interface.

By default, this class is used as the default cache class of the library.

Use-Cases

You should use this class if you wish to cache all the fetched objects from the database into memory.

Usability

Simply pass the instance in the constructor of the repositories (i.e.: BaseRepository and DbRepository) or when calling the fetched operations (i.e.: Query and QueryAll).

Customize

Create a custom interface that implements the ICache interface.

public interface IJsonCache : ICache
{
    // More custom methods
}

Then, implement it in the cache class.

public class JsonCache : IJsonCache
{
    ...
}

Lastly, register in the services collection.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // Registration
    services.AddSingleton<IJsonCache, JsonCache>();
}

Below is the code on how to inject it in the repositories.

public class NorthwindRepository : DbRepository<SqlConnection>
{
    public NorthwindRepository(IOptions<AppSettings> settings,
        IJsonCache cache) // Injected
        : base(settings.Value.ConnectionString, cache)
    { }

    ...
}

Alternatviely, you can create a factory class that returns an instance of ICache object.

public static CacheFactory
{
    private static object _syncLock = new object();
    private static ICache _cache = null;
    
    public static ICache CreateCacher()
    {
        if (_cache == null)
        {
            lock (_syncLock)
            {
                if (_cache == null)
                {
                    _cache = new MemoryCache();
                }
            }
        }
        return _cache;
    }
}

Then, pass it when calling the Query and QueryAll operations.

// Factory class as pointer to the cacher
var memoryCache = CacheFactory.CreateCacher();

// Pass it during the calls
using (var connection = new SqlConnection(connectionString))
{
    var products = connection.QueryAll<Product>(cacheKey: "CacheKey:Products", cache: memoryCache);
}

Or pass it in the constructor of the BaseRepository or DbRepository objects.

// Pass it on the constructor
public class NorthwithRepository : DbRepository<SqlConnection>
{
    public NorthwithRepository(IAppSettings settings)
        : base(settings.Value.ConnectionString, CacheFactory.CreateCacher())
    { }
}