Files
Yi.Admin/CC.Yi/CC.Yi.Common/Cache/RedisCache.cs

100 lines
2.2 KiB
C#
Raw Normal View History

2021-05-13 01:39:34 +08:00
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace CC.Yi.Common.Cache
{
public class RedisCache : ICacheWriter
{
private RedisClient client;
2021-06-02 20:00:25 +08:00
private string ip = "49.235.212.122";
private int port = 6379;
private string pwd = "Qz52013142020.";
2021-05-13 01:39:34 +08:00
public RedisCache()
{
2021-06-02 20:00:25 +08:00
}
public void Dispose()
{
client.Dispose();
2021-05-13 01:39:34 +08:00
}
public bool AddCache<T>(string key, T value, DateTime expDate)
{
2021-06-02 20:00:25 +08:00
try
{
using (client = new RedisClient(ip, port, pwd))
{
return client.Add<T>(key, value, expDate);
}
}
catch
{
return false;
}
2021-05-13 01:39:34 +08:00
}
public bool AddCache<T>(string key, T value)
{
2021-06-02 20:00:25 +08:00
return client.Add<T>(key, value);
2021-05-13 01:39:34 +08:00
}
public bool RemoveCache(string key)
{
2021-06-02 20:00:25 +08:00
return client.Remove(key);
2021-05-13 01:39:34 +08:00
}
public T GetCache<T>(string key)
{
2021-06-02 20:00:25 +08:00
try
{
using (client = new RedisClient(ip, port, pwd))
{
return client.Get<T>(key);
}
}
catch
{
object p = new object();
return (T)p;
}
2021-05-13 01:39:34 +08:00
}
2021-06-02 20:00:25 +08:00
public bool SetCache<T>(string key, T value, DateTime expDate)
2021-05-13 01:39:34 +08:00
{
2021-06-02 20:00:25 +08:00
try
{
using (client = new RedisClient(ip, port, pwd))
{
return client.Set<T>(key, value, expDate);
}
}
catch
{
return false;
}
2021-05-13 01:39:34 +08:00
}
public bool SetCache<T>(string key, T value)
{
2021-06-02 20:00:25 +08:00
try
{
using (client = new RedisClient(ip, port, pwd))
{
return client.Set<T>(key, value);
}
}
catch
{
object p = new object();
return false;
}
2021-05-13 01:39:34 +08:00
}
}
}