概述
private static string redisHost = ConfigurationManager.AppSettings["RedisServerHost"];
private static string pwd = ConfigurationManager.AppSettings["RedisServerPassword"];
private static readonly object syncObject = new object();
private static ConnectionMultiplexer _redis;
public static ConnectionMultiplexer RedisManager
{
get
{
if (_redis == null)
{
lock (syncObject)
{
if (_redis == null)
{
if (string.IsNullOrWhiteSpace(pwd))
_redis = ConnectionMultiplexer.Connect(redisHost + ",syncTimeout=5000");
else
_redis = ConnectionMultiplexer.Connect(redisHost + ",password=" + pwd + ",syncTimeout=5000");
}
else
{
return _redis;
}
}
}
return _redis;
}
}
///设置单体,有过期时间的
public static bool String_Set<T>(string key, T t, DateTime? expiry = default(DateTime?))
{
if (string.IsNullOrWhiteSpace(key))
return false;
var json = JsonConvert.SerializeObject(t);
IDatabase dataBase = RedisManager.GetDatabase();
bool flag = dataBase.StringSet(key, json);
if (expiry != default(DateTime?))
dataBase.KeyExpire(key, expiry);
return flag;
}
// 获取单体
public static T String_Get<T>(string key) where T : class
{
IDatabase dataBase = RedisManager.GetDatabase();
if (!dataBase.KeyExists(key))
return null;
var res = dataBase.StringGet(key);
if (!res.HasValue)
return null;
T model = JsonConvert.DeserializeObject<T>(res);
return model;
}
//移除单体
public static bool String_Remove(string key)
{
bool flag = false;
IDatabase dataBase = RedisManager.GetDatabase();
if (dataBase.KeyExists(key))
flag = dataBase.KeyDelete(key);
return flag;
}
// list 集合,添加单个对象
public static void List_Add<T>(string key, T model, DateTime? expiry = default(DateTime?))
{
if (model != null)
{
IDatabase dataBase = RedisManager.GetDatabase();
RedisValue rv = JsonConvert.SerializeObject(model);
dataBase.ListRightPush(key, rv);
if (expiry != default(DateTime?))
dataBase.KeyExpire(key, expiry);
}
}
/// 根据Key获取list集合
public static List<T> List_GetList<T>(string key)
{
IDatabase dataBase = RedisManager.GetDatabase();
if (dataBase.KeyExists(key))
{
List<T> list = new List<T>();
RedisValue[] rv = dataBase.ListRange(key);
rv.ToList().ForEach(t =>
{
list.Add(JsonConvert.DeserializeObject<T>(t));
});
return list;
}
else
return null;
}
// 存储数据到hash表
public static bool Hash_Set<T>(string key, string dataKey, T model, DateTime? expiry = default(DateTime?))
{
if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(dataKey))
return false;
string json = JsonConvert.SerializeObject(model);
HashEntry[] upValues = new HashEntry[] { new HashEntry(dataKey, json) };
IDatabase dataBase = RedisManager.GetDatabase();
dataBase.HashSet(key, upValues);
if (expiry != default(DateTime?))
dataBase.KeyExpire(key, expiry);
return true;
}
// 移除hash中的某值
public static bool Hash_Remove(string key, string dataKey)
{
IDatabase dataBase = RedisManager.GetDatabase();
if (dataBase.HashExists(key, dataKey))
{
bool flag = dataBase.HashDelete(key, dataKey);
return flag;
}
else
return true;
}
public static void HashBatchSet<T>(string key,List<T> list)
{
IDatabase db = RedisManager.GetDatabase();
var batch = db.CreateBatch();//批量操作
foreach (var item in list)
{
string json = JsonConvert.SerializeObject(item);
HashEntry[] upValues = new HashEntry[] { new HashEntry("aaabbcc", json) };
batch.HashSetAsync(key, upValues);
}
batch.Execute();
}
最后
以上就是风中荷花为你收集整理的Redis相关的全部内容,希望文章能够帮你解决Redis相关所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复