Exemplo n.º 1
0
        /// <summary>
        /// 以同步的方式更新Entity
        /// </summary>
        /// <param name="entityList"></param>
        public static bool SendSync(IEnumerable <AbstractEntity> entityList)
        {
            var keyList   = new List <byte[]>();
            var valueList = new List <byte[]>();

            foreach (var entity in entityList)
            {
                if (entity == null)
                {
                    continue;
                }
                entity.TempTimeModify = MathUtils.Now;
                string key       = GetQueueFormatKey(entity);
                var    keyValues = key.Split('_', '|');
                int    id        = AbstractEntity.DecodeKeyCode(keyValues[1]).ToInt();
                string keyCode   = keyValues[2];
                string redisKey  = string.Format("{0}_{1}", keyValues[0], keyCode);
                byte[] idBytes   = BufferUtils.GetBytes(id);
                var    keyBytes  = RedisConnectionPool.ToByteKey(redisKey);
                byte[] entityBytes;
                bool   isDelete = entity.IsDelete;
                entityBytes = _serializer.Serialize(entity);
                //modify resean: set unchange status.
                entity.Reset();

                byte[] stateBytes = BufferUtils.GetBytes(isDelete ? 1 : 0);
                byte[] values     = BufferUtils.MergeBytes(BufferUtils.GetBytes(idBytes.Length + stateBytes.Length), idBytes, stateBytes, entityBytes);
                keyList.Add(keyBytes);
                valueList.Add(values);
            }
            return(ProcessRedisSyncQueue(string.Empty, keyList.ToArray(), valueList.ToArray()));
        }
Exemplo n.º 2
0
        private static void OnEntitySyncQueue(object state)
        {
            if (Interlocked.CompareExchange(ref _entityQueueRunning, 1, 0) == 0)
            {
                try
                {
                    var tempRemove = Interlocked.Exchange(ref _entityRemoteSet, new HashSet <string>());
                    var temp       = Interlocked.Exchange(ref _entitySet, new HashSet <string>());
                    if (temp.Count == 0 || _queueWatchTimers == null)
                    {
                        return;
                    }
                    TraceLog.WriteWarn("OnEntitySyncQueue execute count:{0}, success:{1}/total {2}, fail:{3} start...", temp.Count, ExecuteSuccessCount, SendWaitCount, ExecuteFailCount);

                    RedisConnectionPool.ProcessPipeline(pipeline =>
                    {
                        bool hasPost = false;
                        foreach (var key in temp)
                        {
                            var keyValues = key.Split('_', '|');
                            if (keyValues.Length != 3)
                            {
                                TraceLog.WriteWarn("OnEntitySyncQueue:{0}", key);
                                ExecuteFailCount++;
                                continue;
                            }

                            AbstractEntity entity = CacheFactory.GetPersonalEntity(key) as AbstractEntity;
                            int id          = AbstractEntity.DecodeKeyCode(keyValues[1]).ToInt();
                            string keyCode  = keyValues[2];
                            string redisKey = string.Format("{0}_{1}", keyValues[0], keyCode);
                            string hashId   = GetRedisSyncQueueKey(id);
                            byte[] idBytes  = BufferUtils.GetBytes(id);
                            var keyBytes    = RedisConnectionPool.ToByteKey(redisKey);
                            bool isDelete;
                            byte[] entityBytes;
                            if (entity != null)
                            {
                                isDelete    = entity.IsDelete;
                                entityBytes = _serializer.Serialize(entity);
                                //modify resean: set unchange status.
                                entity.Reset();
                            }
                            else if (tempRemove.Contains(key))
                            {
                                entityBytes = new byte[0];
                                isDelete    = true;
                            }
                            else
                            {
                                ExecuteFailCount++;
                                TraceLog.WriteError("EntitySync queue key {0} faild object is null.", key);
                                continue;
                            }
                            byte[] stateBytes = BufferUtils.GetBytes(isDelete ? 1 : 0);
                            byte[] values     =
                                BufferUtils.MergeBytes(BufferUtils.GetBytes(idBytes.Length + stateBytes.Length),
                                                       idBytes, stateBytes, entityBytes);
                            pipeline.QueueCommand(c =>
                            {
                                ((RedisClient)c).HSet(hashId, keyBytes, values);
                            });
                            hasPost = true;
                            ExecuteSuccessCount++;
                        }
                        if (hasPost)
                        {
                            pipeline.Flush();
                        }
                    });
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("EntitySync queue {0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _entityQueueRunning, 0);
                }
            }
        }