Ejemplo n.º 1
0
 /// <summary>
 /// 生成存储在Redis的Key
 /// </summary>
 /// <param name="type"></param>
 /// <param name="personalKey"></param>
 /// <returns></returns>
 public static string GenerateRedisKey(Type type, string personalKey)
 {
     if (string.IsNullOrEmpty(personalKey))
     {
         return(string.Format("{0}", RedisConnectionPool.EncodeTypeName(type.FullName)));
     }
     return(string.Format("{0}_{1}", RedisConnectionPool.EncodeTypeName(type.FullName), personalKey));
 }
Ejemplo n.º 2
0
        private static string GetEntityTypeFromKey(string key, out string typeName, ref Type type, out string asmName, out bool isEntityType, out string redisKey)
        {
            int index = key.IndexOf(',');
            var arr   = (index > -1 ? key.Substring(0, index) : key).Split('_');

            typeName = arr[0];
            asmName  = index == -1 ? "" : key.Substring(index + 1, key.Length - index - 1);
            string persionKey = string.Empty;
            string entityKey  = string.Empty;

            if (arr.Length > 1)
            {
                entityKey = arr[1];
                var tempArr = entityKey.Split('|');
                if (tempArr.Length > 1)
                {
                    persionKey = tempArr[0];
                    entityKey  = tempArr[1];
                }
            }
            isEntityType = false;
            if (string.IsNullOrEmpty(persionKey))
            {
                isEntityType = true;
                redisKey     = string.Format("{0}_{1}", RedisConnectionPool.EncodeTypeName(typeName), entityKey);
            }
            else
            {
                //私有类型
                redisKey = string.Format("{0}_{1}", RedisConnectionPool.EncodeTypeName(typeName), persionKey);
            }
            string formatString = entityTypeNameFormat;

            if (isEntityType)
            {
                formatString = "{0},{1}";
            }
            if (type == null)
            {
                string entityTypeName = RedisConnectionPool.DecodeTypeName(typeName);
                type = Type.GetType(string.Format(formatString, entityTypeName, asmName), false, true);
                if (Equals(type, null))
                {
                    var enitityAsm = ScriptEngines.GetEntityAssembly();
                    if (enitityAsm != null)
                    {
                        asmName = enitityAsm.GetName().Name;
                        type    = Type.GetType(string.Format(formatString, entityTypeName, asmName), false, true);
                        if (Equals(type, null))
                        {
                            //调试模式下type为空处理
                            type = enitityAsm.GetType(entityTypeName, false, true);
                        }
                    }
                }
            }
            return(entityKey);
        }
Ejemplo n.º 3
0
        internal static string GenerateEntityKey(AbstractEntity entity)
        {
            if (entity == null)
            {
                return(string.Empty);
            }

            return(string.Format("{0}_{1}|{2}",
                                 RedisConnectionPool.EncodeTypeName(entity.GetType().FullName),
                                 entity.PersonalId,
                                 entity.GetKeyCode()));
        }
Ejemplo n.º 4
0
        private static string GetQueueFormatKey(AbstractEntity entity)
        {
            if (entity == null)
            {
                return(string.Empty);
            }

            return(string.Format("{0}_{1}|{2}",
                                 RedisConnectionPool.EncodeTypeName(entity.GetType().FullName),
                                 entity.GetIdentityId(),
                                 entity.GetKeyCode()));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get entity of personal object
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="personalId"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static dynamic GetPersonalEntity(string entityType, string personalId, params object[] keys)
        {
            string key = AbstractEntity.EncodeKeyCode(personalId);

            if (keys.Length > 0)
            {
                key += "|" + AbstractEntity.CreateKeyCode(keys);
            }
            string       redisKey = string.Format("{0}_{1}", RedisConnectionPool.EncodeTypeName(entityType), key);
            CacheItemSet itemSet;

            return(GetPersonalEntity(redisKey, out itemSet));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Send to queue pool
        /// </summary>
        /// <param name="entityList"></param>
        public static void Send(params AbstractEntity[] entityList)
        {
            string key = "";

            try
            {
                //modify season:异步调用时不能保证提交的顺序,造成更新到Redis中不是最后一次的
                lock (_entitySet)
                {
                    foreach (var entity in entityList)
                    {
                        key = string.Format("{0}_{1}|{2}",
                                            RedisConnectionPool.EncodeTypeName(entity.GetType().FullName),
                                            entity.GetIdentityId(),
                                            entity.GetKeyCode());

                        //todo debug
                        var stacktrace = TraceLog.GetStackTrace();
                        var hashpair   = new KeyValuePair <int, string>(entity.GetHashCode(), stacktrace);
                        KeyValuePair <int, string> oldHashPair;
                        if (_checkVersions.TryGetValue(key, out oldHashPair))
                        {
                            if (oldHashPair.Key != hashpair.Key)
                            {
                                TraceLog.ReleaseWriteDebug("Send {0}, hash:{1}/{2}, new stack:{3} \r\n old stack:{4}",
                                                           key, oldHashPair.Key, hashpair.Key, hashpair.Value, oldHashPair.Value);
                                _checkVersions[key] = hashpair;
                            }
                        }
                        else
                        {
                            _checkVersions[key] = new KeyValuePair <int, string>(entity.GetHashCode(), stacktrace);
                        }
                        //todo end debug

                        if (!_entitySet.Contains(key))
                        {
                            _entitySet.Add(key);
                        }
                        if (entity.IsDelete && !_entityRemoteSet.Contains(key))
                        {
                            _entityRemoteSet.Add(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Post changed key:{0} error:{1}", key, ex);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Send to queue pool
        /// </summary>
        /// <param name="entityList"></param>
        public static void Send(params AbstractEntity[] entityList)
        {
            string key = "";

            try
            {
                if (entityList == null || entityList.Length == 0)
                {
                    return;
                }
                var groupList = entityList.GroupBy(t => t.GetIdentityId());
                foreach (var g in groupList)
                {
                    var      valueList  = g.ToList();
                    byte[][] keyBytes   = new byte[valueList.Count][];
                    byte[][] valueBytes = new byte[valueList.Count][];
                    string   queueKey   = GetRedisSyncQueueKey(g.Key);
                    byte[]   idBytes    = BufferUtils.GetBytes(g.Key);

                    int index = 0;
                    foreach (var entity in valueList)
                    {
                        key             = string.Format("{0}_{1}", RedisConnectionPool.EncodeTypeName(entity.GetType().FullName), entity.GetKeyCode());
                        keyBytes[index] = RedisConnectionPool.ToByteKey(key);
                        byte[] stateBytes = BufferUtils.GetBytes(entity.IsDelete ? 1 : 0);
                        valueBytes[index] = BufferUtils.MergeBytes(
                            BufferUtils.GetBytes(idBytes.Length + stateBytes.Length),
                            idBytes,
                            stateBytes,
                            _serializer.Serialize(entity));
                        index++;
                    }
                    _asyncSendHandle.BeginInvoke(queueKey, keyBytes, valueBytes, null, null);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Post changed key:{0} error:{1}", key, ex);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Send to queue pool
        /// </summary>
        /// <param name="entityList"></param>
        public static void Send(params AbstractEntity[] entityList)
        {
            string key = "";

            try
            {
                //modify season:异步调用时不能保证提交的顺序,造成更新到Redis中不是最后一次的
                lock (entitySyncRoot)
                {
                    foreach (var entity in entityList)
                    {
                        if (entity == null)
                        {
                            continue;                //cacheCollection has changed
                        }
                        entity.TempTimeModify = MathUtils.Now;
                        key = string.Format("{0}_{1}|{2}",
                                            RedisConnectionPool.EncodeTypeName(entity.GetType().FullName),
                                            entity.GetIdentityId(),
                                            entity.GetKeyCode());
                        if (_entitySet.Add(key))
                        {
                            SendWaitCount++;
                        }
                        if (entity.IsDelete)
                        {
                            _entityRemoteSet.Add(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Post changed key:{0} error:{1}", key, ex);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 通过Redis键获取实体对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isRemove"></param>
        /// <param name="type"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public static dynamic GetEntityFromRedis(string key, bool isRemove, Type type, ICacheSerializer serializer)
        {
            string  typeName;
            string  asmName;
            bool    isEntityType;
            string  redisKey;
            string  entityKey = GetEntityTypeFromKey(key, out typeName, ref type, out asmName, out isEntityType, out redisKey);
            dynamic entity    = null;

            RedisConnectionPool.Process(client =>
            {
                if (isEntityType)
                {
                    var data = client.Get <byte[]>(redisKey);
                    if (data != null && type != null)
                    {
                        entity = serializer.Deserialize(data, type);
                    }
                }
                else
                {
                    var data = client.Get <byte[]>(redisKey);
                    if (data != null && type != null)
                    {
                        var dict = (IDictionary)serializer.Deserialize(data, type);
                        entity   = dict[entityKey];
                    }
                }
                if (entity == null)
                {
                    //新版本Hash格式
                    var data = client.HGet(typeName, RedisConnectionPool.ToByteKey(entityKey));
                    if (data != null && type != null)
                    {
                        entity = serializer.Deserialize(data, type);
                    }
                }

                if (isRemove && entity == null && type != null)
                {
                    //临时队列删除Entity
                    string setId     = (isEntityType ? RedisConnectionPool.EncodeTypeName(typeName) : redisKey) + ":remove";
                    IDictionary dict = null;
                    RedisConnectionPool.ProcessTrans(client, new string[] { setId }, () =>
                    {
                        var data = client.Get <byte[]>(setId);
                        if (data == null)
                        {
                            return(false);
                        }
                        dict   = (IDictionary)serializer.Deserialize(data, type);
                        entity = dict[entityKey];
                        dict.Remove(entityKey);

                        return(true);
                    }, trans =>
                    {
                        if (dict != null && dict.Count > 0)
                        {
                            trans.QueueCommand(c => c.Set(setId, serializer.Serialize(dict)));
                        }
                        else
                        {
                            trans.QueueCommand(c => c.Remove(setId));
                        }
                    }, null);
                }
            });
            return(entity);
        }