Redis相关
来源:互联网 发布:android 电子书源码 编辑:程序博客网 时间:2024/05/19 12:27
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;
}
- Redis相关
- redis相关
- redis相关
- Redis相关
- redis相关
- Redis相关
- redis相关
- redis相关
- Redis相关
- Redis相关
- Redis相关
- redis相关
- redis相关
- redis相关
- Redis相关
- Redis 内存相关
- redis配置文件相关
- Redis 相关资源
- java+xstream实现xml序列化(XStream轻松转换xml和java对象)
- Hibernate 映射实体Bean的关联关系
- LeetCode Wildcard Matching
- BOS项目中出现的错误以及解决方案
- eclipse 中安装spring tool suite 插件100%成功率
- Redis相关
- leetcode_middle_22_12. Integer to Roman
- 使用cryptography进行RSA加密
- MyBatis中多对多关系的映射和查询
- 网页访问IE8以下CLOSE浏览器
- mysql启动时报 Bind on unix socket Address already in use的问题处理
- tips使用
- 外部看门狗驱动模块支持-max706s
- Android硬件抽象层(HAL)深入剖析(二)