mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Redis Cache
This commit is contained in:
parent
0b70d7966d
commit
e0f0b3d69e
@ -1,12 +1,170 @@
|
|||||||
using System;
|
using ServiceStack.Redis;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using SqlSugar;
|
||||||
namespace SqlSugar.Extensions
|
namespace SqlSugar.Extensions
|
||||||
{
|
{
|
||||||
public class RedisCache
|
public class RedisCache : ICacheService
|
||||||
{
|
{
|
||||||
|
ServiceStackRedis service = null;
|
||||||
|
public RedisCache(string host, int port, string password, int expirySeconds, long db)
|
||||||
|
{
|
||||||
|
service = new ServiceStackRedis(host, port, password, expirySeconds, db);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisCache(string host)
|
||||||
|
{
|
||||||
|
service = new ServiceStackRedis(host);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RedisCache()
|
||||||
|
{
|
||||||
|
service = new ServiceStackRedis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add<V>(string key, V value)
|
||||||
|
{
|
||||||
|
service.Set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||||
|
{
|
||||||
|
service.Set(key, value,cacheDurationInSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsKey<V>(string key)
|
||||||
|
{
|
||||||
|
return service.ContainsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public V Get<V>(string key)
|
||||||
|
{
|
||||||
|
return service.Get<V>(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> GetAllKey<V>()
|
||||||
|
{
|
||||||
|
return service.GetAllKeys();
|
||||||
|
}
|
||||||
|
|
||||||
|
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
|
||||||
|
{
|
||||||
|
if (this.ContainsKey<V>(cacheKey))
|
||||||
|
{
|
||||||
|
return this.Get<V>(cacheKey);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var result = create();
|
||||||
|
this.Add(cacheKey, result, cacheDurationInSeconds);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove<V>(string key)
|
||||||
|
{
|
||||||
|
service.Remove(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
public class ServiceStackRedis
|
||||||
|
{
|
||||||
|
private readonly int _expirySeconds = -1;
|
||||||
|
private readonly PooledRedisClientManager _redisClientManager;
|
||||||
|
private readonly SerializeService _serializeService = new SerializeService();
|
||||||
|
public ServiceStackRedis(string host, int port, string password, int expirySeconds, long db)
|
||||||
|
{
|
||||||
|
_expirySeconds = expirySeconds;
|
||||||
|
var hosts = new[] { string.Format("{0}@{1}:{2}", password, host, port) };
|
||||||
|
_redisClientManager = new PooledRedisClientManager(hosts, hosts, null, db, 500, _expirySeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceStackRedis(string host)
|
||||||
|
: this(host, 6379, null, -1, 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceStackRedis()
|
||||||
|
: this("localhost", 6379, null, -1, 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Set(string key, object value)
|
||||||
|
{
|
||||||
|
if (key == null) throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
if (_expirySeconds != -1) return Set(key, value, _expirySeconds);
|
||||||
|
|
||||||
|
var json = _serializeService.SerializeObject(value);
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
return client.Set(key, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Set(string key, object value, int duration)
|
||||||
|
{
|
||||||
|
if (key == null) throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
var json = _serializeService.SerializeObject(value);
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
return client.Set(key, json, DateTime.Now.AddSeconds(duration));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get<T>(string key)
|
||||||
|
{
|
||||||
|
if (key == null) throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
string data;
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
data = client.Get<string>(key);
|
||||||
|
}
|
||||||
|
return data == null ? default(T) : _serializeService.DeserializeObject<T>(data);
|
||||||
|
}
|
||||||
|
public bool Remove(string key)
|
||||||
|
{
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
return client.Remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveAll()
|
||||||
|
{
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.FlushDb();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsKey(string key)
|
||||||
|
{
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
return client.ContainsKey(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> GetAllKeys()
|
||||||
|
{
|
||||||
|
using (var client = _redisClientManager.GetClient())
|
||||||
|
{
|
||||||
|
return client.SearchKeys("SqlSugarDataCache");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,22 @@
|
|||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="ServiceStack.Common, Version=4.5.14.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\ServiceStack.Common.4.5.14\lib\net45\ServiceStack.Common.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="ServiceStack.Interfaces, Version=4.0.0.0, Culture=neutral, PublicKeyToken=e06fbc6124f57c43, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\ServiceStack.Interfaces.4.5.14\lib\portable-wp80+sl5+net45+win8+wpa81+monotouch+monoandroid+xamarin.ios10\ServiceStack.Interfaces.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="ServiceStack.Redis, Version=4.5.14.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\ServiceStack.Redis.4.5.14\lib\net45\ServiceStack.Redis.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="ServiceStack.Text, Version=4.5.14.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\ServiceStack.Text.4.5.14\lib\net45\ServiceStack.Text.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Web" />
|
<Reference Include="System.Web" />
|
||||||
@ -51,6 +67,9 @@
|
|||||||
<Name>SqlSugar</Name>
|
<Name>SqlSugar</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="packages.config" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="ServiceStack.Common" version="4.5.14" targetFramework="net452" />
|
||||||
|
<package id="ServiceStack.Interfaces" version="4.5.14" targetFramework="net452" />
|
||||||
|
<package id="ServiceStack.Redis" version="4.5.14" targetFramework="net452" />
|
||||||
|
<package id="ServiceStack.Text" version="4.5.14" targetFramework="net452" />
|
||||||
|
</packages>
|
Loading…
Reference in New Issue
Block a user