Exemplo n.º 1
0
        public bool TrySearchRedisKeys(out List <string> keys, int top = 0)
        {
            string pattern = "*";

            if (_redisSearchPatternList.Count == 1)
            {
                pattern = _redisSearchPatternList[0];
            }
            List <string> list   = null;
            bool          result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                if (top > 0)
                {
                    list = _redisSearchPatternList.Count > 1
                    ? SearchKeys(client, _redisSearchPatternList).OrderBy(k => k).Take(top).ToList()
                    : client.SearchKeys(pattern).OrderBy(k => k).Take(top).ToList();
                }
                else
                {
                    list = _redisSearchPatternList.Count > 1
                    ? SearchKeys(client, _redisSearchPatternList).OrderBy(k => k).ToList()
                    : client.SearchKeys(pattern).OrderBy(k => k).ToList();
                }
                result = true;
            });
            keys = list;
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Check queue is completed.
        /// </summary>
        /// <param name="keyIndex"></param>
        /// <returns></returns>
        public static bool CheckCompleted(int keyIndex = 0)
        {
            var keys = new string[DataSyncQueueManager.SyncQueueCount];

            if (keys.Length == 1)
            {
                keys[0] = (DataSyncQueueManager.RedisSyncQueueKey);
            }
            else
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    keys[i] = string.Format("{0}:{1}", DataSyncQueueManager.RedisSyncQueueKey, i);
                }
            }

            bool result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                var values = client.MGet(keys);
                result     = values == null || values.Length == 0 || values.Any(t => t == null);
            });
            return(result);
        }
Exemplo n.º 3
0
 private static void LoadUnLineData()
 {
     try
     {
         RedisConnectionPool.ProcessReadOnly(client =>
         {
             byte[] data = client.Get(sessionRedisKey) ?? new byte[0];
             if (data.Length == 0)
             {
                 return;
             }
             var temp = ProtoBufUtils.Deserialize <ConcurrentDictionary <Guid, GameSession> >(data);
             if (temp != null)
             {
                 var paris = temp.Where(p =>
                 {
                     p.Value.UserId = 0;//reset userid
                     return(!p.Value.CheckExpired());
                 }).ToArray();
                 _globalSession = new ConcurrentDictionary <Guid, GameSession>(paris);
             }
         });
     }
     catch (Exception er)
     {
         TraceLog.WriteError("Load GameSession from redis faild,{0}", er);
     }
 }
Exemplo n.º 4
0
        public static void TestCheckSqlSyncQueue(int identity)
        {
            if (Interlocked.Exchange(ref _isWatchWorking[identity], 1) == 0)
            {
                try
                {
                    string   queueKey   = GetSqlQueueKey(identity);
                    string   workingKey = queueKey + "_temp";
                    bool     result;
                    byte[][] bufferBytes = new byte[0][];
                    do
                    {
                        result = false;
                        RedisConnectionPool.ProcessReadOnly(client =>
                        {
                            bool hasWorkingQueue    = client.ContainsKey(workingKey);
                            bool hasNewWorkingQueue = client.ContainsKey(queueKey);

                            if (!hasWorkingQueue && !hasNewWorkingQueue)
                            {
                                return;
                            }
                            if (!hasWorkingQueue)
                            {
                                try
                                {
                                    client.Rename(queueKey, workingKey);
                                }
                                catch { }
                            }

                            bufferBytes = client.ZRange(workingKey, 0, sqlSyncPackSize);
                            if (bufferBytes.Length > 0)
                            {
                                client.ZRemRangeByRank(workingKey, 0, sqlSyncPackSize);
                                result = true;
                            }
                            else
                            {
                                client.Remove(workingKey);
                            }
                        });
                        if (!result)
                        {
                            break;
                        }
                        DoProcessSqlSyncQueue(workingKey, bufferBytes);
                    } while (true);
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("OnCheckSqlSyncQueue error:{0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _isWatchWorking[identity], 0);
                }
            }
        }
Exemplo n.º 5
0
        private static void OnCheckSqlSyncQueue(object state)
        {
            int identity = (int)state;

            if (Interlocked.CompareExchange(ref _isWatchWorking[identity], 1, 0) == 0)
            {
                try
                {
                    string   queueKey   = GetSqlQueueKey(identity);
                    string   workingKey = queueKey + "_temp";
                    bool     result;
                    byte[][] bufferBytes = new byte[0][];
                    byte[][] valueBytes  = new byte[0][];
                    byte[][] scoreBytes  = new byte[0][];
                    do
                    {
                        result = false;
                        RedisConnectionPool.ProcessReadOnly(client =>
                        {
                            bool hasWorkingQueue    = client.ContainsKey(workingKey);
                            bool hasNewWorkingQueue = client.ContainsKey(queueKey);

                            if (!hasWorkingQueue && !hasNewWorkingQueue)
                            {
                                return;
                            }
                            if (!hasWorkingQueue)
                            {
                                try
                                {
                                    client.Rename(queueKey, workingKey);
                                }
                                catch { }
                            }
                            bufferBytes = GetPackFromQueue(client, workingKey, sqlSyncPackSize);

                            if (bufferBytes.Length > 0)
                            {
                                result = true;
                            }
                        });
                        if (!result)
                        {
                            break;
                        }
                        DoProcessSqlSyncQueue(workingKey, queueKey, bufferBytes);
                    } while (true);
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("OnCheckSqlSyncQueue error:{0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _isWatchWorking[identity], 0);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Check to be synchronized queue of redis
        /// </summary>
        /// <param name="state"></param>
        private static void OnCheckRedisSyncQueue(object state)
        {
            int identity = (int)state;

            if (Interlocked.CompareExchange(ref _isRedisSyncWorking[identity], 1, 0) == 0)
            {
                try
                {
                    string   queueKey   = GetRedisSyncQueueKey(identity);
                    string   workingKey = queueKey + "_temp";
                    byte[][] keys       = new byte[0][];
                    byte[][] values     = new byte[0][];
                    RedisConnectionPool.ProcessReadOnly(client =>
                    {
                        bool hasWorkingQueue    = client.HLen(workingKey) > 0;
                        bool hasNewWorkingQueue = client.HLen(queueKey) > 0;

                        if (!hasWorkingQueue && !hasNewWorkingQueue)
                        {
                            return;
                        }
                        if (!hasWorkingQueue)
                        {
                            try
                            {
                                client.Rename(queueKey, workingKey);
                            }
                            catch
                            {
                            }
                        }

                        var keyValuePairs = client.HGetAll(workingKey);
                        if (keyValuePairs != null && keyValuePairs.Length > 0)
                        {
                            keys   = keyValuePairs.Where((buffs, index) => index % 2 == 0).ToArray();
                            values = keyValuePairs.Where((buffs, index) => index % 2 == 1).ToArray();
                            client.Remove(workingKey);
                        }
                    });

                    if (keys != null && keys.Length > 0)
                    {
                        _threadPools.QueueWorkItem(DoProcessRedisSyncQueue, workingKey, keys, values);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("OnCheckRedisSyncQueue error:{0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _isRedisSyncWorking[identity], 0);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Check queue is completed.
        /// </summary>
        /// <param name="keyIndex"></param>
        /// <returns></returns>
        public static bool CheckCompleted(int keyIndex = 0)
        {
            bool result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                result = client.SearchKeys(DataSyncQueueManager.RedisSyncQueueKey + "*").Count == 0;
            });
            return(result);
        }
Exemplo n.º 8
0
        public bool TryRemoveRedisKey(string key)
        {
            bool result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                result = client.Remove(key);
            });
            return(result);
        }
Exemplo n.º 9
0
        public bool TryExecute(Func <RedisClient, bool> func)
        {
            bool result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                result = func(client);
            });
            return(result);
        }
Exemplo n.º 10
0
        public bool TryRemoveAllRedisKey()
        {
            bool result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                client.FlushDb();
                result = true;
            });
            return(result);
        }
Exemplo n.º 11
0
        public bool TryGetRedisKeyValue(string key, out int error, out string msg)
        {
            bool   result = false;
            int    e      = 0;
            string str    = "";

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                result = GetRedisKeyValue(client, key, out e, out str);
            });
            error = e;
            msg   = str;
            return(result);
        }
Exemplo n.º 12
0
        public bool TryRenameKey(IEnumerable <KeyValuePair <string, string> > keyPairs)
        {
            bool result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                foreach (var pair in keyPairs)
                {
                    client.RenameKey(pair.Key, pair.Value);
                }
                result = true;
            });
            return(result);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 从Redis内存移除,并保存到数据库,
        /// </summary>
        /// <param name="match">实体类型, 实体Key列表</param>
        public static void RemoveToDatabase(params KeyValuePair <Type, IList <string> >[] match)
        {
            var removeEntityKeys = new List <KeyValuePair <string, byte[][]> >();
            var entityList       = new List <EntityHistory>();

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                foreach (var express in match)
                {
                    try
                    {
                        string hashtId    = RedisConnectionPool.GetRedisEntityKeyName(express.Key);
                        byte[][] keyBytes = express.Value.Select(t => RedisConnectionPool.ToByteKey(t)).ToArray();
                        if (keyBytes.Length == 0)
                        {
                            continue;
                        }
                        removeEntityKeys.Add(new KeyValuePair <string, byte[][]>(hashtId, keyBytes));
                        //转存到DB使用protobuf
                        byte[][] valueBytes = client.HMGet(hashtId, keyBytes);
                        for (int i = 0; i < keyBytes.Length; i++)
                        {
                            entityList.Add(new EntityHistory()
                            {
                                Key   = string.Format("{0}_{1}", hashtId, RedisConnectionPool.ToStringKey(keyBytes[i])),
                                Value = valueBytes[i]
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("Redis cache remove key:{0} to Database error:{1}", express, ex);
                    }
                }
            });

            if (entityList.Count > 0)
            {
                DataSyncManager.SendSql <EntityHistory>(entityList, false, true);
                RedisConnectionPool.ProcessReadOnly(client =>
                {
                    foreach (var pair in removeEntityKeys)
                    {
                        client.HDel(pair.Key, pair.Value);
                    }
                });
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// 从Redis内存移除,并保存到数据库
        /// </summary>
        /// <param name="keys"></param>
        public static void RemoveToDatabase(params string[] keys)
        {
            var entityKeys = new List <string>();
            var entityList = new List <EntityHistory>();

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                foreach (var k in keys)
                {
                    try
                    {
                        string key   = k;
                        string setId = key + "_remove";
                        if (key.EndsWith("_remove"))
                        {
                            setId = key;
                            key   = key.Replace("_remove", "");
                        }
                        else
                        {
                            if (client.ContainsKey(key))
                            {
                                client.Rename(key, setId);
                            }
                        }
                        entityKeys.Add(setId);
                        //转存到DB使用protobuf
                        byte[] keyValues = ProtoBufUtils.Serialize(client.HGetAll(setId));
                        var history      = new EntityHistory()
                        {
                            Key = key, Value = keyValues
                        };
                        entityList.Add(history);
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("Redis cache remove key:{0} to Database error:{1}", k, ex);
                    }
                }
            });

            if (entityList.Count > 0)
            {
                DataSyncManager.GetDataSender().Send <EntityHistory>(entityList.ToArray());
                RedisConnectionPool.ProcessReadOnly(client => client.RemoveAll(entityKeys));
            }
        }
Exemplo n.º 15
0
        public bool TryRedisInfo(out string str)
        {
            string msg    = "";
            bool   result = false;

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                msg = string.Format("Redis[{0}] Info:\r\n", this.RedisConfig.ReadOnlyHost);
                client.Info.ToList().OrderBy(p => p.Key).ToList().ForEach(o =>
                {
                    msg += "\t" + o.Key + ":\t" + o.Value + "\r\n";
                });
                result = true;
            });
            str = msg;
            return(result);
        }
Exemplo n.º 16
0
 private static void LoadUnLineData()
 {
     try
     {
         RedisConnectionPool.ProcessReadOnly(client =>
         {
             byte[] data = client.Get(sessionRedisKey) ?? new byte[0];
             if (data.Length == 0)
             {
                 return;
             }
             var temp = ProtoBufUtils.Deserialize <ConcurrentDictionary <Guid, GameSession> >(data);
             if (temp != null)
             {
                 _globalSession = temp;
             }
         });
     }
     catch (Exception er)
     {
         TraceLog.WriteError("Load GameSession from redis faild,{0}", er);
     }
 }
Exemplo n.º 17
0
        private static void LoadUnLineData()
        {
            try
            {
                IDictionary <string, byte[]> pairs = null;
                RedisConnectionPool.ProcessReadOnly(client =>
                {
                    //client.Add(string.Format("{0}:{1}", sessionRedisKey, sessionId), new GameSession().User, new TimeSpan(Timeout * 1000));
                    var keys = client.SearchKeys(string.Format("{0}:*", sessionRedisKey));
                    if (keys == null || keys.Count == 0)
                    {
                        return;
                    }
                    pairs = client.GetAll <byte[]>(keys);
                });
                pairs = pairs ?? new Dictionary <string, byte[]>();
                foreach (var pair in pairs)
                {
                    var arr = pair.Key.Split(new char[] { ':' }, 2);
                    if (arr.Length != 2)
                    {
                        continue;
                    }
                    Guid sessionId;
                    if (Guid.TryParse(arr[1], out sessionId))
                    {
                        var user = Encoding.UTF8.GetString(pair.Value).ParseJson <SessionUser>();
                        if (user == null)
                        {
                            continue;
                        }
                        var session = new GameSession(sessionId, null)
                        {
                            LastActivityTime = user.OnlineDate
                        };
                        _globalSession[sessionId] = session;

                        int         userId = user.GetUserId();
                        GameSession oldsession;
                        Guid        sid;
                        if (_userHash.TryGetValue(userId, out sid) &&
                            sid != session.KeyCode &&
                            (oldsession = Get(sid)) != null)
                        {
                            //防止先后问题
                            if (oldsession.LastActivityTime < session.LastActivityTime)
                            {
                                session.UnBind();
                                Expire(session, 0);
                            }
                            else
                            {
                                continue;
                            }
                        }
                        _userHash[userId] = session.KeyCode;
                        session.User      = user;
                    }
                }
            }
            catch (Exception er)
            {
                TraceLog.WriteError("Load GameSession from redis faild,{0}", er);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Check to be synchronized wait queue of Sql
        /// </summary>
        /// <param name="state"></param>
        private static void OnCheckSqlWaitSyncQueue(object state)
        {
            int identity = (int)state;

            if (Interlocked.CompareExchange(ref _isSqlWaitSyncWorking[identity], 1, 0) == 0)
            {
                try
                {
                    string   queueKey   = GetSqlWaitSyncQueueKey(identity);
                    string   workingKey = queueKey + "_temp";
                    byte[][] keys       = null;
                    byte[][] values     = null;
                    RedisConnectionPool.ProcessReadOnly(client =>
                    {
                        bool hasWorkingQueue    = client.HLen(workingKey) > 0;
                        bool hasNewWorkingQueue = client.HLen(queueKey) > 0;

                        if (!hasWorkingQueue && !hasNewWorkingQueue)
                        {
                            return;
                        }
                        if (!hasWorkingQueue)
                        {
                            try
                            {
                                client.Rename(queueKey, workingKey);
                            }
                            catch
                            {
                            }
                        }

                        var keyValuePairs = client.HGetAll(workingKey);
                        if (keyValuePairs != null && keyValuePairs.Length > 0)
                        {
                            keys   = keyValuePairs.Where((buffs, index) => index % 2 == 0).ToArray();
                            values = keyValuePairs.Where((buffs, index) => index % 2 == 1).ToArray();
                            client.Remove(workingKey);
                        }
                    });
                    if (keys != null && keys.Length > 0)
                    {
                        try
                        {
                            int      pos = 0;
                            byte[][] subKeys, subValues;

                            while (true)
                            {
                                int count = pos + sqlWaitPackSize < keys.Length ? sqlWaitPackSize : keys.Length - pos;
                                if (count <= 0)
                                {
                                    break;
                                }
                                subKeys   = new byte[count][];
                                subValues = new byte[count][];
                                Array.Copy(keys, pos, subKeys, 0, subKeys.Length);
                                Array.Copy(values, pos, subValues, 0, subValues.Length);
                                _threadPools.QueueWorkItem(DoProcessSqlWaitSyncQueue, workingKey, subKeys, subValues);
                                pos += sqlWaitPackSize;
                            }
                        }
                        catch (Exception er)
                        {
                            TraceLog.WriteError("OnCheckSqlWaitSyncQueue error:{0}", er);
                            RedisConnectionPool.Process(client => client.HMSet(SqlSyncWaitErrirQueueKey, keys, values));
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("OnCheckSqlWaitSyncQueue error:{0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _isSqlWaitSyncWorking[identity], 0);
                }
            }
        }