/// <summary> /// 设置指定key的过期时间 /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">key</param> /// <param name="expiry">过期时间</param> /// <returns></returns> public static bool KeyExpire(string nodeName, string logicName, string key, TimeSpan?expiry) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); return(cfg.Set(db => db.KeyExpire(k, expiry))); }
/// <summary> /// 保存一个string value /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="expiry">过期时间</param> /// <returns>bool</returns> public static bool Set(string nodeName, string logicName, string key, string value, TimeSpan?expiry = default(TimeSpan?)) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); return(cfg.Set(db => db.StringSet(k, value, expiry))); }
/// <summary> /// 移除某个key的值 /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">key</param> /// <returns>bool</returns> public static bool RemoveKey(string nodeName, string logicName, string key) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); return(cfg.Set(db => db.KeyDelete(k))); }
/// <summary> /// 移除指定key值 /// </summary> /// <param name="nodeName">nodeName</param> /// <param name="logicName">logicName</param> /// <param name="key">key</param> /// <param name="filed">filed</param> public static void RemoveHashKey(string nodeName, string logicName, string key, string filed) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); cfg.Set(db => { return(db.HashDelete(k, filed)); }); }
/// <summary> /// 存放key为Obj的对象到模型nodeName下面 /// </summary> /// <typeparam name="T">要存储的对象Type</typeparam> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">key</param> /// <param name="obj">要存储的对象</param> /// <param name="expiry">过期时间,一般不指定,使用默认</param> /// <returns>bool</returns> public static bool Set <T>(string nodeName, string logicName, string key, T obj, TimeSpan?expiry = default(TimeSpan?)) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); string json = ConvertJson(obj); return(cfg.Set(db => db.StringSet(k, json, expiry))); }
/// <summary> /// 给一个指定的key字符串,增加value的计数 /// </summary> /// <param name="nodeName">nodeName</param> /// <param name="logicName">logicName</param> /// <param name="key">key</param> /// <param name="value">value</param> public static void StringIncrement(string nodeName, string logicName, string key, long value) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); cfg.Set(db => { db.StringIncrement(k, value); return(true); }); }
public static bool RestoreKey(string nodeName, string logicName, string key, byte[] obj, TimeSpan?expiry = default(TimeSpan?)) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); return(cfg.Set(db => { db.KeyRestore(k, obj, expiry); return true; })); }
/// <summary> /// 存储一个key value的HashSet /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">key</param> /// <param name="value">value</param> public static void SetHash <T>(string nodeName, string logicName, string key, KeyValuePair <string, T> value) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); HashEntry[] hash = new HashEntry[] { new HashEntry(value.Key, ConvertJson(value.Value)) }; cfg.Set(db => { db.HashSet(k, hash); return(true); }); }
/// <summary> /// Redis是单进程单线程,不存在并发问题,因此只要保证多条命令原则执行即可 /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">队列名称</param> /// <param name="value">值</param> /// <param name="queueSize">值</param> /// <returns>是否</returns> public static bool PushQueue(string nodeName, string logicName, string key, string value, int queueSize = 1000) { RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); return(cfg.Set(db => { var rtn = db.ListLeftPush(k, value); db.ListTrim(k, 0, queueSize); return true; })); }
/// <summary> /// 这个方法最好不要用,因为你在外部构建一个IDictionary需要循环一次, /// 扔里面来里面再循环一次,做了没有意义的事情,所以在外面就直接调用KeyValuePair的参数吧 /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="logicName">业务名称</param> /// <param name="key">key</param> /// <param name="values">values</param> public static void SetHash <T>(string nodeName, string logicName, string key, IDictionary <string, T> values) { if (values == null || values.Count < 1) { return; } //批量入库,一次写,速度更快 RedisNode cfg = RedisConfig.GetNode(nodeName); string k = getKey(logicName, key); cfg.Set(db => { db.HashSet(k, values.Select(x => new HashEntry(x.Key, ConvertJson(x.Value))).ToArray()); return(true); }); //foreach (KeyValuePair<string, T> kv in values) //{ // SetHash(nodeName, logicName, key, kv); //} }