Skip to content

scaleddomains/ScaledDomains.Extensions.Caching.MySql

Repository files navigation

ScaledDomains.Extensions.Caching.MySql

About

ScaledDomains.Extensions.Caching.MySql is a free, open source distributed cache implementation using MySql as datastore, inspired by Microsoft.Extensions.Cachning.SqlServer. Founded and maintained by Endre Toth and Attila Ersek.

Build License Codecov SonarCloud

Getting started

The Distributed MySQL Server Cache implementation allows the distributed cache to use a MySQL Server database as its caching store. To create a table in a database instance, you can use the mysql-distributed-cache tool. The tool creates a table the name that you specify.

1. Create table

1.1 Requirements

The following example creates table with myDistributedCache name on MySQL server (example.com) under the pizzaordersystem-db schema.

The CLI tool parameters:

  1. [connectionString] - The mysql connection string to connect to the database.
  2. [tableName] - Name of the table to be created.
mysql-distributed-cache Server=example.com;Database=pizzaordersystem-db;User=dbAdmin; myDistributedCache

The connection string may contain credentials that should be kept out of source control systems.

The table has the following schema:

describe `pizzaordersystem-db`.`myDistributedCache`;
Field Type Null Key Default Extra
Id varchar(767) NO PRI NULL
AbsoluteExpiration datetime(6) YES NULL
ExpiresAt datetime(6) NO MUL NULL
SlidingExpiration time(6) YES NULL
Value longblob NO NULL

2. Setup your application

The package manipulates cache values using an instance of IDistributedCache.

services.AddDistributedMySqlServerCache(options => {
                options.ConnectionString = _config["DistributedCache_ConnectionString"];
                options.TableName = "myDistributedCache";
});

2.1 Options

Property Type Description Default Required/Optional
ConnectionString string The connection string to the database. REQUIRED
TableName string Name of the table where the cache items are stored. REQUIRED
ExpirationScanFrequency TimeSpan The minimum length of time between successive scans for expired items. 00:20:00 OPTIONAL

Usage

The IDistributedCache interface provides basic methods to manage items, in the distributed cache implementation:

  • Get, GetAsync: Accepts a string key and retrieves a cached item as a byte[] array if found in the cache.
  • Set, SetAsync: Adds an item (as byte[] array) to the cache using a string key.
  • Refresh, RefreshAsync: Refreshes an item in the cache based on its key, resetting its sliding expiration timeout (if any).
  • Remove, RemoveAsync: Removes a cache item based on its string key.

Limits

  • Cache key length must be less than 767 and encoding should be ASCII.
  • The maximum size of the cache item is 4Gb.

Bug reports and feature requests

Please use the Issue Tracker.