Exemple #1
0
        /// <summary>
        /// Gets a <c>CacheItem</c> for the specified key.
        /// </summary>
        /// <param name="key">The key being used to identify the item within the cache.</param>
        /// <param name="region">The cache region.</param>
        /// <returns>The <c>CacheItem</c>.</returns>
        protected override CacheItem <TCacheValue> GetCacheItemInternal(string key, string region)
        {
            CacheItem <TCacheValue> item = null;

            if (_serializer != null)
            {
                // If it's expired, this result could be null, so we just set Item to null
                var cacheItemBytes = _cache.Get(key, region);

                if (cacheItemBytes != null)
                {
                    item = _serializer.DeserializeCacheItem <TCacheValue>((byte[])cacheItemBytes, typeof(TCacheValue));
                }
            }
            else
            {
                item = (CacheItem <TCacheValue>)_cache.Get(key, region);
            }

            if (item == null)
            {
                return(null);
            }

            // maybe the item is already expired because FileCache implements a default interval
            // of 20 seconds! to check for expired items on each store, we do it on access to also
            // reflect smaller time frames especially for sliding expiration...
            // cache.Get eventually triggers eviction callback, but just in case...
            if (item.IsExpired)
            {
                RemoveInternal(item.Key, item.Region);
                TriggerCacheSpecificRemove(item.Key, item.Region, CacheItemRemovedReason.Expired, item.Value);
                return(null);
            }

            if (item.ExpirationMode == ExpirationMode.Sliding)
            {
                // because we don't use UpdateCallback because of some multithreading issues lets
                // try to simply reset the item by setting it again.
                // item = this.GetItemExpiration(item); // done via base cache handle

                if (_serializer != null)
                {
                    _cache.Set(key, _serializer.SerializeCacheItem(item), GetPolicy(item), region);
                }
                else
                {
                    _cache.Set(key, item, GetPolicy(item), region);
                }
            }

            return(item);
        }
            protected override object DeserializeObject(ArraySegment <byte> value)
            {
                var position    = value.Offset;
                var typeNameLen = BitConverter.ToUInt16(value.Array, position);

                position += 2;

                var typeName = Encoding.UTF8.GetString(value.Array, position, typeNameLen);

                position += typeNameLen;
                if (value.Array[position++] != 0)
                {
                    throw new InvalidOperationException("Invalid data, stop bit not found in type name encoding.");
                }

                var data = new byte[value.Count - position + value.Offset];

                Buffer.BlockCopy(value.Array, position, data, 0, data.Length);
                return(_serializer.DeserializeCacheItem <T>(data, TypeCache.GetType(typeName)));
            }
Exemple #3
0
        protected override CacheItem <TCacheValue> GetCacheItemInternal(string key, string region)
        {
            var fullKey = GetKey(key, region);

            CacheItem <TCacheValue> deserializedResult = null;

            if (_cache.TryGetValue(fullKey, out Tuple <Type, byte[]> result))
            {
                deserializedResult = _serializer.DeserializeCacheItem <TCacheValue>(result.Item2, result.Item1);

                if (deserializedResult.ExpirationMode != ExpirationMode.None && IsExpired(deserializedResult, DateTime.UtcNow))
                {
                    _cache.TryRemove(fullKey, out Tuple <Type, byte[]> removeResult);
                    TriggerCacheSpecificRemove(key, region, CacheItemRemovedReason.Expired, deserializedResult.Value);
                    return(null);
                }
            }

            return(deserializedResult);
        }