LiteX.Cache.Core 5.0.0

LiteXCache is simple yet powerful and very high performance cache mechanism and incorporating both synchronous and asynchronous usage with some advanced usages of caching which can help us to handle caching more easier!
 Provide Cache service for any type of application (.NET Core, .NET Standard).
 Very simple yet advanced configuration. Minimal (one line) code configuration is required.

     
 It supports various cache providers and implements many advanced features. You can also write your own and extend it also extend eaxting providers. Easily migrate or switch between one to another provider with no code breaking changes.
 Having a default/generic implementation to wrap the MemoryCache, Redis Cache, Memcached, SQLite, HTTP Request cache and independed on the underlying caching framework(s).
 LiteXCache is an interface to unify the programming model for various cache providers.
 Better use of Http request cache for other external cache providers (Redis, Memcached, SQLite etc).
 The Core library contains all base interfaces and tools. One should install at least one other CacheManager package to get cache handle implementations.
 This is the ASP.NET Core configuration integration package (Built-in).

There is a newer version of this package available.
See the version list below for details.
Install-Package LiteX.Cache.Core -Version 5.0.0
dotnet add package LiteX.Cache.Core --version 5.0.0
<PackageReference Include="LiteX.Cache.Core" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LiteX.Cache.Core --version 5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

LiteXCache

LiteXCache is simple yet powerful and very high-performance cache mechanism and incorporating both synchronous and asynchronous usage with some advanced usages of caching which can help us to handle caching more easier!

Install Nuget packages

Run the nuget command for installing the client as,

Install-Package LiteX.Cache.Core
Install-Package LiteX.Cache
Install-Package LiteX.Cache.Redis
Install-Package LiteX.Cache.Memcached
Install-Package LiteX.Cache.SQLite
AppSettings
{  
  "RedisConfig": {
    "RedisCachingConnectionString": "127.0.0.1:6379,ssl=False",
    "PersistDataProtectionKeysToRedis": false
  },
  "MemcachedConfig": {
    "PersistDataProtectionKeysToMemcached": false
  },
  //LiteX SQLite Config settings (Optional)
  "SQLiteConfig": {
    "FilePath": "",
    "FileName": ""
  }
}
Startup Configuration
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        //LiteX Caching (In-Memory)
        services.AddLiteXCache();

        //LiteX Caching (Redis)
        // 1. Use default configuration from appsettings.json's 'RedisConfig'
        services.AddLiteXRedisCache();
        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXRedisCache(option =>
        {
            option.RedisCachingConnectionString = "127.0.0.1:6379,ssl=False";
            //option.PersistDataProtectionKeysToRedis = true;
        });
        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var redisConfig = new RedisConfig()
        {
            RedisCachingConnectionString = "127.0.0.1:6379,ssl=False",
            //PersistDataProtectionKeysToRedis = true
        };
        services.AddLiteXRedisCache(redisConfig);

        //LiteX Caching (SQLite)
        // 1. Use default configuration from appsettings.json's 'SQLiteConfig'
        services.AddLiteXSQLiteCache();
        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXSQLiteCache(option =>
        {
            option.FileName = "";
            option.FilePath = "";
            option.OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.ReadWriteCreate;
            option.CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default;
        });
        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var sqLiteConfig = new SQLiteConfig()
        {
            FileName = "",
            FilePath = "",
            OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.ReadWriteCreate,
            CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default
        };
        services.AddLiteXSQLiteCache(sqLiteConfig);

        //LiteX Caching (Memcached)
        // 1. Use default configuration from appsettings.json's 'MemcachedConfig'
        services.AddLiteXMemcachedCache(providerOption =>
        {
            providerOption.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            providerOption.Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>() { new Enyim.Caching.Configuration.Server() { Address = "", Port = 0 } };

            // configure rest of the options as needed
        });
        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXMemcachedCache(providerOption =>
        {
            providerOption.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            providerOption.Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>() { new Enyim.Caching.Configuration.Server() { Address = "", Port = 0 } };

            // configure rest of the options as needed
        }, option =>
        {
            option.PersistDataProtectionKeysToMemcached = true;
        });
        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var memcachedConfig = new MemcachedConfig()
        {
            PersistDataProtectionKeysToMemcached = true
        };
        services.AddLiteXMemcachedCache(providerOption =>
        {
            providerOption.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            providerOption.Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>() { new Enyim.Caching.Configuration.Server() { Address = "", Port = 0 } };
            // configure rest of the options as needed
        }, memcachedConfig);
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseLiteXMemcachedCache();
        app.UseLiteXSQLiteCache();
    }
}

Use in Controller or Business layer

public class CustomerController : Controller
{
    private readonly ILiteXCacheManager _cacheManager;
    public CustomerController(ILiteXCacheManager cacheManager)
    {
        _cacheManager = cacheManager;
    }
    public IActionResult GetCacheProviderType()
    {
        return Ok(_cacheManager.CacheProviderType.ToString());
    }
    public IActionResult CacheCustomers()
    {
        IList<Customer> customers;
        var key = "customers";
        customers = _cacheManager.Get(key, () =>
        {
            var result = new List<Customer>();
            result = GetCustomers().ToList();
            return result;
        });
        //customers = await _cacheManager.GetAsync(key, () =>
        //{
        //    var result = new List<Customer>();
        //    result = GetCustomers().ToList();
        //    return result;
        //});
    }

    public IActionResult CacheCustomers(int cacheTime)
    {
        IList<Customer> customers;
        var cacheKey = "customers";
        customers = _cacheManager.Get(cacheKey, cacheTime, () =>
        {
            var result = new List<Customer>();
            result = GetCustomers().ToList();
            return result;
        });
        //customers = await _cacheManager.GetAsync(cacheKey, cacheTime, () =>
        //{
        //    var result = new List<Customer>();
        //    result = GetCustomers().ToList();
        //    return result;
        //});
    }

    public IActionResult CacheCustomer(int customerId)
    {
        Customer customer = null;
        var cacheKey = $"customer-{customerId}";
        customer = _cacheManager.Get<Customer>(cacheKey);
        //customer = await _cacheManager.GetAsync<Customer>(cacheKey);
        if (customer == default(Customer))
        {
            //no value in the cache yet
            //let's load customer and cache the result
            customer = GetCustomerById(customerId);
            _cacheManager.Set(cacheKey, customer, 60);
            //await _cacheManager.SetAsync(cacheKey, customer, 60);
        }
    }

    public IActionResult RemoveCachedCustomers()
    {
        var cacheKey = "customers";
        _cacheManager.Remove(cacheKey);
        //await _cacheManager.RemoveAsync(cacheKey);
        var cacheKeyPattern = "customers-";
        // remove by pattern
        _cacheManager.RemoveByPattern(cacheKeyPattern);
        //await _cacheManager.RemoveByPatternAsync(cacheKeyPattern);
    }

    public IActionResult ClearCachedItems()
    {
        _cacheManager.Clear();
        //await _cacheManager.ClearAsync();
        return Ok();
    }
}

Coming soon

  • Logging

LiteXCache

LiteXCache is simple yet powerful and very high-performance cache mechanism and incorporating both synchronous and asynchronous usage with some advanced usages of caching which can help us to handle caching more easier!

Install Nuget packages

Run the nuget command for installing the client as,

Install-Package LiteX.Cache.Core
Install-Package LiteX.Cache
Install-Package LiteX.Cache.Redis
Install-Package LiteX.Cache.Memcached
Install-Package LiteX.Cache.SQLite
AppSettings
{  
  "RedisConfig": {
    "RedisCachingConnectionString": "127.0.0.1:6379,ssl=False",
    "PersistDataProtectionKeysToRedis": false
  },
  "MemcachedConfig": {
    "PersistDataProtectionKeysToMemcached": false
  },
  //LiteX SQLite Config settings (Optional)
  "SQLiteConfig": {
    "FilePath": "",
    "FileName": ""
  }
}
Startup Configuration
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        //LiteX Caching (In-Memory)
        services.AddLiteXCache();

        //LiteX Caching (Redis)
        // 1. Use default configuration from appsettings.json's 'RedisConfig'
        services.AddLiteXRedisCache();
        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXRedisCache(option =>
        {
            option.RedisCachingConnectionString = "127.0.0.1:6379,ssl=False";
            //option.PersistDataProtectionKeysToRedis = true;
        });
        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var redisConfig = new RedisConfig()
        {
            RedisCachingConnectionString = "127.0.0.1:6379,ssl=False",
            //PersistDataProtectionKeysToRedis = true
        };
        services.AddLiteXRedisCache(redisConfig);

        //LiteX Caching (SQLite)
        // 1. Use default configuration from appsettings.json's 'SQLiteConfig'
        services.AddLiteXSQLiteCache();
        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXSQLiteCache(option =>
        {
            option.FileName = "";
            option.FilePath = "";
            option.OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.ReadWriteCreate;
            option.CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default;
        });
        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var sqLiteConfig = new SQLiteConfig()
        {
            FileName = "",
            FilePath = "",
            OpenMode = Microsoft.Data.Sqlite.SqliteOpenMode.ReadWriteCreate,
            CacheMode = Microsoft.Data.Sqlite.SqliteCacheMode.Default
        };
        services.AddLiteXSQLiteCache(sqLiteConfig);

        //LiteX Caching (Memcached)
        // 1. Use default configuration from appsettings.json's 'MemcachedConfig'
        services.AddLiteXMemcachedCache(providerOption =>
        {
            providerOption.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            providerOption.Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>() { new Enyim.Caching.Configuration.Server() { Address = "", Port = 0 } };

            // configure rest of the options as needed
        });
        //OR
        // 2. Load configuration settings using options.
        services.AddLiteXMemcachedCache(providerOption =>
        {
            providerOption.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            providerOption.Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>() { new Enyim.Caching.Configuration.Server() { Address = "", Port = 0 } };

            // configure rest of the options as needed
        }, option =>
        {
            option.PersistDataProtectionKeysToMemcached = true;
        });
        //OR
        // 3. Load configuration settings on your own.
        // (e.g. appsettings, database, hardcoded)
        var memcachedConfig = new MemcachedConfig()
        {
            PersistDataProtectionKeysToMemcached = true
        };
        services.AddLiteXMemcachedCache(providerOption =>
        {
            providerOption.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Binary;
            providerOption.Servers = new System.Collections.Generic.List<Enyim.Caching.Configuration.Server>() { new Enyim.Caching.Configuration.Server() { Address = "", Port = 0 } };
            // configure rest of the options as needed
        }, memcachedConfig);
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseLiteXMemcachedCache();
        app.UseLiteXSQLiteCache();
    }
}

Use in Controller or Business layer

public class CustomerController : Controller
{
    private readonly ILiteXCacheManager _cacheManager;
    public CustomerController(ILiteXCacheManager cacheManager)
    {
        _cacheManager = cacheManager;
    }
    public IActionResult GetCacheProviderType()
    {
        return Ok(_cacheManager.CacheProviderType.ToString());
    }
    public IActionResult CacheCustomers()
    {
        IList<Customer> customers;
        var key = "customers";
        customers = _cacheManager.Get(key, () =>
        {
            var result = new List<Customer>();
            result = GetCustomers().ToList();
            return result;
        });
        //customers = await _cacheManager.GetAsync(key, () =>
        //{
        //    var result = new List<Customer>();
        //    result = GetCustomers().ToList();
        //    return result;
        //});
    }

    public IActionResult CacheCustomers(int cacheTime)
    {
        IList<Customer> customers;
        var cacheKey = "customers";
        customers = _cacheManager.Get(cacheKey, cacheTime, () =>
        {
            var result = new List<Customer>();
            result = GetCustomers().ToList();
            return result;
        });
        //customers = await _cacheManager.GetAsync(cacheKey, cacheTime, () =>
        //{
        //    var result = new List<Customer>();
        //    result = GetCustomers().ToList();
        //    return result;
        //});
    }

    public IActionResult CacheCustomer(int customerId)
    {
        Customer customer = null;
        var cacheKey = $"customer-{customerId}";
        customer = _cacheManager.Get<Customer>(cacheKey);
        //customer = await _cacheManager.GetAsync<Customer>(cacheKey);
        if (customer == default(Customer))
        {
            //no value in the cache yet
            //let's load customer and cache the result
            customer = GetCustomerById(customerId);
            _cacheManager.Set(cacheKey, customer, 60);
            //await _cacheManager.SetAsync(cacheKey, customer, 60);
        }
    }

    public IActionResult RemoveCachedCustomers()
    {
        var cacheKey = "customers";
        _cacheManager.Remove(cacheKey);
        //await _cacheManager.RemoveAsync(cacheKey);
        var cacheKeyPattern = "customers-";
        // remove by pattern
        _cacheManager.RemoveByPattern(cacheKeyPattern);
        //await _cacheManager.RemoveByPatternAsync(cacheKeyPattern);
    }

    public IActionResult ClearCachedItems()
    {
        _cacheManager.Clear();
        //await _cacheManager.ClearAsync();
        return Ok();
    }
}

Coming soon

  • Logging

Release Notes

Code re-factoring and optimization changes.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
6.0.0 1,068 6/30/2018
5.0.0 209 6/23/2018
4.0.0 201 6/18/2018
3.0.0 299 6/2/2018
2.0.0 198 5/7/2018
1.0.0 330 5/4/2018