EntegratorPro.Cache 1.0.2

dotnet add package EntegratorPro.Cache --version 1.0.2                
NuGet\Install-Package EntegratorPro.Cache -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntegratorPro.Cache" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntegratorPro.Cache --version 1.0.2                
#r "nuget: EntegratorPro.Cache, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntegratorPro.Cache as a Cake Addin
#addin nuget:?package=EntegratorPro.Cache&version=1.0.2

// Install EntegratorPro.Cache as a Cake Tool
#tool nuget:?package=EntegratorPro.Cache&version=1.0.2                

EntegratorPro.Cache

Overview

EntegratorPro.Cache is a caching library built around Redis to simplify caching operations. It provides features like storing, retrieving, and removing cached items with support for asynchronous operations. The library includes tools to configure and interact with Redis, offering flexibility in use cases such as highly performant data retrieval and distributed cache management.

Features

  • Redis Integration: Includes core abstractions and implementations for Redis caching.
  • Flexible Service Registration: Configure your cache services with different lifetimes (Scoped, Singleton, Transient).
  • Asynchronous Support: All methods are asynchronous to improve performance in high-concurrency environments.
  • Key-based and Pattern-based Cache Management:
    • Add, retrieve, or remove keys.
    • Remove keys matching patterns.
  • Database Management:
    • Full support for managing caching databases (FlushDatabase).
  • Customizable Options:
    • Define your Redis connection strings, absolute expiration times, and instance identifiers.

Installation

To install and configure the caching services:

  1. Add the EntegratorPro.Cache library to your project.
  2. Configure the Redis connection options in your application.
  3. Use the provided extension methods to integrate caching into your service container.

Configuration

Service Registration

Configure Redis caching services in your Startup.cs or Program.cs by extending IServiceCollection:

var redisOptions = new RedisOption(
    instanceName: "YourInstanceName",
    identityName: "YourIdentity",
    connectionString: "localhost:6379", // your Redis server
    absoluteExpiration: 30 // default expiration in minutes
);

services.AddRedisService(redisOptions, ServiceLifetime.Scoped);

Service registration supports lifetimes:

  • Scoped (default)
  • Singleton
  • Transient

Components

1. RedisOption

RedisOption allows specifying core configurations:

  • InstanceName: The prefix for keys.
  • IdentityName: An identifier for Redis.
  • ConnectionString: Connection details for the Redis server.
  • AbsoluteExpiration: Default expiration time in minutes for cache entries.

Example:

var redisOptions = new RedisOption("AppInstance", "AppIdentity", "localhost:6379", 30);

2. ICacheService and RedisCacheService

The RedisCacheService implements ICacheService and provides methods to interact with the cache:

Core Methods:
  • Check if a key exists:

    await cacheService.AnyAsync("key");
    
  • Retrieve data:

    • Single Key:
      string result = await cacheService.GetAsync("key");
      
    • Typed Data:
      MyType result = await cacheService.GetAsync<MyType>("key");
      
  • Store data:

    await cacheService.SetAsync("key", "value", 10); // Expire in 10 minutes
    
  • Remove data:

    • By Key:
      await cacheService.RemoveAsync("key");
      
    • By Pattern:
      await cacheService.RemoveByPatternAsync("pattern*");
      

3. IRedisServer and RedisServer

RedisServer interacts with the Redis instance:

  • Connects to the server (ConnectionMultiplexer).
  • Provides methods to retrieve databases or flush them:
    var database = redisServer.GetDb(0);
    redisServer.FlushDatabase(0);
    

Example Usage

Simple Cache Operations

// Add an item to the cache
await cacheService.SetAsync("myKey", "myValue", 15); // Expires in 15 minutes

// Retrieve the cached item
var cachedValue = await cacheService.GetAsync("myKey");
Console.WriteLine(cachedValue);

// Check if a key exists
bool exists = await cacheService.AnyAsync("myKey");
Console.WriteLine(exists); // true

// Remove the cached item
await cacheService.RemoveAsync("myKey");

Pattern Matching Cache Removal

await cacheService.RemoveByPatternAsync("User.GetAll*"); // Removes all keys matching the pattern

Error Handling

  • Redis Connection: RedisServer throws exceptions if a connection cannot be established due to invalid credentials or connection strings.
  • Argument Validation: Methods validate and throw exceptions for invalid arguments such as null keys or options.

Dependencies

  • Libraries:
    • StackExchange.Redis: For Redis connection and operations.
    • Microsoft.Extensions.DependencyInjection: For dependency injection.

Use Cases

  • API Response Caching: Cache frequently accessed API results to improve performance.
  • Session Management: Use Redis to manage user sessions across distributed systems.
  • Application Data Cache: Cache application objects to reduce database calls.

License

No explicit license provided. Use responsibly within your project's licensing constraints.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 303 2/23/2025
1.0.1 76 2/23/2025