Update
This commit is contained in:
9
ConsoleApp2/Cache/CacheKeys.cs
Normal file
9
ConsoleApp2/Cache/CacheKeys.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace ConsoleApp2.Cache;
|
||||
|
||||
#nullable disable
|
||||
public static class CacheKeys
|
||||
{
|
||||
public static Func<string, string> Order_OrderNo_CompanyID { get; set; }
|
||||
public static Func<string, string> OrderBlockPlan_ID_CompanyID { get; set; }
|
||||
public static Func<string, string> OrderProcess_ID_ShardKey { get; set; }
|
||||
}
|
10
ConsoleApp2/Cache/ICacher.cs
Normal file
10
ConsoleApp2/Cache/ICacher.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace ConsoleApp2.Cache;
|
||||
|
||||
public interface ICacher
|
||||
{
|
||||
Task<string?> GetStringAsync(string key);
|
||||
Task SetStringAsync(string key, string value);
|
||||
Task<bool> ExistsAsync(string key);
|
||||
Task SetHashAsync(string key, IReadOnlyDictionary<string, string> hash);
|
||||
Task<Dictionary<string, string>> GetHashAsync(string key);
|
||||
}
|
63
ConsoleApp2/Cache/RedisCache.cs
Normal file
63
ConsoleApp2/Cache/RedisCache.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using ConsoleApp2.Options;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace ConsoleApp2.Cache;
|
||||
|
||||
public class RedisCache : ICacher
|
||||
{
|
||||
private readonly IDatabase _db;
|
||||
|
||||
public string KeyPrefix { get; set; }
|
||||
|
||||
public RedisCache(IConnectionMultiplexer conn, int dataBase, string keyPrefix = "")
|
||||
{
|
||||
_db = conn.GetDatabase(dataBase);
|
||||
KeyPrefix = keyPrefix;
|
||||
}
|
||||
|
||||
public async Task<string?> GetStringAsync(string key)
|
||||
{
|
||||
var value = await _db.StringGetAsync($"{KeyPrefix}{key}");
|
||||
return !value.HasValue ? null : value.ToString();
|
||||
}
|
||||
|
||||
public async Task SetStringAsync(string key, string value)
|
||||
{
|
||||
if (!await _db.StringSetAsync($"{KeyPrefix}{key}", value))
|
||||
throw new RedisCommandException("设置Redis缓存失败");
|
||||
}
|
||||
|
||||
public Task<bool> ExistsAsync(string key)
|
||||
{
|
||||
return _db.KeyExistsAsync($"{KeyPrefix}{key}");
|
||||
}
|
||||
|
||||
public Task SetHashAsync(string key, IReadOnlyDictionary<string, string> hash)
|
||||
{
|
||||
return _db.HashSetAsync($"{KeyPrefix}{key}", hash.Select(pair => new HashEntry(pair.Key, pair.Value)).ToArray());
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, string>> GetHashAsync(string key)
|
||||
{
|
||||
var entries = await _db.HashGetAllAsync($"{KeyPrefix}{key}");
|
||||
var result = new Dictionary<string, string>();
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
result.Add(entry.Name.ToString(), entry.Value.ToString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RedisCacheExtensions
|
||||
{
|
||||
public static IServiceCollection AddRedisCache(this IServiceCollection services, RedisCacheOptions options)
|
||||
{
|
||||
var conn = ConnectionMultiplexer.Connect(options.Configuration
|
||||
?? throw new ApplicationException("未配置Redis连接字符串"));
|
||||
services.AddSingleton<ICacher>(new RedisCache(conn, options.Database, options.InstanceName));
|
||||
return services;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user