You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.2 KiB
C#
124 lines
3.2 KiB
C#
using CacheInterface;
|
|
using Google.Api;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using NewLife.Caching;
|
|
using Newtonsoft.Json.Linq;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CacheService
|
|
{
|
|
public class RedisCacheService : ICacheService
|
|
{
|
|
public RedisCacheService(IServiceProvider services, string url)
|
|
{
|
|
Services = services;
|
|
|
|
Logger = Services.GetRequiredService<ILogger<RedisCacheService>>();
|
|
|
|
Redis = new FullRedis();
|
|
|
|
Redis.Init(url);
|
|
|
|
Logger.LogDebug("加载Redis缓存服务");
|
|
}
|
|
|
|
private readonly IServiceProvider Services;
|
|
|
|
private readonly ILogger<RedisCacheService> Logger;
|
|
|
|
private readonly FullRedis Redis;
|
|
|
|
public Task Set<T>(string key, T value, int ttl = -1)
|
|
{
|
|
Logger.LogDebug($"设置缓存{key}:{JsonSerializer.Serialize(value)}");
|
|
|
|
if (value == null)
|
|
{
|
|
Redis.Remove(key);
|
|
}
|
|
else
|
|
{
|
|
Redis.Set(key, value, ttl);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task BatchSet<T>(Dictionary<string, T> values, int ttl = -1)
|
|
{
|
|
Logger.LogDebug($"批量设置缓存:{JsonSerializer.Serialize(values)}");
|
|
|
|
Redis.SetAll(values, ttl);
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<T> Get<T>(string key)
|
|
{
|
|
Logger.LogDebug($"读取缓存{key}");
|
|
|
|
T Result = Redis.Get<T>(key);
|
|
|
|
Logger.LogDebug($"{JsonSerializer.Serialize(Result)}");
|
|
|
|
return Task.FromResult(Result);
|
|
}
|
|
|
|
public Task<Dictionary<string, T>> BatchGet<T>(IEnumerable<string> keys)
|
|
{
|
|
Logger.LogDebug("批量读取缓存");
|
|
|
|
Dictionary<string, T> Result = (Dictionary<string, T>)Redis.GetAll<T>(keys);
|
|
|
|
Logger.LogDebug($"{JsonSerializer.Serialize(Result)}");
|
|
|
|
return Task.FromResult(Result);
|
|
}
|
|
|
|
public Task Delete(string key)
|
|
{
|
|
Logger.LogDebug($"删除缓存{key}");
|
|
|
|
Redis.Remove(key);
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task BatchDelete(params string[] keys)
|
|
{
|
|
Logger.LogDebug($"批量删除缓存{JsonSerializer.Serialize(keys)}");
|
|
|
|
Redis.Remove(keys);
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<Dictionary<string, T>> Query<T>(string query, int count = 9999)
|
|
{
|
|
Logger.LogDebug($"查询缓存{query}");
|
|
|
|
var Keys = Redis.Search(query, count);
|
|
|
|
var Result = new Dictionary<string, T>();
|
|
|
|
if (Keys.Count() > 0)
|
|
{
|
|
Result = (Dictionary<string, T>)Redis.GetAll<T>(Keys);
|
|
}
|
|
|
|
Logger.LogDebug($"查询缓存{query}:{JsonSerializer.Serialize(Result)}");
|
|
|
|
return Task.FromResult(Result);
|
|
}
|
|
}
|
|
}
|