# Community.Microsoft.Extensions.Caching.PostgreSQL
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
Startup
Configure
method.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...
})
IServiceProvider
accessservices.AddDistributedPostgreSqlCache((serviceProvider, setup) =>
{
// IConfiguration is used as an example here
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
setup.ConnectionString = configuration["ConnectionString"];
...
})
IConfigureOptions<PostgreSqlCacheOptions>
use
services.AddDistributedPostgreSqlCache();
and implement and register
IConfigureOptions<PostgreSqlCacheOptions>
DisableRemoveExpired = True
use case:When you have 2 or more instances/microservices/processes and you just to leave one of them removing expired items.
True
, all the expired items will not be auto-removed, when you call GetItem
those expired items are filtred out.
In that case you are responsable to manually remove the expired key or update it /// this is extracted from the React+WebApi WebSample
private readonly IDistributedCache _cache;
public WeatherForecastController(IDistributedCache cache)
{
_cache = cache;
}
UpdateOnGetCacheItem = false
use case:For read-only databases or if the database user does not have write
permission you can set UpdateOnGetCacheItem = false
services.AddDistributedPostgreSqlCache((serviceProvider, setup) =>
{
...
setup.UpdateOnGetCacheItem = false;
// Or
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
setup.UpdateOnGetCacheItem = configuration["UpdateOnGetCacheItem"];
...
})
It creates a table & schema for storing cache (names are configurable)
You will need a local postgresql server with this configuration:
postgres
account, if not attached already to your user.PostgreSqlCacheSample
:dotnet restore
prepare-database.cmd -create
dotnet run
You will need a local postgresql server with this configuration:
postgres
account, if not attached already to your user.npm
and node
installed on your dev machineWebSample
: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
IHostApplicationLifetime
if not supported on the platform: AWS
for instance - issue #28Debug
Level, multitarget .net5 and .net6, dropped support to netstandard2.0, fix sample to match multitarget and sample database.CreateInfrastructure
also creates the schema issue #8DisableRemoveExpired
configuration added; If TRUE
the cache instance won`t delete expired items.