Ejemplo n.º 1
0
        /// <summary>
        ///   The number of items in the cache or in a partition, if specified.
        /// </summary>
        /// <param name="partition">The optional partition.</param>
        /// <param name="cacheReadMode">The cache read mode.</param>
        /// <returns>The number of items in the cache.</returns>
        /// <remarks>Calling this method does not extend sliding items lifetime.</remarks>
        protected override long CountInternal(string partition, CacheReadMode cacheReadMode = CacheReadMode.ConsiderExpiryDate)
        {
            // If partition has not been specified, then we use the GetCount method provided directly
            // by the MemoryCache.
            if (partition == null)
            {
                return(_store.GetCount());
            }

            // Otherwise, we need to count items, which is surely slower. In fact, we also need to
            // deserialize the key in order to understand if the item belongs to the partition.
            return(_store.Count(x => DeserializeCacheKey(x.Key).Partition == partition));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Clears this instance or a partition, if specified.
        /// </summary>
        /// <param name="partition">The optional partition.</param>
        /// <param name="cacheReadMode">The cache read mode.</param>
        /// <returns>The number of items that have been removed.</returns>
        protected override long ClearInternal(string partition, CacheReadMode cacheReadMode = CacheReadMode.IgnoreExpiryDate)
        {
            // We need to make a snapshot of the keys, since the cache might be used by other
            // processes. Therefore, we start projecting all keys.
            var keys = _store.Select(x => x.Key);

            // Then, if a partition has been specified, we select only those keys that belong to that partition.
            if (partition != null)
            {
                keys = keys.Where(k => DeserializeCacheKey(k).Partition == partition);
            }

            // Now we take the snapshot of the keys.
            var keysArray = keys.ToArray();

            // At last, we can remove them safely from the store itself.
            foreach (var key in keys)
            {
                _store.Remove(key);
            }

            return(keysArray.LongLength);
        }
Ejemplo n.º 3
0
 /// <summary>
 ///   The number of items in the cache or in a partition, if specified.
 /// </summary>
 /// <param name="partition">The optional partition.</param>
 /// <param name="cacheReadMode">The cache read mode.</param>
 /// <returns>The number of items in the cache.</returns>
 /// <remarks>Calling this method does not extend sliding items lifetime.</remarks>
 protected override long CountInternal(string partition, CacheReadMode cacheReadMode = CacheReadMode.ConsiderExpiryDate) => 0L;
Ejemplo n.º 4
0
 /// <summary>
 ///   Clears this instance or a partition, if specified.
 /// </summary>
 /// <param name="partition">The optional partition.</param>
 /// <param name="cacheReadMode">The cache read mode.</param>
 /// <returns>The number of items that have been removed.</returns>
 protected override long ClearInternal(string partition, CacheReadMode cacheReadMode = CacheReadMode.IgnoreExpiryDate) => 0L;