Skip to content

New distributed cache implementation based on Microsoft SQL Server #43

@kichalla

Description

@kichalla

A new distributed cache implementation based on Microsoft SQL Server was checked in few days back.

There are two nuget packages related to this:

  • Microsoft.Framework.Caching.SqlServer - Has the actual cache implementation
  • Microsoft.Framework.Caching.SqlConfig - Has the 'global' command to set up the database table and indexes

Using this cache requires setting up the database and tables that this implementation understands. Follow the steps below for that:

  • Create a database in the SQL server manually.
  • Run dnu commands install Microsoft.Framework.Caching.SqlConfig to install the command called sqlservercache
  • Run sqlservercache create <connectionstring-to-database> <schema-name> <table-name> to set up the table and indexes.
  • Register this cache in DI. Example:
services.AddSqlServerCache(o =>
{
    o.ConnectionString = "Server=localhost;Database=CacheSampleDb;Trusted_Connection=True;";
    o.SchemaName = "dbo";
    o.TableName = "CacheSample";
});

And End-to-end example of using this cache is with Session middleware where session data is stored in database making it available across multiple servers behind a load balancer.

// Registering services
services.AddSqlServerCache(o =>
{
    o.ConnectionString = "Server=localhost;Database=CacheSampleDb;Trusted_Connection=True;";
    o.SchemaName = "dbo";
    o.TableName = "CacheSample";
});
services.AddSession();

// Registering session middleware
app.UseSession();
app.UseMvcWithDefaultRoute();

Code:

Samples:

Feedback is welcome

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions