示例#1
0
        private bool _Set(Areas area, string field, string key, string value, TimeSpan expiration)
        {
            key = EncodeBlobName(key);

            string _value = null;

            /* if the string is over 250K in length (approximately 500K in size) then compress it for performance */
            if (value.Length > 1024 * 250)
            {
                _value = Compression.Compress(value);
            }
            else
            {
                _value = value;
            }

            var blob = GetBlobReference(area, field, key);

            VigilCacheData data           = new VigilCacheData(_value, expiration);
            var            serializedData = JsonConvert.SerializeObject(data);

            blob.UploadText(serializedData);

            return(true);
        }
示例#2
0
        private string _Get(Areas area, string field, string key)
        {
            key = EncodeBlobName(key);

            var blob = GetBlobReference(area, field, key);

            if (!blob.Exists())
            {
                return(null);
            }

            var            tmp = blob.DownloadText();
            VigilCacheData vcd = VigilCacheData.FromString(tmp);

            if (vcd.ExpirationDate < DateTime.UtcNow)
            {
                blob.Delete();
                return(null);
            }

            string result = null;

            if (Compression.IsBase64String(tmp))
            {
                result = Compression.Decompress(vcd.Value);
            }
            else
            {
                result = vcd.Value;
            }

            return(result);
        }
示例#3
0
        private string _Get(Areas area, string field, string key)
        {
            key = EncodeBlobName(key);
            var       redisKey = string.Format("{0}^{1}^{2}", area.ToString(), field, key);
            IDatabase cache    = lazyConnection.Value.GetDatabase();

            if (cache.KeyExists(redisKey))
            {
                var            tmp = cache.StringGet(redisKey);
                VigilCacheData vcd = VigilCacheData.FromString(tmp);

                if (vcd != null && vcd.ExpirationDate < DateTime.UtcNow)
                {
                    cache.KeyDelete(redisKey);
                    return(null);
                }

                string result = null;
                if (Compression.IsBase64String(tmp))
                {
                    result = Compression.Decompress(vcd.Value);
                }
                else
                {
                    result = vcd.Value;
                }
                return(result);
            }

            return(null);
        }
示例#4
0
        private bool _Set(Areas area, string field, string key, string value, TimeSpan expiration)
        {
            key = EncodeBlobName(key);

            string _value = null;

            /* if the string is over 250K in length (approximately 500K in size) then compress it for performance */
            if (value.Length > 1024 * 250)
            {
                _value = Compression.Compress(value);
            }
            else
            {
                _value = value;
            }

            var       redisKey = string.Format("{0}^{1}^{2}", area.ToString(), field, key);
            IDatabase cache    = lazyConnection.Value.GetDatabase();

            VigilCacheData data           = new VigilCacheData(_value, expiration);
            var            serializedData = JsonConvert.SerializeObject(data);

            cache.StringSet(redisKey, serializedData, expiration);
            return(true);
        }
示例#5
0
        private T _Get <T>(Areas area, string field, string key)
        {
            key = EncodeBlobName(key);

            var blob = GetBlobReference(area, field, key);

            if (!blob.Exists())
            {
                return(default(T));
            }

            string json = "";

            try
            {
                var            tmp = blob.DownloadText();
                VigilCacheData vcd = VigilCacheData.FromString(tmp);

                if (vcd.ExpirationDate < DateTime.UtcNow)
                {
                    blob.Delete();
                    return(default(T));
                }

                if (Compression.IsBase64String(vcd.Value))
                {
                    json = Compression.Decompress(vcd.Value);
                }
                else
                {
                    json = vcd.Value;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Could not retrieve cache object for key {0}. {1}", key, ex.Message);
                return(default(T));
            }

            if (!string.IsNullOrEmpty(json))
            {
                try
                {
                    return(JsonConvert.DeserializeObject <T>(json));
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Could not deserialize cache object for key {0}. {1}", key, ex.Message);
                }
            }

            return(default(T));
        }
示例#6
0
        private T _Get <T>(Areas area, string field, string key)
        {
            key = EncodeBlobName(key);
            var       redisKey = string.Format("{0}^{1}^{2}", area.ToString(), field, key);
            IDatabase cache    = lazyConnection.Value.GetDatabase();
            string    json     = "";

            if (cache.KeyExists(redisKey))
            {
                var            tmp = cache.StringGet(redisKey);
                VigilCacheData vcd = VigilCacheData.FromString(tmp);

                if (vcd == null || vcd.ExpirationDate < DateTime.UtcNow)
                {
                    cache.KeyDelete(redisKey);
                    return(default(T));;
                }

                if (Compression.IsBase64String(vcd.Value))
                {
                    json = Compression.Decompress(vcd.Value);
                }
                else
                {
                    json = vcd.Value;
                }
            }

            if (!string.IsNullOrEmpty(json))
            {
                try
                {
                    return(JsonConvert.DeserializeObject <T>(json));
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Could not deserialize cache object for key {0}. {1}", key, ex.Message);
                }
            }

            return(default(T));
        }