This implemantation uses PostgreSQL as distributed cache.
- Install the package into your project
dotnet add package Community.Microsoft.Extensions.Caching.PostgreSql
- Add the following line to the
Startup
Configure
method.
services.AddDistributedPostgreSqlCache(setup =>
{
ConnectionString = configuration["ConnectionString"],
SchemaName = configuration["SchemaName"],
TableName = configuration["TableName"],
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.
})
- Then pull from DI like any other service
private readonly IDistributedCache _cache;
public WeatherForecastController(IDistributedCache cache)
{
_cache = cache;
}
Creates a table and six functions, see scripts folder for more details.
[schemaName].datediff
[schemaName].deletecacheitemformat
[schemaName].deleteexpiredcacheitemsformat
[schemaName].getcacheitemformat
[schemaName].setcache
[schemaName].updatecacheitemformat
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
Then you can delete the database with:
prepare-database.cmd -erase
- 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