Пример #1
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)
                    {
                        return;
                    }
                    using (var client = RedisConnectionPool.GetClient())
                    {
                        var pipeline = client.CreatePipeline();
                        try
                        {
                            bool hasPost = false;
                            foreach (var key in temp)
                            {
                                var keyValues = key.Split('_', '|');
                                if (keyValues.Length != 3)
                                {
                                    continue;
                                }

                                AbstractEntity entity   = CacheFactory.GetPersonalEntity(key) as AbstractEntity;
                                int            id       = 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);
                                }
                                else if (tempRemove.Contains(key))
                                {
                                    entityBytes = new byte[0];
                                    isDelete    = true;
                                }
                                else
                                {
                                    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;
                            }
                            if (hasPost)
                            {
                                pipeline.Flush();
                            }
                        }
                        finally
                        {
                            pipeline.Dispose();
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("EntitySync queue {0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _entityQueueRunning, 0);
                }
            }
        }