예제 #1
0
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="cacheKey">缓存键</param>
        /// <param name="cacheValue">缓存值</param>
        /// <param name="expiration">过期时间</param>
        public void Set <T>(string cacheKey, T cacheValue, TimeSpan expiration) where T : class
        {
            cacheKey.CheckNotNullOrEmpty(nameof(cacheKey));
            cacheValue.CheckNotNull(nameof(cacheValue));
            expiration.CheckGreaterThan(nameof(expiration), TimeSpan.Zero);

            //var content = ToJson(cacheValue);
            var content = _serializer.Serialize(cacheValue);

            cacheKey = AddSysCustomKey(cacheKey);

            _database.StringSet(cacheKey, content, expiration);
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="cacheKey">缓存键</param>
        /// <param name="cacheValue">缓存值</param>
        /// <param name="expiry">过期时间</param>
        public void Set <T>(string cacheKey, T cacheValue, TimeSpan expiry)
        {
            Check.NotNullOrEmpty(cacheKey, nameof(cacheKey));
            Check.NotNull(cacheValue, nameof(cacheValue));
            Check.NotNegativeOrZero(expiry, nameof(expiry));

            if (MaxRdSecond > 0)
            {
                var addSec = new Random().Next(1, MaxRdSecond);
                expiry.Add(new TimeSpan(0, 0, addSec));
            }

            _cache.StringSet(cacheKey, _serializer.Serialize(cacheValue), expiry);
        }
예제 #3
0
 public Task SetAsync <T>(string key, T value, DateTimeOffset?expiration = null)
 {
     return(Task.Run(async() =>
     {
         var bytes = Serializer.Serialize(value);
         await SetAsync(key: key, value: bytes);
     }));
 }
예제 #4
0
 internal protected override async Task SetCoreAsync(string key, object value, CacheEntryOptions options = null)
 {
     Guard.ArgumentNotNullOrWhiteSpace(key, nameof(key));
     var options2 = options != null ? new DistributedCacheEntryOptions
     {
         AbsoluteExpiration = options.AbsoluteExpiration,
         AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow,
         SlidingExpiration = options.SlidingExpiration
     } : _options;
     await _cache.SetAsync(key, _serializer.Serialize(value), options2);
 }
예제 #5
0
        public async Task AddOrUpdateAsync(Products product)
        {
            product = product ?? throw new NullReferenceException(nameof(product));
            var serialized = _cacheResializer.Serialize(product);
            var result     = await _context.Database.StringSetAsync($"products.{product.Id}", serialized);

            if (!result)
            {
                //TODO: write to log
            }
        }
예제 #6
0
 public void DumpEntity(ISqlEntity entity)
 {
     if (m_Entities.ContainsKey(entity))
     {
         return;
     }
     else
     {
         m_Entities.Add(entity, m_Serializer.Serialize(entity));
     }
 }
예제 #7
0
        public async Task TrySetAsync(string key, T value, DistributedCacheEntryOptions options = null, CancellationToken cancellationToken = default)
        {
            var cacheKey = BuildKey(key);
            var distributedCacheValue = _cacheSerializer.Serialize(value);

            try
            {
                await _distributedCache.SetAsync(cacheKey, distributedCacheValue, options, cancellationToken);
            }
            catch
            {
                // ignore
            }
        }
예제 #8
0
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation, Func <Task <TResult> > proceed)
        {
            string key = _keyGenerator.GenerateKey(invocation.Method, invocation.Arguments);

            byte[] cached = await _cache.GetAsync(key);

            if (cached is null || cached.Length == 0)
            {
                _logger.LogTrace("Cached value unavailable for {Method}", invocation.Method);

                var value = await proceed();

                _logger.LogTrace("Setting cached value for {Method}", invocation.Method);
                await _cache.SetAsync(key, _serializer.Serialize(value));

                return(value);
            }
예제 #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);
        }
예제 #10
0
        RedisValue IRedisValueConverter <object> .ToRedisValue(object value)
        {
            var valueType = value.GetType();

            if (valueType == _byteArrayType)
            {
                var converter = (IRedisValueConverter <byte[]>) this;
                return(converter.ToRedisValue((byte[])value));
            }
            else if (valueType == _byteType)
            {
                var converter = (IRedisValueConverter <byte>) this;
                return(converter.ToRedisValue((byte)value));
            }
            else if (valueType == _stringType)
            {
                var converter = (IRedisValueConverter <string>) this;
                return(converter.ToRedisValue((string)value));
            }
            else if (valueType == _intType)
            {
                var converter = (IRedisValueConverter <int>) this;
                return(converter.ToRedisValue((int)value));
            }
            else if (valueType == _uIntType)
            {
                var converter = (IRedisValueConverter <uint>) this;
                return(converter.ToRedisValue((uint)value));
            }
            else if (valueType == _shortType)
            {
                var converter = (IRedisValueConverter <short>) this;
                return(converter.ToRedisValue((short)value));
            }
            else if (valueType == _uShortType)
            {
                var converter = (IRedisValueConverter <ushort>) this;
                return(converter.ToRedisValue((ushort)value));
            }
            else if (valueType == _singleType)
            {
                var converter = (IRedisValueConverter <float>) this;
                return(converter.ToRedisValue((float)value));
            }
            else if (valueType == _doubleType)
            {
                var converter = (IRedisValueConverter <double>) this;
                return(converter.ToRedisValue((double)value));
            }
            else if (valueType == _boolType)
            {
                var converter = (IRedisValueConverter <bool>) this;
                return(converter.ToRedisValue((bool)value));
            }
            else if (valueType == _longType)
            {
                var converter = (IRedisValueConverter <long>) this;
                return(converter.ToRedisValue((long)value));
            }
            else if (valueType == _uLongType)
            {
                var converter = (IRedisValueConverter <ulong>) this;
                return(converter.ToRedisValue((ulong)value));
            }
            else if (valueType == _charType)
            {
                var converter = (IRedisValueConverter <char>) this;
                return(converter.ToRedisValue((char)value));
            }

            return(_serializer.Serialize(value));
        }
예제 #11
0
        /// <summary>
        /// Try get entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="table"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool TryGetEntity <T>(string redisKey, SchemaTable table, out List <T> list) where T : AbstractEntity
        {
            list = new List <T>();
            bool result = false;

            try
            {
                //修改存储统一Hash格式(TypeName, keyCode, value)
                var    keys     = redisKey.Split('_');
                string keyValue = "";
                string hashId   = GetRedisEntityKeyName(keys[0]);
                byte[] keyCode  = null;
                if (keys.Length > 1)
                {
                    keyValue = keys[1];
                    keyCode  = ToByteKey(keyValue);
                }
                byte[][] valueBytes = null;
                byte[]   value      = null;
                bool     isFilter   = false;
                try
                {
                    ProcessReadOnly(client =>
                    {
                        if (keyCode == null)
                        {
                            if (client.ContainsKey(hashId))
                            {
                                valueBytes = client.HVals(hashId);//.Where((b, index) => index % 2 == 1).ToArray();
                            }
                        }
                        else
                        {
                            value = client.HGet(hashId, keyCode);
                            //修正未使用Persional作为Key,而是多个Key时,加载数据为空问题,修改成加载所有
                            if (value == null && table != null &&
                                !string.IsNullOrEmpty(keyValue) &&
                                typeof(T).IsSubclassOf(typeof(BaseEntity)) &&
                                table.Keys.Length > 1)
                            {
                                isFilter            = true;
                                byte[][] resultKeys = client.HKeys(hashId).Where(k => ContainKey(k, keyCode, AbstractEntity.KeyCodeJoinChar)).ToArray();
                                if (resultKeys.Length > 0)
                                {
                                    valueBytes = client.HMGet(hashId, resultKeys);//.Where((b, index) => index % 2 == 1).ToArray();
                                }
                            }
                        }
                    });

                    if (valueBytes != null || value != null)
                    {
                        if (value != null)
                        {
                            list = new List <T> {
                                (T)_serializer.Deserialize(value, typeof(T))
                            };
                            return(true);
                        }
                        if (valueBytes != null)
                        {
                            if (isFilter)
                            {
                                list = valueBytes.Select(t => (T)_serializer.Deserialize(t, typeof(T))).Where(t => t.PersonalId == keyValue).ToList();
                            }
                            else
                            {
                                list = valueBytes.Select(t => (T)_serializer.Deserialize(t, typeof(T))).ToList();
                            }
                            return(true);
                        }
                    }
                }
                catch (Exception er)
                {
                    list = null;
                    TraceLog.WriteError("Get redis \"{0}\" key:\"{1}\" cache error:{2}", typeof(T).FullName, redisKey, er);
                }

                try
                {
                    //从旧版本存储格式中查找
                    if (typeof(T).IsSubclassOf(typeof(ShareEntity)))
                    {
                        byte[][]      buffers = new byte[0][];
                        List <string> keyList = new List <string>();
                        ProcessReadOnly(client =>
                        {
                            keyList = client.SearchKeys(string.Format("{0}_*", redisKey));
                            if (keyList != null && keyList.Count > 0)
                            {
                                buffers = client.MGet(keyList.ToArray());
                            }
                        });
                        list = new List <T>();
                        byte[][] keyCodes = new byte[buffers.Length][];
                        for (int i = 0; i < buffers.Length; i++)
                        {
                            T entity = (T)_serializer.Deserialize(buffers[i], typeof(T));
                            keyCodes[i] = ToByteKey(entity.GetKeyCode());
                            list.Add(entity);
                        }
                        if (keyCodes.Length > 0)
                        {
                            //转移到新格式
                            UpdateEntity(typeof(T).FullName, keyCodes, buffers);
                            if (keyList.Count > 0)
                            {
                                Process(client => client.RemoveAll(keyList));
                            }
                        }
                    }
                    else
                    {
                        byte[] buffers = new byte[0];
                        ProcessReadOnly(client =>
                        {
                            buffers = client.Get <byte[]>(redisKey) ?? new byte[0];
                        });

                        try
                        {
                            var dataSet = (Dictionary <string, T>)_serializer.Deserialize(buffers, typeof(Dictionary <string, T>));
                            if (dataSet != null)
                            {
                                list = dataSet.Values.ToList();
                            }
                        }
                        catch
                        {
                            //try get entity type data
                            list = new List <T>();
                            T temp = (T)_serializer.Deserialize(buffers, typeof(T));
                            list.Add(temp);
                        }
                        //转移到新格式
                        if (list != null)
                        {
                            byte[][] keyCodes = new byte[list.Count][];
                            byte[][] values   = new byte[list.Count][];
                            for (int i = 0; i < list.Count; i++)
                            {
                                T entity = list[i];
                                keyCodes[i] = ToByteKey(entity.GetKeyCode());
                                values[i]   = _serializer.Serialize(entity);
                            }
                            if (keyCodes.Length > 0)
                            {
                                UpdateEntity(typeof(T).FullName, keyCodes, values);
                                Process(client => client.Remove(redisKey));
                            }
                        }
                    }
                    result = true;
                }
                catch (Exception er)
                {
                    list = null;
                    TraceLog.WriteError("Get redis \"{0}\" key(old):\"{1}\" cache error:{2}", typeof(T).FullName, redisKey, er);
                }
            }
            catch (Exception ex)
            {
                list = null;
                TraceLog.WriteError("Get redis \"{0}\" key:\"{1}\" cache error:{2}", typeof(T).FullName, redisKey, ex);
            }
            return(result);
        }
예제 #12
0
 public async Task SetAsync <T>(string key, T value, DateTimeOffset?expiration = null)
 {
     var bytes = Serializer.Serialize(value);
     await Cache.Insert(key, bytes, expiration);
 }
예제 #13
0
        public virtual void Save()
        {
            string serializedCache = cacheSerializer.Serialize(cacheLookup);

            fileRepository.SaveFile(showNameCacheFullName, serializedCache);
        }
예제 #14
0
        private static bool TryGetOlbValue <T>(string redisKey, out List <T> list) where T : ISqlEntity
        {
            bool result = false;

            try
            {
                if (RedisInfo.ClientVersion >= EntityMinVersion)
                {
                    list = new List <T>();
                    return(true);
                }
                //从旧版本存储格式中查找
                if (typeof(T).IsSubclassOf(typeof(ShareEntity)))
                {
                    var tempList = new List <T>();
                    Process(client =>
                    {
                        byte[][] buffers;
                        List <string> keyList = client.SearchKeys(string.Format("{0}_*", redisKey));
                        if (keyList == null || keyList.Count <= 0)
                        {
                            return;
                        }
                        ProcessTrans(client, keyList.ToArray(), () =>
                        {
                            buffers           = client.MGet(keyList.ToArray());
                            byte[][] keyCodes = new byte[buffers.Length][];
                            for (int i = 0; i < buffers.Length; i++)
                            {
                                T entity    = (T)_serializer.Deserialize(buffers[i], typeof(T));
                                keyCodes[i] = ToByteKey(entity.GetKeyCode());
                                tempList.Add(entity);
                            }
                            if (keyCodes.Length > 0)
                            {
                                //转移到新格式
                                if (!UpdateEntity(typeof(T).FullName, keyCodes, buffers))
                                {
                                    //转移失败
                                    return(false);
                                }
                                if (keyList.Count > 0)
                                {
                                    return(true);
                                }
                            }
                            return(false);
                        }, trans => trans.QueueCommand(c => c.RemoveAll(keyList)), null);
                    });
                    list = tempList;
                }
                else
                {
                    var    tempList = new List <T>();
                    byte[] buffers  = new byte[0];
                    ProcessTrans(redisKey, client =>
                    {
                        try
                        {
                            buffers     = client.Get <byte[]>(redisKey) ?? new byte[0];
                            var dataSet = (Dictionary <string, T>)_serializer.Deserialize(buffers, typeof(Dictionary <string, T>));
                            if (dataSet != null)
                            {
                                tempList = dataSet.Values.ToList();
                            }
                        }
                        catch
                        {
                            //try get entity type data
                            tempList = new List <T>();
                            T temp   = (T)_serializer.Deserialize(buffers, typeof(T));
                            tempList.Add(temp);
                        }
                        //转移到新格式
                        if (tempList != null)
                        {
                            byte[][] keyCodes = new byte[tempList.Count][];
                            byte[][] values   = new byte[tempList.Count][];
                            for (int i = 0; i < tempList.Count; i++)
                            {
                                T entity    = tempList[i];
                                keyCodes[i] = ToByteKey(entity.GetKeyCode());
                                values[i]   = _serializer.Serialize(entity);
                            }
                            if (keyCodes.Length > 0)
                            {
                                if (!UpdateEntity(typeof(T).FullName, keyCodes, values))
                                {
                                    return(false);
                                }
                                return(true);
                            }
                        }
                        return(false);
                    }, trans => trans.QueueCommand(c => c.Remove(redisKey)));

                    list = tempList;
                }
                result = true;
            }
            catch (Exception er)
            {
                list = null;
                TraceLog.WriteError("Get redis \"{0}\" key(old):\"{1}\" cache error:{2}", typeof(T).FullName, redisKey, er);
            }
            return(result);
        }