Skip to content

TJPoorman/CacheBox

Repository files navigation

CacheBox

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.

Features

  • 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.

Installation

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

Usage

Basic Setup

  1. 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.

  2. Register CacheBox in your DI Container

    using CacheBox;
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCacheProvider<MemoryCacheProvider>(); // Example using in-memory cache
        }
    }
  3. 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));
        }
    }

Switching Providers

To switch cache providers, simply register the desired provider in ConfigureServices:

services.AddCacheProvider<RedisCacheProvider>();

Cache Providers

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=shared

    See 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=MyPassword

    See here for documentation.

  • SqliteCacheProvider: Uses Sqlite as the cache backing.

    Example connection string: Data Source=Cache.db;Password=MyPassword

    See here for documentation.

Adding Custom Providers

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
}

License

CacheBox is licensed under the GPL License. See the LICENSE for more information.

Contributing

Contributions are always welcome! Feel free to open an issue or submit a pull request.

About

A super simple way to add caching to your .net core projects

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages