v1.1.4
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using CC.Yi.Common;
|
||||
using CC.Yi.Common.Cache;
|
||||
using CC.Yi.IBLL;
|
||||
using CC.Yi.Model;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
@@ -49,6 +50,15 @@ namespace CC.Yi.API.Controllers
|
||||
// return Ok();
|
||||
//}
|
||||
|
||||
#region
|
||||
//下面,这里是操作reids
|
||||
#endregion
|
||||
[HttpGet]
|
||||
public Result GetReids()
|
||||
{
|
||||
var data = CacheHelper.CacheWriter.GetCache<string>("key01");
|
||||
return Result.Success(data);
|
||||
}
|
||||
|
||||
|
||||
#region
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.11.0" />
|
||||
<PackageReference Include="ServiceStack.Redis" Version="5.10.4" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -11,42 +11,40 @@ namespace CC.Yi.Common.Cache
|
||||
public static ICacheWriter CacheWriter { get; set; }
|
||||
static CacheHelper()
|
||||
{
|
||||
//这里存在一些问题
|
||||
ContainerBuilder containerBuilder = new ContainerBuilder();
|
||||
IContainer container = containerBuilder.Build();
|
||||
CacheHelper.CacheWriter = container.Resolve<ICacheWriter>();
|
||||
CacheHelper.CacheWriter = new RedisCache();
|
||||
}
|
||||
|
||||
public static void AddCache(string key, string value, int expDate)
|
||||
|
||||
|
||||
|
||||
public bool AddCache<T>(string key, T value, DateTime expDate)
|
||||
{
|
||||
CacheWriter.Add(key, value, expDate);
|
||||
return CacheWriter.AddCache<T>(key,value,expDate);
|
||||
}
|
||||
|
||||
//没有过期参数,表示用不过期
|
||||
public static void AddCache(string key, string value)
|
||||
public bool AddCache<T>(string key, T value)
|
||||
{
|
||||
CacheWriter.Add(key, value);
|
||||
return CacheWriter.AddCache<T>(key, value);
|
||||
}
|
||||
|
||||
public static object GetCache(string key)
|
||||
public bool RemoveCache(string key)
|
||||
{
|
||||
return CacheWriter.Get(key);
|
||||
}
|
||||
public static void SetCache(string key, string value, int expDate)
|
||||
{
|
||||
|
||||
CacheWriter.Replace(key, value, expDate);
|
||||
return CacheWriter.RemoveCache(key);
|
||||
}
|
||||
|
||||
//没有过期参数,表示用不过期
|
||||
public static void SetCache(string key, string value)
|
||||
public T GetCache<T>(string key)
|
||||
{
|
||||
CacheWriter.Replace(key, value);
|
||||
return CacheWriter.GetCache<T>(key);
|
||||
}
|
||||
|
||||
public static void Remove(string key)
|
||||
public bool SetCache<T>(string key, T value, DateTime expDate)
|
||||
{
|
||||
CacheWriter.Remove(key);
|
||||
return CacheWriter.SetCache<T>(key,value,expDate);
|
||||
}
|
||||
|
||||
public bool SetCache<T>(string key, T value)
|
||||
{
|
||||
return CacheWriter.SetCache<T>(key, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,24 +8,11 @@ namespace CC.Yi.Common.Cache
|
||||
{
|
||||
public interface ICacheWriter
|
||||
{
|
||||
//void AddCache(string key, object value, DateTime expDate);
|
||||
//void AddCache(string key, object value);
|
||||
//object GetCache(string key);
|
||||
//T GetCache<T>(string key);
|
||||
//void SetCache(string key, object value, DateTime expDate);
|
||||
//void SetCache(string key, object value);
|
||||
//bool Delete(string key);
|
||||
|
||||
string Get(string key);
|
||||
|
||||
string GetString(string key);
|
||||
|
||||
void AddString(string key, string value);
|
||||
|
||||
void Add(string key, string value, int ExpirationTime = 20);
|
||||
|
||||
void Remove(string key);
|
||||
|
||||
void Replace(string key, string value, int ExpirationTime = 20);
|
||||
bool AddCache<T>(string key, T value, DateTime expDate);
|
||||
bool AddCache<T>(string key, T value);
|
||||
bool RemoveCache(string key);
|
||||
T GetCache<T>(string key);
|
||||
bool SetCache<T>(string key, T value, DateTime expDate);
|
||||
bool SetCache<T>(string key, T value);
|
||||
}
|
||||
}
|
||||
|
||||
46
CC.Yi.Common/Cache/RedisCache.cs
Normal file
46
CC.Yi.Common/Cache/RedisCache.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using ServiceStack.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CC.Yi.Common.Cache
|
||||
{
|
||||
public class RedisCache : ICacheWriter
|
||||
{
|
||||
private RedisClient client;
|
||||
public RedisCache()
|
||||
{
|
||||
client = new RedisClient("127.0.0.1", 6379, "52013142020.");
|
||||
}
|
||||
|
||||
public bool AddCache<T>(string key, T value, DateTime expDate)
|
||||
{
|
||||
return client.Add<T>(key, value, expDate);
|
||||
}
|
||||
|
||||
public bool AddCache<T>(string key, T value)
|
||||
{
|
||||
return client.Add<T>(key, value);
|
||||
}
|
||||
|
||||
public bool RemoveCache(string key)
|
||||
{
|
||||
return client.Remove(key);
|
||||
}
|
||||
|
||||
public T GetCache<T>(string key)
|
||||
{
|
||||
return client.Get<T>(key);
|
||||
}
|
||||
|
||||
public bool SetCache<T>(string key,T value, DateTime expDate)
|
||||
{
|
||||
return client.Set<T>(key, value, expDate);
|
||||
}
|
||||
|
||||
public bool SetCache<T>(string key, T value)
|
||||
{
|
||||
return client.Set<T>(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Caching.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CC.Yi.Common.Cache
|
||||
{
|
||||
public class RedisCacheService :ICacheWriter
|
||||
{
|
||||
private RedisCache _redisCache = null;
|
||||
public RedisCacheService(RedisCacheOptions options)
|
||||
{
|
||||
_redisCache = new RedisCache(options);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns></returns>
|
||||
public string Get(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
return Encoding.UTF8.GetString(_redisCache.Get(key));
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="value">缓存值</param>
|
||||
/// <param name="ExpirationTime">绝对过期时间(分钟)</param>
|
||||
public void Add(string key, string value, int ExpirationTime = 20)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
_redisCache.Set(key, Encoding.UTF8.GetBytes(value), new DistributedCacheEntryOptions()
|
||||
{
|
||||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirationTime)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void AddString(string key, string value)
|
||||
{
|
||||
_redisCache.SetString(key, value);
|
||||
}
|
||||
|
||||
public string GetString(string key)
|
||||
|
||||
{
|
||||
return _redisCache.GetString(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
public void Remove(string key)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
_redisCache.Remove(key);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="value">缓存值</param>
|
||||
/// <param name="ExpirationTime"></param>
|
||||
public void Replace(string key, string value, int ExpirationTime = 20)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
_redisCache.Remove(key);
|
||||
_redisCache.Set(key, Encoding.UTF8.GetBytes(value), new DistributedCacheEntryOptions()
|
||||
{
|
||||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirationTime)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user