Example #1
0
        /// <summary>
        /// Puts the result to cache.
        /// </summary>
        private void PutResultToCache(DbCacheKey key, object result, DbQueryInfo queryInfo)
        {
            var expiration = _commandInfo.Policy != null
                ? _commandInfo.Policy.GetExpirationTimeout(queryInfo)
                : TimeSpan.MaxValue;

            _commandInfo.Cache.PutItem(key, result, expiration);
        }
Example #2
0
        /// <summary>
        /// Puts the item to cache.
        /// </summary>
        public void PutItem(DbCacheKey key, object value, TimeSpan absoluteExpiration)
        {
            using (var stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, value);

                var valueBytes = stream.ToArray();

                var cache = GetCacheWithExpiry(absoluteExpiration);

                ((ICacheInternal)cache).DoOutInOpExtension <object>(ExtensionId, OpPutItem, w =>
                {
                    WriteKey(key, w, true);

                    w.WriteByteArray(valueBytes);
                }, null);
            }
        }
Example #3
0
        /// <summary>
        /// Gets the item from cache.
        /// </summary>
        public bool GetItem(DbCacheKey key, out object value)
        {
            var valueBytes = ((ICacheInternal)_cache).DoOutInOpExtension(ExtensionId, OpGetItem,
                                                                         w => WriteKey(key, w, false), r => r.ReadObject <byte[]>());

            if (valueBytes == null)
            {
                value = null;

                return(false);
            }

            using (var ms = new MemoryStream(valueBytes))
            {
                value = new BinaryFormatter().Deserialize(ms);
            }

            return(true);
        }
Example #4
0
        /// <summary>
        /// Writes the key.
        /// </summary>
        private static void WriteKey(DbCacheKey key, IBinaryRawWriter writer, bool includeNames)
        {
            writer.WriteString(key.Key);

            if (key.EntitySetVersions != null)
            {
                writer.WriteInt(key.EntitySetVersions.Count);

                // Versions should be in the same order, so we can't iterate over the dictionary.
                foreach (var entitySet in key.EntitySets)
                {
                    writer.WriteLong(key.EntitySetVersions[entitySet.Name]);

                    if (includeNames)
                    {
                        writer.WriteString(entitySet.Name);
                    }
                }
            }
            else
            {
                writer.WriteInt(-1);
            }
        }
Example #5
0
        /// <summary>
        /// Writes the key.
        /// </summary>
        private static void WriteKey(DbCacheKey key, IBinaryRawWriter writer, bool includeNames)
        {
            writer.WriteString(key.Key);

            if (key.EntitySetVersions != null)
            {
                writer.WriteInt(key.EntitySetVersions.Count);

                // Versions should be in the same order, so we can't iterate over the dictionary.
                foreach (var entitySet in key.EntitySets)
                {
                    writer.WriteLong(key.EntitySetVersions[entitySet.Name]);

                    if (includeNames)
                        writer.WriteString(entitySet.Name);
                }
            }
            else
            {
                writer.WriteInt(-1);
            }
        }
Example #6
0
        /// <summary>
        /// Puts the item to cache.
        /// </summary>
        public void PutItem(DbCacheKey key, object value, TimeSpan absoluteExpiration)
        {
            using (var stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, value);

                var valueBytes = stream.ToArray();

                var cache = GetCacheWithExpiry(absoluteExpiration);

                ((ICacheInternal)cache).DoOutInOpExtension<object>(ExtensionId, OpPutItem, w =>
                {
                    WriteKey(key, w, true);

                    w.WriteByteArray(valueBytes);
                }, null);
            }
        }
Example #7
0
        /// <summary>
        /// Gets the item from cache.
        /// </summary>
        public bool GetItem(DbCacheKey key, out object value)
        {
            var valueBytes = ((ICacheInternal) _cache).DoOutInOpExtension(ExtensionId, OpGetItem,
                w => WriteKey(key, w, false), r => r.ReadObject<byte[]>());

            if (valueBytes == null)
            {
                value = null;

                return false;
            }

            using (var ms = new MemoryStream(valueBytes))
            {
                value = new BinaryFormatter().Deserialize(ms);
            }

            return true;
        }
Example #8
0
        /// <summary>
        /// Puts the result to cache.
        /// </summary>
        private void PutResultToCache(DbCacheKey key, object result, DbQueryInfo queryInfo)
        {
            var expiration = _commandInfo.Policy != null
                ? _commandInfo.Policy.GetExpirationTimeout(queryInfo)
                : TimeSpan.MaxValue;

            _commandInfo.Cache.PutItem(key, result, expiration);
        }