BlogDoFt.RedisToggler
1.0.0
See the version list below for details.
dotnet add package BlogDoFt.RedisToggler --version 1.0.0
NuGet\Install-Package BlogDoFt.RedisToggler -Version 1.0.0
<PackageReference Include="BlogDoFt.RedisToggler" Version="1.0.0" />
<PackageVersion Include="BlogDoFt.RedisToggler" Version="1.0.0" />
<PackageReference Include="BlogDoFt.RedisToggler" />
paket add BlogDoFt.RedisToggler --version 1.0.0
#r "nuget: BlogDoFt.RedisToggler, 1.0.0"
#:package BlogDoFt.RedisToggler@1.0.0
#addin nuget:?package=BlogDoFt.RedisToggler&version=1.0.0
#tool nuget:?package=BlogDoFt.RedisToggler&version=1.0.0
BlogDoFt.RedisToggler.Lib
Português
Esta biblioteca é um wrapper para a Microsoft.Extensions.Caching.StackExchangeRedis, adicionando a capacidade de se auto-desligar quando perceber uma queda do servidor Redis conectado. Desta forma, as requisições não ficarão lentas, aguardando pelo time-out do request ao Redis.
Ainda, quando o Redis retornar on-line, a biblioteca irá se religar automaticamente, tornando as funcionalidades de cache disponíveis outra vez.
Outra funcionalidade que esta biblioteca adiciona é a padronização de chaves. Desta forma, quando necessitar de compartilhar o cache entre aplicações diferentes, o padrão poderá garantir que as aplicações acessem sempre o mesmo conteúdo.
Configurando o cache
Ao configurar as dependências da sua aplicação, adicione uma chamada a .AddCacheWrapper(). Este método configura a conexão e o tipo de cache a ser utilizado.
builder.Services
.AddCacheWrapper(opt =>
{
opt.ConnectionString = "localhost:6379,asyncTimeout=1000,connectTimeout=1000,password=<your-redis-password>,abortConnect=false";
opt.CacheType = CacheType.Redis;
});
O método configura uma instância da classe RedisToggler.Lib.Configurations.CacheConfig, responsável por configurar o cache. Os tipos suportados são:
- NoCache: Nenhuma funcionalidade de cache estará ativa;
- Redis: A aplicação irá utilizar o Redis como repositório de cache;
- Memory: A aplicação irá utilizar a memória como repositório de cache;
Veja a documentação em: CacheType.cs.
Configurando a entidade para cache
Como as entidades cacheadas podem seguir padrões de storage diferentes, a biblioteca possui a capacidade de adicionar configurações para cada entidade cacheada.
Para isso, basta adicionar uma instância de CacheEntryConfiguration como Singleton como dependência da aplicação.
builder.Services
.AddSingleton(opt =>
{
return new CacheEntryConfiguration();
})
Caso você possua mais de uma entidade sendo recuperada do cache, basta que você crie uma classe, herdando de CacheEntryConfiguration, e também a adicione como Singleton nas dependências da aplicação.
Como utilizar?
Adicione ao construtor da classe que utilizará o cache, uma dependência para IDistributedTypedCache<CacheEntryConfiguration>. Neste caso, estamos utilizando uma referência para CacheEntryConfiguration, mas caso você possua outra configuração, basta substituir a referência de CacheEntryConfiguration pela classe de configuração desejada.
Você pode consultar a documentação de IDistributedTypedCache<TEntryConfig> para mais detalhes. Ou pode ver o projeto de exemplo no github do projeto.
English
This library is a wrapper to Microsoft.Extensions.Caching.StackExchangeRedis, adding the capability to auto turn-off it's self when Redis server has a breakdown. This way, the request will not become slow, waiting for Redis connection time-out.
When Redis become on-line, the library will turn-on it's self automatically, enabling the cache capabilities again.
Another feature that this library add is the opiniative pattern of Cache Keys. This way, when will needed to share the cache data with different applications, the pattern could grant that the applications always access the same content.
Configuring cache
When you are configuring the application dependencies, add a call to .AddCacheWrapper(). This extension method configures the connection and the cache type to be used.
builder.Services
.AddCacheWrapper(opt =>
{
opt.ConnectionString = "localhost:6379,asyncTimeout=1000,connectTimeout=1000,password=<your-redis-password>,abortConnect=false";
opt.CacheType = CacheType.Redis;
});
This extension method configures a class instance of RedisToggler.Lib.Configurations.CacheConfig, in charge of cache configuration. The supported types are:
- NoCache: None cache capability are active;
- Redis: The application will use Redis as cache storage;
- Memory: Application will use his container memory as cache storage;
See documentation at: CacheType.cs.
Setup an Entity to be cached
As the entities could be cached by different ways, this library has the ability to add custom configuration to each cacheable entity.
To this, you must add a CacheEntryConfiguration instance as a singleton into IServiceCollection.
builder.Services
.AddSingleton(opt =>
{
return new CacheEntryConfiguration();
})
In case that you have more then one entity being restore from cache, you must write a classe that inherits from CacheEntryConfiguration, and also add an instance of created class, as singleton, into IServiceCollection.
How to use?
At class constructor, that will use the cached entity, add a dependency to IDistributedTypedCache<CacheEntryConfiguration>. In this specific case, we are using a reference to CacheEntryConfiguration, but if you are using other (or multiple) configurations, you may change the CacheEntryConfiguration by the desired configuration class.
You may consult the documentation of IDistributedTypedCache<TEntryConfig> for more details. Or you can read the sample project into github project.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Caching.Memory (>= 6.0.1)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 6.0.20)
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- StackExchange.Redis (>= 2.6.122)
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 |
|---|---|---|
| 2.1.0-alpha.31 | 188 | 12/22/2025 |
| 2.1.0-alpha.2 | 111 | 12/21/2025 |
| 2.0.0-beta.1 | 184 | 12/22/2025 |
| 1.1.0-alpha.7 | 121 | 12/21/2025 |
| 1.1.0-alpha.5 | 180 | 9/10/2023 |
| 1.1.0-alpha.3 | 173 | 8/26/2023 |
| 1.0.0 | 711 | 8/26/2023 |
| 1.0.0-change-log.1 | 172 | 8/26/2023 |
| 1.0.0-beta.1 | 172 | 8/26/2023 |
| 1.0.0-alpha.28 | 172 | 8/26/2023 |
| 1.0.0-alpha.26 | 176 | 8/26/2023 |
| 1.0.0-alpha.24 | 171 | 8/26/2023 |
| 1.0.0-alpha.22 | 171 | 8/26/2023 |
| 1.0.0-alpha.20 | 177 | 8/21/2023 |