Exemple #1
0
        /// <summary>
        /// 获取字符串对象
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="logicName">业务名称</param>
        /// <param name="key">key</param>
        /// <returns>string</returns>
        public static string Get(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <string>(db => db.StringGet(k)));
        }
Exemple #2
0
        /// <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)));
        }
Exemple #3
0
        /// <summary>
        /// 清除某个业务模型下面的所有节点
        /// </summary>
        /// <param name="nodeName">业务模型名称</param>
        /// <returns>bool</returns>
        // public static bool FlushAll(string nodeName,string logicName)
        // {
        //    RedisNode cfg = RedisConfig.GetNode(nodeName);
        //    return cfg.Set(db => db.KeyDelete(nodeName));
        // }

        /// <summary>
        /// 查询指定的key是否存在
        /// </summary>
        /// <param name="nodeName"></param>
        /// <param name="logicName">logicName</param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool ContainsKey(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <bool>(db => db.KeyExists(k)));
        }
Exemple #4
0
        /// <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)));
        }
Exemple #5
0
        /// <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)));
        }
Exemple #6
0
        /// <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)); });
        }
Exemple #7
0
        /// <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); });
        }
Exemple #8
0
        /// <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)));
        }
Exemple #9
0
        // private static readonly string localConfig = "<RedisConfig><Node NodeName=\"default\" IsMasterSlave=\"false\"><Item pool = \"0\" IsMaster=\"false\" server=\"{0}\" port=\"{1}\" allowadmin=\"true\" connectTimeout=\"0\" ssl=\"false\" pwd=\"\"  /></Node></RedisConfig>";

        /// <summary>
        /// 初始化服务器配置
        /// </summary>
        /// <returns>服务器配置列表</returns>
        // public static Dictionary<string,RedisNode> InitlizeServerList(XElement root)
        // {
        //    if (root != null && root.Element("RedisConfig") != null &&
        //        root.Element("RedisConfig").Elements("Node") != null)
        //    {
        //        IEnumerable nodes =
        //            from em in root.Element("RedisConfig").Elements("Node")
        //            select em;
        //        foreach (XElement it in nodes)
        //        {
        //            if (it.Elements("Item") == null)
        //            {
        //                continue;
        //            }
        //            RedisNode node = new RedisNode()
        //            {
        //                NodeName = it.Attribute("NodeName").Value.ToLower(),
        //                IsMasterSlave = it.Attribute("IsMasterSlave").Value == "true" ? true : false,
        //            };
        //            if (redisNodes.ContainsKey(node.NodeName))
        //            {
        //                Log.GrantLogTextWriter.Write(new Exception(string.Format("不能配置业务节点同名的redis服务器,{0}节点的配置将被忽略", node.NodeName)));
        //                continue;
        //            }
        //            redisNodes[node.NodeName] = node;
        //            IEnumerable srvs = from sr in it.Elements("Item")
        //                               select sr;
        //            var servers = new List<RedisServer>();
        //            foreach (XElement s in srvs)
        //            {
        //                var srv = new RedisServer
        //                {
        //                    Node = node,
        //                    IsMaster = s.Attribute("IsMaster").Value == "true" ? true : false,
        //                    Pool = int.Parse(s.Attribute("pool").Value),
        //                    ConnectTimeout = int.Parse(s.Attribute("connectTimeout").Value),
        //                    Server = s.Attribute("server").Value,
        //                    Port = int.Parse(s.Attribute("port").Value),
        //                    AllowAdmin = s.Attribute("allowadmin").Value == "true" ? true : false,
        //                    Ssl = s.Attribute("ssl").Value == "true" ? true : false,
        //                    Pwd = s.Attribute("pwd").Value,
        //                };
        //                if (node.IsMasterSlave && srv.IsMaster)
        //                {
        //                    RedisConnectionManager.GetConnection(srv); // 提前预初始化
        //                    node.MasterServer = srv; // 如果配置了多个主,这里只会把最后一个做为主
        //                }
        //                else
        //                {
        //                    servers.Add(srv); // 如果是非主从模式,将所有的都加到从上,没有主次
        //                    RedisConnectionManager.GetConnection(srv);
        //                }
        //            }
        //            node.SlaveServers = servers.ToArray();
        //        }
        //        return redisNodes;
        //    }
        //    throw new Exception("无法解析RedisConfig配置");
        //    // else
        //    //    Log.LogEx.LogError("redis clinet config is null");
        // }
        public static Dictionary <string, RedisNode> InitlizeServerList(Config.RedisConfig config)
        {
            if (config != null && config.Nodes != null && config.Nodes.Count > 0)
            {
                foreach (Config.RedisNode it in config.Nodes)
                {
                    RedisNode node = new RedisNode()
                    {
                        NodeName      = it.NodeName.ToLower(),
                        IsMasterSlave = it.IsMasterSlave,
                    };

                    // 这里的初始化方法需要满足 重复调用. 所以注释掉这句, 覆盖原来的配置
                    //if (redisNodes.ContainsKey(node.NodeName))
                    //{
                    //    Log.GrantLogTextWriter.Write(new Exception(string.Format("不能配置业务节点同名的redis服务器,{0}节点的配置将被忽略", node.NodeName)));
                    //    continue;
                    //}
                    redisNodes[node.NodeName] = node;
                    var servers = new List <RedisServer>();
                    foreach (Config.RedisItem sItem in it.Items)
                    {
                        var srv = new RedisServer
                        {
                            Node           = node,
                            IsMaster       = sItem.IsMaster,
                            Pool           = sItem.Pool,
                            ConnectTimeout = sItem.ConnectTimeout,
                            Server         = sItem.Server,
                            Port           = sItem.Port,
                            AllowAdmin     = sItem.AllowAdmin,
                            Ssl            = sItem.Ssl,
                            Ssl2           = sItem.Ssl2,
                            Pwd            = sItem.Pwd,
                            DbIndex        = sItem.DbIndex,
                        };
                        if (node.IsMasterSlave && srv.IsMaster)
                        {
                            RedisConnectionManager.GetConnection(srv); // 提前预初始化
                            node.MasterServer = srv;                   // 如果配置了多个主,这里只会把最后一个做为主
                        }
                        else
                        {
                            servers.Add(srv); // 如果是非主从模式,将所有的都加到从上,没有主次
                            RedisConnectionManager.GetConnection(srv);
                        }
                    }

                    node.SlaveServers = servers.ToArray();
                }

                return(redisNodes);
            }

            throw new Exception("无法解析RedisConfig配置");
        }
Exemple #10
0
        public static bool LockRelease(string nodeName, string logicName, string key, string value)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <bool>(db =>
            {
                return db.LockRelease(k, value, CommandFlags.FireAndForget);
            }));
        }
Exemple #11
0
        public static bool LockTake(string nodeName, string logicName, string key, string value, TimeSpan expiry)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <bool>(db =>
            {
                return db.LockTake(k, value, expiry);
            }));
        }
Exemple #12
0
        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;
            }));
        }
Exemple #13
0
        public static string LockQuery(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <string>(db =>
            {
                return db.LockQuery(k);
            }));
        }
Exemple #14
0
        public static byte[] DumpKey(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get(db =>
            {
                return db.KeyDump(k);
            }));
        }
Exemple #15
0
        /// <summary>
        /// 弹出 最大的日志信息
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="logicName">业务名称</param>
        /// <param name="key">队列名称</param>
        /// <returns>最大数量日志</returns>
        public static List <string> PopAllQueue(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <List <string> >(db =>
            {
                var list = db.ListRange(k);
                return list?.Select(x => x.ToString()).ToList();
            }));
        }
Exemple #16
0
        /// <summary>
        /// 获取某个模型下面的特定key的值
        /// </summary>
        /// <typeparam name="T">存储对象</typeparam>
        /// <param name="nodeName">节点名称</param>
        /// <param name="logicName">业务名称</param>
        /// <param name="key">key</param>
        /// <returns>T</returns>
        public static T Get <T>(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            return(cfg.Get <T>(db =>
            {
                RedisValue rv = db.StringGet(k);
                return ConvertValue <T>(rv);
            }));
        }
Exemple #17
0
        /// <summary>
        /// 获取Hash字段值
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="nodeName">nodeName</param>
        /// <param name="logicName">logicName</param>
        /// <param name="key">key</param>
        /// <param name="filed">filed</param>
        /// <returns>TT</returns>
        public static T GetHash <T>(string nodeName, string logicName, string key, string filed)
        {
            RedisNode  cfg = RedisConfig.GetNode(nodeName);
            string     k   = getKey(logicName, key);
            RedisValue v   = cfg.Get <RedisValue>(db =>
            {
                return(db.HashGet(k, filed));
            });

            return(ConvertValue <T>(v));
        }
Exemple #18
0
        /// <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);
            });
        }
Exemple #19
0
        /// <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;
            }));
        }
Exemple #20
0
        /// <summary>
        /// 获取整个Hash表
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="nodeName">nodeName</param>
        /// <param name="logicName">logicName</param>
        /// <param name="key">key</param>
        /// <returns>static</returns>
        public static Dictionary <string, T> GetHashAll <T>(string nodeName, string logicName, string key)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);
            string    k   = getKey(logicName, key);

            HashEntry[] vs = cfg.Get <HashEntry[]>(db =>
            {
                return(db.HashGetAll(k));
            });
            if (vs == null || vs.Length < 1)
            {
                return(null);
            }

            return(vs.ToDictionary(a => a.Name.ToString(), a => ConvertValue <T>(a.Value)));
        }
Exemple #21
0
        /// <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);
            //}
        }
Exemple #22
0
        /// <summary>
        /// 是哦夫存在节点配置
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public static bool IsExistCfg(string nodeName)
        {
            RedisNode cfg = RedisConfig.GetNode(nodeName);

            return(cfg != null);
        }