Skip to content

Commit bf486dc

Browse files
authored
Merge pull request #112 from PomeloFoundation/dev
Merging dev to master
2 parents b146297 + 542ffb3 commit bf486dc

16 files changed

+188
-148
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: csharp
22
mono: none
3-
dotnet: 7.0.100
4-
dist: bionic
3+
dotnet: 8.0.101
4+
dist: jammy
55
addons:
66
apt:
77
packages:
@@ -10,7 +10,6 @@ addons:
1010
- libicu-dev
1111
- libssl-dev
1212
- libunwind8
13-
- zlib1g
1413
env:
1514
global:
1615
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Basing on https://learn.microsoft.com/en-us/aspnet/core/performance/caching/dist
88

99
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.
1010

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`):
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`), table name (for example, `NewTableName`) and optional database (for example, `MyDatabaseName`):
1212

1313
```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"
14+
dotnet mysql-cache create "server=192.169.0.1;user id=userName;password=P4ssword123!;port=3306;database=MyDatabaseName;Allow User Variables=True" "NewTableName" --databaseName "MyDatabaseName"
1515
```
1616

1717
A message is logged to indicate that the tool was successful:
@@ -33,8 +33,8 @@ The example snippet how to implement MySql Server cache in `Program.cs`:
3333
builder.Services.AddDistributedMySqlCache(options =>
3434
{
3535
options.ConnectionString = builder.Configuration.GetConnectionString("DistCache_ConnectionString");
36-
options.SchemaName = "databaseName";
37-
options.TableName = "NewTableName";
36+
options.SchemaName = "MyDatabaseName"; //optional
37+
options.TableName = "NewTableName"; //required
3838
});
3939
```
4040

src/Pomelo.Extensions.Caching.MySql/MySqlCache.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public MySqlCache(IOptions<MySqlCacheOptions> options)
3838
+ $" and {nameof(cacheOptions.ConnectionString)} cannot be empty or null at the same time.");
3939
}
4040

41-
if (string.IsNullOrEmpty(cacheOptions.SchemaName))
42-
{
43-
throw new ArgumentException(
44-
$"{nameof(cacheOptions.SchemaName)} cannot be empty or null.");
45-
}
41+
//if (string.IsNullOrEmpty(cacheOptions.SchemaName))
42+
//{
43+
// throw new ArgumentException(
44+
// $"{nameof(cacheOptions.SchemaName)} cannot be empty or null.");
45+
//}
4646
if (string.IsNullOrEmpty(cacheOptions.TableName))
4747
{
4848
throw new ArgumentException(

src/Pomelo.Extensions.Caching.MySql/MySqlCacheOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ MySqlCacheOptions IOptions<MySqlCacheOptions>.Value
7272
{
7373
get
7474
{
75-
//correct empty conn strings if possible more inteligently
75+
//correct empty conn strings if possible more intelligently
7676
WriteConnectionString = WriteConnectionString ?? ConnectionString;
7777
ReadConnectionString = (ReadConnectionString ?? ConnectionString) ?? WriteConnectionString;
7878

src/Pomelo.Extensions.Caching.MySql/MySqlCacheServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static IServiceCollection AddDistributedMySqlCache(this IServiceCollectio
3939
}
4040

4141
// to enable unit testing
42-
/// TODO: make this internal again
4342
internal static void AddMySqlCacheServices(IServiceCollection services)
4443
{
4544
services.Add(ServiceDescriptor.Singleton<IDistributedCache, MySqlCache>());

src/Pomelo.Extensions.Caching.MySql/MySqlParameterCollectionExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
namespace Pomelo.Extensions.Caching.MySql
88
{
9-
/// <summary>
10-
/// TODO: make this class internal again
11-
/// </summary>
129
internal static class MySqlParameterCollectionExtensions
1310
{
1411
// For all values where the length is less than the below value, try setting the size of the

src/Pomelo.Extensions.Caching.MySql/MySqlQueries.cs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ namespace Pomelo.Extensions.Caching.MySql
55
{
66
internal class MySqlQueries
77
{
8-
private const string TableInfoFormat =
9-
"SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE " +
10-
"FROM INFORMATION_SCHEMA.TABLES " +
11-
"WHERE TABLE_SCHEMA = '{0}' " +
12-
"AND TABLE_NAME = '{1}'";
8+
//private const string TableInfoFormat =
9+
// "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE " +
10+
// "FROM INFORMATION_SCHEMA.TABLES " +
11+
// "WHERE TABLE_SCHEMA = '{0}' " +
12+
// "AND TABLE_NAME = '{1}'";
1313

1414
private const string UpdateCacheItemFormat =
1515
"UPDATE {0} " +
16-
"SET ExpiresAtTime = " +
16+
"SET " + Columns.Names.ExpiresAtTime + " = " +
1717
"(CASE " +
18-
"WHEN TIME_TO_SEC(TIMEDIFF(AbsoluteExpiration, @UtcNow)) <= SlidingExpirationInSeconds " +
19-
"THEN AbsoluteExpiration " +
18+
"WHEN TIME_TO_SEC(TIMEDIFF(" + Columns.Names.AbsoluteExpiration + ", @UtcNow)) <= " + Columns.Names.SlidingExpirationInSeconds + " " +
19+
"THEN " + Columns.Names.AbsoluteExpiration + " " +
2020
"ELSE " +
21-
"DATE_ADD(@UtcNow, INTERVAL SlidingExpirationInSeconds SECOND) " +
21+
"DATE_ADD(@UtcNow, INTERVAL " + Columns.Names.SlidingExpirationInSeconds + " SECOND) " +
2222
"END) " +
23-
"WHERE Id = @Id " +
24-
"AND @UtcNow <= ExpiresAtTime " +
25-
"AND SlidingExpirationInSeconds IS NOT NULL " +
26-
"AND (AbsoluteExpiration IS NULL OR AbsoluteExpiration <> ExpiresAtTime);";
23+
"WHERE " + Columns.Names.CacheItemId + " = @Id " +
24+
"AND @UtcNow <= " + Columns.Names.ExpiresAtTime + " " +
25+
"AND " + Columns.Names.SlidingExpirationInSeconds + " IS NOT NULL " +
26+
"AND (" + Columns.Names.AbsoluteExpiration + " IS NULL OR " + Columns.Names.AbsoluteExpiration + " <> " + Columns.Names.ExpiresAtTime + ");";
2727

2828
private const string GetCacheItemFormat =
29-
"SELECT Id, ExpiresAtTime, SlidingExpirationInSeconds, AbsoluteExpiration, Value " +
30-
"FROM {0} WHERE Id = @Id AND @UtcNow <= ExpiresAtTime;";
29+
"SELECT Id, " + Columns.Names.ExpiresAtTime + ", " + Columns.Names.SlidingExpirationInSeconds + ", " + Columns.Names.AbsoluteExpiration + ", " + Columns.Names.CacheItemValue + " " +
30+
"FROM {0} WHERE " + Columns.Names.CacheItemId + " = @Id AND @UtcNow <= " + Columns.Names.ExpiresAtTime + ";";
3131

3232
private const string SetCacheItemFormat =
3333
"SET @ExpiresAtTime = " +
@@ -37,33 +37,36 @@ internal class MySqlQueries
3737
"ELSE " +
3838
"DATE_ADD(@UtcNow, INTERVAL @SlidingExpirationInSeconds SECOND) " +
3939
"END);" +
40-
"INSERT INTO {0} " +
41-
"(Id, Value, ExpiresAtTime, SlidingExpirationInSeconds, AbsoluteExpiration) " +
40+
"INSERT INTO {0} (" + Columns.Names.CacheItemId + ", " + Columns.Names.CacheItemValue + ", " +
41+
Columns.Names.ExpiresAtTime + ", " + Columns.Names.SlidingExpirationInSeconds + ", " +
42+
Columns.Names.AbsoluteExpiration + ") " +
4243
"VALUES (@Id, @Value, @ExpiresAtTime, @SlidingExpirationInSeconds, @AbsoluteExpiration)" +
4344
" ON DUPLICATE KEY " +
44-
"UPDATE Value = @Value, ExpiresAtTime = @ExpiresAtTime," +
45-
"SlidingExpirationInSeconds = @SlidingExpirationInSeconds, AbsoluteExpiration = @AbsoluteExpiration " +
45+
"UPDATE " + Columns.Names.CacheItemValue + " = @Value, " + Columns.Names.ExpiresAtTime + " = @ExpiresAtTime," +
46+
Columns.Names.SlidingExpirationInSeconds + " = @SlidingExpirationInSeconds, " + Columns.Names.AbsoluteExpiration + " = @AbsoluteExpiration " +
4647
";";
4748

48-
private const string DeleteCacheItemFormat = "DELETE FROM {0} WHERE Id = @Id";
49+
private const string DeleteCacheItemFormat = "DELETE FROM {0} WHERE " + Columns.Names.CacheItemId + " = @Id";
4950

50-
public const string DeleteExpiredCacheItemsFormat = "DELETE FROM {0} WHERE @UtcNow > ExpiresAtTime";
51+
public const string DeleteExpiredCacheItemsFormat = "DELETE FROM {0} WHERE @UtcNow > " + Columns.Names.ExpiresAtTime;
5152

5253
public MySqlQueries(string schemaName, string tableName)
5354
{
54-
var tableNameWithSchema = string.Format(
55-
"{0}.{1}", EscapeIdentifier(DelimitIdentifier(schemaName)), EscapeIdentifier(DelimitIdentifier(tableName)));
55+
var tableNameWithSchema = string.Format("{0}{1}",
56+
(string.IsNullOrEmpty(schemaName) ? "" : EscapeIdentifier(DelimitIdentifier(schemaName)) + '.'),
57+
EscapeIdentifier(DelimitIdentifier(tableName))
58+
);
5659

5760
// when retrieving an item, we do an UPDATE first and then a SELECT
5861
GetCacheItem = string.Format(UpdateCacheItemFormat + GetCacheItemFormat, tableNameWithSchema);
5962
GetCacheItemWithoutValue = string.Format(UpdateCacheItemFormat, tableNameWithSchema);
6063
DeleteCacheItem = string.Format(DeleteCacheItemFormat, tableNameWithSchema);
6164
DeleteExpiredCacheItems = string.Format(DeleteExpiredCacheItemsFormat, tableNameWithSchema);
6265
SetCacheItem = string.Format(SetCacheItemFormat, tableNameWithSchema);
63-
TableInfo = string.Format(TableInfoFormat, EscapeLiteral(schemaName), EscapeLiteral(tableName));
66+
//TableInfo = string.Format(TableInfoFormat, EscapeLiteral(schemaName), EscapeLiteral(tableName));
6467
}
6568

66-
public string TableInfo { get; }
69+
//public string TableInfo { get; }
6770

6871
public string GetCacheItem { get; }
6972

src/Pomelo.Extensions.Caching.MySql/Pomelo.Extensions.Caching.MySql.csproj

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@
1111
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
1212
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1313
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
14-
<Version>2.1.4</Version>
14+
<Version>2.2.0</Version>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1616
<SignAssembly>true</SignAssembly>
1717
<AssemblyOriginatorKeyFile>Pomelo.snk</AssemblyOriginatorKeyFile>
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>
19-
</PropertyGroup>
19+
<PackageReadmeFile>README.md</PackageReadmeFile>
20+
</PropertyGroup>
2021

2122
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
2223
<DefineConstants>DEBUG;TRACE</DefineConstants>
2324
</PropertyGroup>
2425

2526
<ItemGroup>
26-
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
27-
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
28-
<PackageReference Include="MySqlConnector" Version="2.2.5" />
27+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
32+
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
33+
<PackageReference Include="MySqlConnector" Version="2.3.7" />
2934
</ItemGroup>
3035

3136
</Project>

src/Pomelo.Extensions.Caching.MySqlConfig.Tools/Pomelo.Extensions.Caching.MySqlConfig.Tools.csproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Command line tool to create tables and indexes in a MySql Server database for distributed caching.</Description>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<NoWarn>$(NoWarn);CS1591</NoWarn>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -18,16 +18,17 @@
1818
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
1919
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
2020
<RootNamespace>Pomelo.Extensions.Caching.MySqlConfig.Tools</RootNamespace>
21-
<Version>2.1.1</Version>
21+
<Version>2.2.0</Version>
2222
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2323
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
24-
<AssemblyVersion>2.1.1.0</AssemblyVersion>
24+
<AssemblyVersion>2.2.0.0</AssemblyVersion>
2525
<IsPackable>true</IsPackable>
2626
<ToolCommandName>dotnet-mysql-cache</ToolCommandName>
2727
<PackAsTool>true</PackAsTool>
2828
<SignAssembly>true</SignAssembly>
2929
<AssemblyOriginatorKeyFile>Pomelo.snk</AssemblyOriginatorKeyFile>
3030
<PackageLicenseExpression>MIT</PackageLicenseExpression>
31+
<PackageReadmeFile>README.md</PackageReadmeFile>
3132
</PropertyGroup>
3233

3334
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -36,9 +37,13 @@
3637
<DocumentationFile>obj\Debug\dotnet-mysql-cache.xml</DocumentationFile>
3738
</PropertyGroup>
3839

40+
<ItemGroup>
41+
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
42+
</ItemGroup>
43+
3944
<ItemGroup>
4045
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
41-
<PackageReference Include="MySqlConnector" Version="2.2.5" />
46+
<PackageReference Include="MySqlConnector" Version="2.3.7" />
4247
</ItemGroup>
4348

4449
</Project>

src/Pomelo.Extensions.Caching.MySqlConfig.Tools/Program.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ public int Run(string[] args)
6363

6464
cliApp.Command("create", command =>
6565
{
66-
command.Error = Error;//internal command Out/Erro are not yet changed, possible shortcomming
66+
command.Error = Error;//internal command Out/Error are not yet changed, possible shortcoming
6767
command.Out = Out;
6868

6969
command.Description = description;
7070
var connectionStringArg = command.Argument(
7171
"[connectionString]",
7272
"The connection string to connect to the database.");
73-
var databaseNameArg = command.Argument("[databaseName]", "Name of the database.");
73+
//var databaseNameArg = command.Argument("[databaseName]", "Name of the database.");
74+
var databaseNameOpt = command.Option("-d|--databaseName", "Name of the database. If not existing or set in connection string.", CommandOptionType.SingleValue);
7475
var tableNameArg = command.Argument("[tableName]", "Name of the table to be created.");
7576
command.HelpOption("-?|-h|--help");
7677

7778
command.OnExecute(async () =>
7879
{
7980
if (string.IsNullOrEmpty(connectionStringArg.Value)
80-
|| string.IsNullOrEmpty(databaseNameArg.Value)
8181
|| string.IsNullOrEmpty(tableNameArg.Value))
8282
{
8383
await Error.WriteLineAsync("Invalid input");
@@ -86,7 +86,7 @@ public int Run(string[] args)
8686
}
8787

8888
_connectionString = connectionStringArg.Value;
89-
_databaseName = databaseNameArg.Value;
89+
_databaseName = databaseNameOpt.Value();
9090
_tableName = tableNameArg.Value;
9191

9292
return await CreateTableAndIndexes();
@@ -95,25 +95,25 @@ public int Run(string[] args)
9595

9696
cliApp.Command("script", command =>
9797
{
98-
command.Error = Error;//internal command Out/Erro are not yet changed, possible shortcomming
98+
command.Error = Error;//internal command Out/Error are not yet changed, possible shortcoming
9999
command.Out = Out;
100100

101101
command.Description = "Generate creation script";
102-
var databaseNameArg = command.Argument("[databaseName]", "Name of the database.");
102+
//var databaseNameArg = command.Argument("[databaseName]", "Name of the database.");
103+
var databaseNameOpt = command.Option("-d|--databaseName", "Name of the database. If not existing or set in connection string.", CommandOptionType.SingleValue);
103104
var tableNameArg = command.Argument("[tableName]", "Name of the table to be created.");
104105
command.HelpOption("-?|-h|--help");
105106

106107
command.OnExecute(async () =>
107108
{
108-
if (string.IsNullOrEmpty(databaseNameArg.Value)
109-
|| string.IsNullOrEmpty(tableNameArg.Value))
109+
if (string.IsNullOrEmpty(tableNameArg.Value))
110110
{
111111
await Error.WriteLineAsync("Invalid input");
112112
cliApp.ShowHelp(command.Name);
113113
return 2;
114114
}
115115

116-
_databaseName = databaseNameArg.Value;
116+
_databaseName = databaseNameOpt.Value();
117117
_tableName = tableNameArg.Value;
118118

119119
return await GenerateScript();
@@ -169,12 +169,6 @@ public int Run(string[] args)
169169
await command.ExecuteNonQueryAsync(token);
170170
}
171171

172-
//using (var command = new MySqlCommand(sqlQueries.CreateNonClusteredIndexOnExpirationTime,
173-
// connection, transaction))
174-
//{
175-
// await command.ExecuteNonQueryAsync(token);
176-
//}
177-
178172
transaction.Commit();
179173

180174
await Out.WriteLineAsync("Table and index were created successfully.");

0 commit comments

Comments
 (0)