Пример #1
0
 private static string GetQueueFormatKey(AbstractEntity entity)
 {
     return(CacheFactory.GenerateEntityKey(entity));
 }
Пример #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;
                    }

                    RedisConnectionPool.ProcessPipeline(pipeline =>
                    {
                        bool hasPost = false;
                        foreach (var key in temp)
                        {
                            var keyValues = key.Split('_', '|');
                            if (keyValues.Length != 3)
                            {
                                TraceLog.WriteWarn("OnEntitySyncQueue:{0}", key);
                                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
                            {
                                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();
                        }
                    });
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("EntitySync queue {0}", ex);
                }
                finally
                {
                    Interlocked.Exchange(ref _entityQueueRunning, 0);
                }
            }
        }