CacheBox is a lightweight and flexible caching solution designed for .NET applications. It provides an abstraction over multiple cache providers, allowing you to seamlessly switch between different caching providers while maintaining a consistent API.
- Pluggable Cache Providers: Use multiple cache backends like in-memory, Redis, or distributed caches with ease.
- Dependency Injection Support: Integrate CacheBox seamlessly into your .NET Core projects using dependency injection.
- High Performance: Optimized for minimal overhead.
To install CacheBox in your project, you can use the following NuGet command for your provider:
dotnet add package CacheBox.LiteDb
dotnet add package CacheBox.Memory
dotnet add package CacheBox.Redis
dotnet add package CacheBox.Sqlite-
Configuration
CacheBox uses a very simple configuration for all providers with 3 lines.
"Cache": { // Optional: Global prefix to prepend to all keys. Useful for shared systems. "AppPrefix": "MyApplication", // Connection string to connect to the provider. See examples and links below for individual providers. "ConnectionString": "MyConnectionString", // Optional: Default TTL of the stored items. This is a string representation interpreted by TimeSpan.TryParse(). "Timeout": "0:10:0" },
See the Microsoft Documentation for examples of time strings.
-
Register CacheBox in your DI Container
using CacheBox; public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddCacheProvider<MemoryCacheProvider>(); // Example using in-memory cache } }
-
Use CacheBox in your application
public class MyService { private readonly ICacheProvider _cache; public MyService(ICacheProvider cache) { _cache = cache; } public async Task DoWorkAsync() { string data = await _cache.GetAsync("myKey", nameof(MyService)); MyClass stronglyTyped = await _cache.GetAsync<MyClass>("myOtherKey", nameof(MyService)); } }
To switch cache providers, simply register the desired provider in ConfigureServices:
services.AddCacheProvider<RedisCacheProvider>();CacheBox supports multiple cache providers. Each provider can be installed as a separate NuGet package:
-
LiteDbCacheProvider: Uses LiteDb as the cache backing.
Example connection string:
Filename=D:\\cache.db;Password=MyPassword;Connection=sharedSee here for documentation.
Note: You must use a shared connection for a distributed cache, otherwise the first service to connect will create a lock.
-
MemoryCacheProvider: A simple in-memory cache.
A connection string is not needed for in-memory caching.
-
RedisCacheProvider: Uses Redis as the cache backing.
Example connection string:
myRedisServer:6379,password=MyPasswordSee here for documentation.
-
SqliteCacheProvider: Uses Sqlite as the cache backing.
Example connection string:
Data Source=Cache.db;Password=MyPasswordSee here for documentation.
You can also implement your own custom cache provider by creating a class that implements the ICacheProvider interface.
public class MyCustomCacheProvider : ICacheProvider
{
// Implementation goes here
}CacheBox is licensed under the GPL License. See the LICENSE for more information.
Contributions are always welcome! Feel free to open an issue or submit a pull request.