using Furion.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Magic.Core;
///
/// Redis缓存
///
public class RedisCache : ICache, ISingleton
{
public RedisCache(IOptions cacheOptions)
{
var csredis = new CSRedis.CSRedisClient(cacheOptions.Value.RedisConnectionString);
RedisHelper.Initialization(csredis);
}
public long Del(params string[] key)
{
return RedisHelper.Del(key);
}
public Task DelAsync(params string[] key)
{
return RedisHelper.DelAsync(key);
}
public async Task DelByPatternAsync(string pattern)
{
if (string.IsNullOrEmpty(pattern))
return default;
//pattern = Regex.Replace(pattern, @"\{.*\}", "*");
var keys = (await RedisHelper.KeysAsync(pattern + "*"));
if (keys != null && keys.Length > 0)
{
return await RedisHelper.DelAsync(keys);
}
return default;
}
public bool Exists(string key)
{
return RedisHelper.Exists(key);
}
public Task ExistsAsync(string key)
{
return RedisHelper.ExistsAsync(key);
}
public string Get(string key)
{
return RedisHelper.Get(key);
}
public T Get(string key)
{
return RedisHelper.Get(key);
}
public Task GetAsync(string key)
{
return RedisHelper.GetAsync(key);
}
public Task GetAsync(string key)
{
return RedisHelper.GetAsync(key);
}
public bool Set(string key, object value)
{
return RedisHelper.Set(key, value);
}
public bool Set(string key, object value, TimeSpan expire)
{
return RedisHelper.Set(key, value, expire);
}
public Task SetAsync(string key, object value)
{
return RedisHelper.SetAsync(key, value);
}
public Task SetAsync(string key, object value, TimeSpan expire)
{
return RedisHelper.SetAsync(key, value, expire);
}
public List GetAllKeys()
{
return RedisHelper.Keys("*").ToList();
}
}