This implemantation uses PostgreSQL 11+ as distributed cache. Version 4 and up.
Use older versions of this packages (<= 3.1.2)
dotnet add package Community.Microsoft.Extensions.Caching.PostgreSql
services.AddDistributedPostgreSqlCache(setup =>
{
setup.ConnectionString = configuration["ConnectionString"];
setup.SchemaName = configuration["SchemaName"];
setup.TableName = configuration["TableName"];
setup.DisableRemoveExpired = configuration["DisableRemoveExpired"];
// Optional - DisableRemoveExpired default is FALSE
setup.CreateInfrastructure = configuration["CreateInfrastructure"];
// CreateInfrastructure is optional, default is TRUE
// This means que every time starts the application the
// creation of table and database functions will be verified.
setup.ExpiredItemsDeletionInterval = TimeSpan.FromMinutes(30)
// ExpiredItemsDeletionInterval is optional
// This is the periodic interval to scan and delete expired items in the cache. Default is 30 minutes.
// Minimum allowed is 5 minutes. - If you need less than this please share your use case ๐, just for curiosity...
})
services.AddDistributedPostgreSqlCache((serviceProvider, setup) =>
{
// IConfiguration is used as an example here
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
setup.ConnectionString = configuration["ConnectionString"];
...
})
use
services.AddDistributedPostgreSqlCache();
and implement and register
IConfigureOptions<PostgreSqlCacheOptions>
When you have 2 or more instances/microservices/processes and you just to leave one of them removing expired items.
- Note 1: This is not mandatory, see if it fits in you cenario.
- Note 2: If you have only one instance and set to
True
, all the expired items will not be auto-removed, when you callGetItem
those expired items are filtred out. In that case you are responsable to manually remove the expired key or update it
- Then pull from DI like any other service
/// this is extracted from the React+WebApi WebSample
private readonly IDistributedCache _cache;
public WeatherForecastController(IDistributedCache cache)
{
_cache = cache;
}
If you, or the implementation using this cache, is explicitly calling the IDistributedCache.Refresh
to update the sliding window,
then you can turn off the UpdateOnGetCacheItem
to remove the additional DB expiration update call just prior to reading the cached value. This is useful if you are using this package in conjunction with ASP.NET Core Session handling.
services.AddDistributedPostgreSqlCache((serviceProvider, setup) =>
{
...
setup.UpdateOnGetCacheItem = false;
// Or
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
setup.UpdateOnGetCacheItem = configuration["UpdateOnGetCacheItem"];
...
})
For read-only databases or if the database user does not have write
permission you can set ReadOnlyMode = true
- Note 1: This will cancel the sliding expiration and only absolute expiration will work
- Note 2: This can be used to improve performance, but you will not be able to change any cache values
services.AddDistributedPostgreSqlCache((serviceProvider, setup) =>
{
...
setup.ReadOnlyMode = true;
// Or
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
setup.ReadOnlyMode = configuration["UpdateOnGetCacheItem"];
...
})
It creates a table & schema for storing cache (names are configurable)
You will need a local postgresql server with this configuration:
- Server listening to port 5432 at localhost
- The password of your
postgres
account, if not attached already to your user. - Clone this repo.
- Run the following commands inside
PostgreSqlCacheSample
:
dotnet restore
prepare-database.cmd -create
dotnet run
You will need a local postgresql server with this configuration:
- Server listening to port 5432 at localhost
- The password of your
postgres
account, if not attached already to your user. - You also need
npm
andnode
installed on your dev machine - Clone this repo.
- Run the following commands inside
WebSample
:
dotnet restore
prepare-database.cmd -create
dotnet run
It takes some time to npm
retore the packages, grab a โ while waiting...
Then you can delete the database with:
prepare-database.cmd -erase
- v4.0.1 - Add suport to .net 7
- [BREAKING CHANGE] - Drop suport to .net 5
- [BREAKING CHANGE] - Make use of procedures (won't work with PostgreSQL <= 10, use version 3)
- v3.1.2 - removed dependency for
IHostApplicationLifetime
if not supported on the platform:AWS
for instance - issue #28 - v3.1.0 - Added log messages on
Debug
Level, multitarget .net5 and .net6, dropped support to netstandard2.0, fix sample to match multitarget and sample database. - v3.0.2 -
CreateInfrastructure
also creates the schema issue #8 - v3.0.1 -
DisableRemoveExpired
configuration added; IfTRUE
the cache instance won`t delete expired items. - v3.0
- [BREAKING CHANGE] - Direct instantiation not preferred
- Single thread loop remover
- v2.0.x - Update everything to net5.0, more detailed sample project.
- v1.0.8 - Update to latest dependencies -
- MIT