Example #1
0
        protected override bool AddInternalPrepared(CacheItem <TCacheValue> item)
        {
            NotNull(item, nameof(item));

            var key = GetKey(item.Key, item.Region);

            var serializedItem = _serializer.SerializeCacheItem(item);

            return(_cache.TryAdd(key, new Tuple <Type, byte[]>(item.Value.GetType(), serializedItem)));
        }
Example #2
0
            protected override ArraySegment <byte> SerializeObject(object value)
            {
                var cacheItem = value as CacheItem <T>;

                if (cacheItem == null)
                {
                    throw new ArgumentException($"Value is not {nameof(CacheItem<T>)}.", nameof(value));
                }

                var typeName        = cacheItem.Value.GetType().AssemblyQualifiedName;
                var typeNameBytes   = Encoding.UTF8.GetBytes(typeName);
                var typeBytesLength = BitConverter.GetBytes((ushort)typeNameBytes.Length);
                var data            = _serializer.SerializeCacheItem(cacheItem);

                var result = new byte[typeNameBytes.Length + typeBytesLength.Length + data.Length + 1];

                /* Encoding the actual item value Type into the cached item
                 *
                 * | 0 - 1 | 2 - len | len + 1 | ...
                 * |  len  |TypeName |0 - stop | data
                 */
                Buffer.BlockCopy(typeBytesLength, 0, result, 0, typeBytesLength.Length);
                Buffer.BlockCopy(typeNameBytes, 0, result, typeBytesLength.Length, typeNameBytes.Length);
                Buffer.BlockCopy(data, 0, result, typeBytesLength.Length + typeNameBytes.Length + 1, data.Length);

                return(new ArraySegment <byte>(result));
            }
Example #3
0
        /// <summary>
        /// Adds a value to the cache.
        /// </summary>
        /// <param name="item">The <c>CacheItem</c> to be added to the cache.</param>
        /// <returns>
        /// <c>true</c> if the key was not already added to the cache, <c>false</c> otherwise.
        /// </returns>
        protected override bool AddInternalPrepared(CacheItem <TCacheValue> item)
        {
            if (_cache.Contains(item.Key, item.Region))
            {
                return(false);
            }

            var policy = GetPolicy(item);

            if (_serializer != null)
            {
                return(_cache.Add(item.Key, _serializer.SerializeCacheItem(item), policy, item.Region));
            }
            else
            {
                return(_cache.Add(item.Key, item, policy, item.Region));
            }
        }