Esempio n. 1
0
        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        public virtual void Set(string key, object data, TimeSpan expire)
        {
            var res  = new CacheResult <object>(data);
            var json = res.ToJson();

            this._db.StringSet(key, (string)json, expire);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        public virtual void Set(string key, object data, TimeSpan expire)
        {
            var res = new CacheResult <object>()
            {
                Success = true, Result = data
            };
            var entryBytes = this.Serialize(res);

            this._db.StringSet(key, entryBytes, expire);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        public virtual void Set(string key, object data, TimeSpan expire)
        {
            var policy = new CacheItemPolicy();

            policy.AbsoluteExpiration = DateTime.Now + expire;

            var res = new CacheResult <object>()
            {
                Result = data, Success = true
            };

            Cache.Add(new CacheItem(key, Serialize(res)), policy);
        }
Esempio n. 4
0
        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        public virtual void Set(string key, object data, TimeSpan expire)
        {
            RedisManager.PrepareDataBase(_db =>
            {
                var res = new CacheResult <object>()
                {
                    Success = true, Result = data
                };
                var entryBytes = this.Serialize(res);

                _db.StringSet(key, entryBytes, expire);
                return(true);
            }, CACHE_DB);
        }
Esempio n. 5
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public virtual CacheResult <T> Get <T>(string key)
        {
            var cache = new CacheResult <T>()
            {
                Success = false
            };
            var bs = Cache[key] as byte[];

            if (bs != null)
            {
                cache.Result  = Deserialize <T>(bs);
                cache.Success = true;
            }
            return(cache);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public virtual CacheResult <T> Get <T>(string key)
        {
            var cache = new CacheResult <T>()
            {
                Success = false
            };

            RedisManager.PrepareDataBase(_db =>
            {
                var rValue = _db.StringGet(key);
                if (rValue.HasValue)
                {
                    cache.Result  = Deserialize <T>(rValue);
                    cache.Success = true;
                }
                return(true);
            }, CACHE_DB);
            return(cache);
        }
Esempio n. 7
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public virtual CacheResult <T> Get <T>(string key)
        {
            var cache = new CacheResult <T>()
            {
                Success = false
            };

            var rValue = this._db.StringGet(key);

            if (rValue.HasValue)
            {
                var res = this.Deserialize <CacheResult <T> >(rValue);
                if (res != null)
                {
                    res.Success = true;
                    cache       = res;
                }
            }

            return(cache);
        }
Esempio n. 8
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public virtual CacheResult <T> Get <T>(string key)
        {
            var cache = new CacheResult <T>()
            {
                Success = false
            };

            var rValue = this._db.StringGet(key);

            if (rValue.HasValue)
            {
                var res = ((string)rValue).JsonToEntity <CacheResult <T> >(throwIfException: false);
                if (res != null)
                {
                    res.Success = true;
                    cache       = res;
                }
            }

            return(cache);
        }
Esempio n. 9
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public virtual CacheResult <T> Get <T>(string key)
        {
            var cache = new CacheResult <T>()
            {
                Success = false
            };

            RedisManager.PrepareDataBase(_db =>
            {
                var rValue = _db.StringGet(key);
                if (rValue.HasValue)
                {
                    var res = this.Deserialize <CacheResult <T> >(rValue);
                    if (res != null)
                    {
                        res.Success = true;
                        cache       = res;
                    }
                }
                return(true);
            }, CACHE_DB);
            return(cache);
        }
Esempio n. 10
0
 /// <summary>
 /// 设置数据,并标记为成功
 /// </summary>
 public static void SetSuccessData <T>(this CacheResult <T> data, T model)
 {
     data.Success = true;
     data.Result  = model;
 }
Esempio n. 11
0
 public static CacheResult <T> OK <T>(this CacheResult <T> data)
 {
     data.Success = true;
     return(data);
 }