using Furion;
|
using SqlSugar;
|
using SugarRedis;
|
|
//using SugarRedis;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace cylsg.Core
|
{
|
public class SqlSugarRedisCache : ICacheService
|
{
|
|
//NUGET安装 SugarRedis (也可以自个实现)
|
//注意:SugarRedis 不要扔到构造函数里面, 一定要单例模式
|
public static SugarRedisClient _service = new SugarRedisClient(App.Configuration["RedisConfig:ConnectionString"] ?? "127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0");
|
|
|
//+1重载 new SugarRedisClient(字符串)
|
//默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0
|
|
public void Add<V>(string key, V value)
|
{
|
_service.Set(key, value);
|
}
|
|
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
{
|
_service.SetBySeconds(key, value, cacheDurationInSeconds);
|
}
|
|
public bool ContainsKey<V>(string key)
|
{
|
return _service.Exists(key);
|
}
|
|
public V Get<V>(string key)
|
{
|
return _service.Get<V>(key);
|
}
|
|
public IEnumerable<string> GetAllKey<V>()
|
{
|
//性能注意: 只查sqlsugar用到的key
|
return _service.SearchCacheRegex("SqlSugarDataCache.*");
|
}
|
|
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
|
{
|
|
if (this.ContainsKey<V>(cacheKey))
|
{
|
var result = this.Get<V>(cacheKey);
|
if (result == null)
|
{
|
return create();
|
}
|
else
|
{
|
return result;
|
}
|
}
|
else
|
{
|
var result = create();
|
this.Add(cacheKey, result, cacheDurationInSeconds);
|
return result;
|
}
|
}
|
|
public void Remove<V>(string key)
|
{
|
_service.Remove(key);
|
}
|
}
|
}
|