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 / Use
/learn @a-patel/LiteXCacheREADME
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!
Provide Storage service for ASP.NET Core (2.0 and later) applications.
Small library to abstract caching functionalities. Quick setup for any caching provider and very simple wrapper for widely used providers. LiteX Cache uses the least common denominator of functionality between the supported providers to build a caching solution. Abstract interface to implement any kind of basic caching services. Having a default/generic implementation to wrap the MemoryCache, Redis Cache, Memcached, SQLite, HTTP Request cache and independent on the underlying caching framework(s).
Very simple configuration in advanced ways. Purpose of this package is to bring a new level of ease to the developers who deal with different caching provider integration with their system and implements many advanced features. You can also write your own and extend it also extend existing providers. Easily migrate or switch between one to another provider with no code breaking changes.
LiteXCache is an interface to unify the programming model for various cache providers. The Core library contains all base interfaces and tools. One should install at least one other LiteXCache package to get caching mechanism implementation.
Achieve significant performance by better use of Http request cache for other external cache providers (Redis, Memcached, SQLite etc).
Cache Providers :books:
Features :pager:
- Multiple provider support (using provider factory)
- Cache any type of data
- Cache data for specific time
- Distributed Cache
- Async compatible
- Cache Removal and Flush support
- Many other features
- Simple API with familiar sliding or absolute expiration
- Guaranteed single evaluation of your factory delegate whose results you want to cache
- Strongly typed generics based API. No need to cast your cached objects every time you retrieve them
- Thread safe, concurrency ready
- Obsolete sync methods
- Interface based API to support the test driven development and dependency injection
- Leverages a provider model on top of ILiteXCacheManager under the hood and can be extended with your own implementation
Basic Usage :page_facing_up:
Step 1 : Install the package :package:
Choose one kinds of caching type that you needs and install it via Nuget. To install LiteXCache, run the following command in the Package Manager Console
PM> Install-Package LiteX.Cache
PM> Install-Package LiteX.Cache.Redis
PM> Install-Package LiteX.Cache.Memcached
PM> Install-Package LiteX.Cache.SQLite
Step 2 : Configuration 🔨
Different types of caching provider have their own way to config. Here are samples that show you how to config.
2.1 : AppSettings
{
//LiteX InMemory Cache settings (Optional)
"InMemoryConfig": {
"EnableLogging": true
},
//LiteX Redis Cache settings
"RedisConfig": {
"RedisCachingConnectionString": "127.0.0.1:6379,ssl=False",
"EnableLogging": true
},
//LiteX Memcached Cache settings (don't use this option)
"MemcachedConfig": {
"EnableLogging": true
},
//LiteX SQLite Config settings (Optional)
"SQLiteConfig": {
"FilePath": "",
"FileName": "",
"EnableLogging": true
}
}
2.2 : Configure Startup Class
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
#region LiteX Caching (InMemory)
// 1. Use default configuration from appsettings.json's 'InMemoryConfig'
services.AddLiteXCache();
//OR
// 2. Load configuration settings using options.
services.AddLiteXCache(option =>
{
option.EnableLogging = false;
});
//OR
// 3. Load configuration settings on your own.
// (e.g. appsettings, database, hardcoded)
var inMemoryConfig = new InMemoryConfig()
{
EnableLogging = false,
};
services.AddLiteXCache(inMemoryConfig);
#endregion
#region 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;
option.EnableLogging = false;
});
//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
EnableLogging = false,
};
services.AddLiteXRedisCache(redisConfig);
#endregion
#region 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;
option.EnableLogging = false;
});
//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,
EnableLogging = false,
};
services.AddLiteXSQLiteCache(sqLiteConfig);
#endregion
#region LiteX Caching (Memcached)
// don't use this option
// 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;
option.EnableLogging = false;
});
//OR
// 3. Load configuration settings on your own.
// (e.g. appsettings, database, hardcoded)
var memcachedConfig = new MemcachedConfig()
{
PersistDataProtectionKeysToMemcached = true,
EnableLogging = false,
};
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);
#endregion
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//Memcached
app.UseLiteXMemcachedCache();
//SQLite
app.UseLiteXSQLiteCache();
}
}
Step 3 : Use in Controller or Business layer :memo:
/// <summary>
/// Customer controller
/// </summary>
[Route("api/[controller]")]
public class CustomerController : Controller
{
#region Fields
private readonly ILiteXCacheManager _cacheManager;
#endregion
#region Ctor
/// <summary>
/// Ctor
/// </summary>
/// <param name="cacheManager"></param>
public CustomerController(ILiteXCacheManager cacheManager)
{
_cacheManager = cacheManager;
}
#endregion
#regio
Security Score
Audited on Aug 15, 2025
