|
1 | 1 | # Caching-MySQL |
2 | | -[](https://travis-ci.org/ChaosEngine/Caching-MySQL) |
3 | 2 | [](https://ci.appveyor.com/project/ChaosEngine/caching-mysql) |
4 | 3 | [](https://www.nuget.org/packages/Pomelo.Extensions.Caching.MySql/) |
| 4 | + |
| 5 | +Basing on https://learn.microsoft.com/en-us/aspnet/core/performance/caching/distributed and modified accordingly: |
| 6 | + |
| 7 | +### Distributed MySQL Server Cache |
| 8 | + |
| 9 | +The Distributed MySQL Server Cache implementation (**AddDistributedMySqlCache**) allows the distributed cache to use a MySQL Server database as its backing store. To create a MySQL Server cached item table in a MySQL Server instance, you can use the `dotnet-mysql-cache` tool. The tool creates a table with the name and schema that you specify. |
| 10 | + |
| 11 | +Create a table in MySQL Server by running the `dotnet mysql-cache create` command. Provide the MySQL Server connection string, instance (for example `server=192.169.0.1`), database (for example, `databaseName`), and table name (for example, `NewTableName`): |
| 12 | + |
| 13 | +```dotnetcli |
| 14 | +dotnet mysql-cache create "server=192.169.0.1;user id=userName;password=P4ssword123!;port=3306;database=databaseName;Allow User Variables=True" databaseName "NewTableName" |
| 15 | +``` |
| 16 | + |
| 17 | +A message is logged to indicate that the tool was successful: |
| 18 | + |
| 19 | +```console |
| 20 | +Table and index were created successfully. |
| 21 | +``` |
| 22 | + |
| 23 | +The table created by the `dotnet-mysql-cache` tool has the following schema: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +> [!NOTE] |
| 28 | +> An app should manipulate cache values using an instance of [IDistributedCache](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.distributed.idistributedcache?view=dotnet-plat-ext-8.0), not any other. |
| 29 | +
|
| 30 | +The example snippet how to implement MySql Server cache in `Program.cs`: |
| 31 | + |
| 32 | +```cs |
| 33 | +builder.Services.AddDistributedMySqlCache(options => |
| 34 | +{ |
| 35 | + options.ConnectionString = builder.Configuration.GetConnectionString("DistCache_ConnectionString"); |
| 36 | + options.SchemaName = "databaseName"; |
| 37 | + options.TableName = "NewTableName"; |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +> [!NOTE] |
| 42 | +> A **ConnectionString** (and optionally, **SchemaName** and **TableName**) are typically stored outside of source control (for example, stored by the [Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-8.0&tabs=windows) or in `appsettings.json`/`appsettings.{Environment}.json` files). The connection string may contain credentials that should be kept out of source control systems. |
| 43 | +
|
| 44 | +## Use the distributed cache |
| 45 | + |
| 46 | +One can use same technique as described in this section [Use the distributed cache](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-8.0#use-the-distributed-cache) |
0 commit comments