示例#1
0
        private MemoryCacheEntryOptions GetMemoryCacheEntryOptions(byte[] value, ExtendedDistributedCacheEntryOptions options)
        {
            var memoryCacheEntryOptions = new MemoryCacheEntryOptions
            {
                AbsoluteExpiration = options.AbsoluteExpiration,
                AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow,
                SlidingExpiration = options.SlidingExpiration,
                Priority          = options.Priority,
                Size = value.Length
            };

            if (options.ExpirationTokens != null && options.ExpirationTokens.Any())
            {
                foreach (IChangeToken expirationToken in options.ExpirationTokens)
                {
                    memoryCacheEntryOptions.ExpirationTokens.Add(expirationToken);
                }
            }
            if (options.PostEvictionCallbacks != null && options.PostEvictionCallbacks.Any())
            {
                foreach (var callbackRegistration in options.PostEvictionCallbacks)
                {
                    memoryCacheEntryOptions.PostEvictionCallbacks.Add(callbackRegistration);
                }
            }
            return(memoryCacheEntryOptions);
        }
示例#2
0
        /// <summary>
        /// Adds an object to distributed cache
        /// </summary>
        /// <param name="key">Cache Key</param>
        /// <param name="value">Cache object</param>
        /// <param name="options">Distributed cache options. <see cref="DistributedCacheEntryOptions"/></param>
        public new void Set(string key, byte[] value, DistributedCacheEntryOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var cacheEntryOptions =
                new ExtendedDistributedCacheEntryOptions
            {
                AbsoluteExpiration = options.AbsoluteExpiration,
                AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow,
                SlidingExpiration = options.SlidingExpiration,
                Size = new long?((long)value.Length)
            };

            Set(key, value, cacheEntryOptions);
        }
示例#3
0
        private MemoryCacheEntryOptions GetMemoryCacheEntryOptions(ExtendedDistributedCacheEntryOptions options, long?size = default)
        {
            var memoryCacheEntryOptions = new MemoryCacheEntryOptions
            {
                AbsoluteExpiration = options.AbsoluteExpiration,
                AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow,
                SlidingExpiration = options.SlidingExpiration,
                Priority          = options.Priority
            };

            if (size != null)
            {
                memoryCacheEntryOptions.SetSize(size.Value);
            }

            if (options.ExpirationTokens != null && options.ExpirationTokens.Any())
            {
                foreach (IChangeToken expirationToken in options.ExpirationTokens)
                {
                    memoryCacheEntryOptions.ExpirationTokens.Add(expirationToken);
                }
            }
            if (options.PostEvictionCallbacks != null && options.PostEvictionCallbacks.Any())
            {
                foreach (var callbackRegistration in options.PostEvictionCallbacks)
                {
                    memoryCacheEntryOptions.PostEvictionCallbacks.Add(callbackRegistration);
                }
            }
            return(memoryCacheEntryOptions);
        }
示例#4
0
        /// <summary>
        /// Adds an object to distributed cache asynchronously
        /// </summary>
        /// <param name="key">Cache Key</param>
        /// <param name="value">Cache object</param>
        /// <param name="options">Distributed cache options. <see cref="ExtendedDistributedCacheEntryOptions"/></param>
        /// <param name="token"><see cref="CancellationToken"/> to be used while setting cache item</param>
        public Task SetAsync(string key, byte[] value, ExtendedDistributedCacheEntryOptions options, CancellationToken token = default)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Set(key, value, options);
            return(CompletedTask);
        }
示例#5
0
        /// <summary>
        /// Adds an object to distributed cache
        /// </summary>
        /// <param name="key">Cache Key</param>
        /// <param name="value">Cache object</param>
        /// <param name="options">Distributed cache options. <see cref="ExtendedDistributedCacheEntryOptions"/></param>
        public void Set(string key, byte[] value, ExtendedDistributedCacheEntryOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var memoryCacheEntryOptions = GetMemoryCacheEntryOptions(value, options);

            _memCache.Set(key, value, memoryCacheEntryOptions);
        }
示例#6
0
        private ExtendedDistributedCacheEntryOptions GetDistributedCacheEntryOptions(ICachePartition cachePartition,
                                                                                     IChangeToken expirationToken = null,
                                                                                     PostEvictionCallbackRegistration postEvictionCallback = null)
        {
            var cacheEntryOptions = new ExtendedDistributedCacheEntryOptions
            {
                AbsoluteExpiration = cachePartition.AbsoluteExpiration,
                AbsoluteExpirationRelativeToNow = cachePartition.AbsoluteExpirationRelativeToNow,
                SlidingExpiration = cachePartition.SlidingExpiration,
                Priority          = cachePartition.Priority
            };

            if (expirationToken != null)
            {
                cacheEntryOptions.ExpirationTokens.Add(expirationToken);
            }
            if (postEvictionCallback != null)
            {
                cacheEntryOptions.PostEvictionCallbacks.Add(postEvictionCallback);
            }
            return(cacheEntryOptions);
        }
示例#7
0
        /// <summary>
        /// Adds an object to distributed cache
        /// </summary>
        /// <param name="key">Cache Key</param>
        /// <param name="value">Cache object</param>
        /// <param name="options">Distributed cache options. <see cref="ExtendedDistributedCacheEntryOptions"/></param>
        public void Set(string key, byte[] value, ExtendedDistributedCacheEntryOptions options)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            using (ICacheEntry cacheEntry = CreateEntry(key))
            {
                var memoryCacheEntryOptions = GetMemoryCacheEntryOptions(options, value.LongLength);
                cacheEntry.SetOptions(memoryCacheEntryOptions);
                if (memoryCacheEntryOptions.AbsoluteExpiration != null)
                {
                    cacheEntry.SetAbsoluteExpiration(memoryCacheEntryOptions.AbsoluteExpiration.Value);
                }
                if (memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow != null)
                {
                    cacheEntry.SetAbsoluteExpiration(memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow.Value);
                }
                if (memoryCacheEntryOptions.SlidingExpiration != null)
                {
                    cacheEntry.SetSlidingExpiration(memoryCacheEntryOptions.SlidingExpiration.Value);
                }
                cacheEntry.SetPriority(memoryCacheEntryOptions.Priority);
                cacheEntry.SetSize(value.LongLength);
                cacheEntry.SetValue(value);
            }
        }
示例#8
0
 /// <summary>
 /// Adds the Cache object binary stream to distributed cache asynchronously
 /// </summary>
 /// <param name="key">Cache key</param>
 /// <param name="value">Cache object in binary stream</param>
 /// <param name="options"><see cref="ExtendedDistributedCacheEntryOptions"/> where the cache object should be added to.</param>
 /// <param name="token"><see cref="CancellationToken"/> to be used while adding cache object to distributed cache.</param>
 public async Task SetAsync(string key, byte[] value, ExtendedDistributedCacheEntryOptions options,
                            CancellationToken token = default)
 {
     await SetAsync(key, value, options as DistributedCacheEntryOptions, token);
 }
示例#9
0
 /// <summary>
 /// Adds the Cache object binary stream to distributed cache
 /// </summary>
 /// <param name="key">Cache key</param>
 /// <param name="value">Cache object in binary stream</param>
 /// <param name="options"><see cref="ExtendedDistributedCacheEntryOptions"/> where the cache object should be added to.</param>
 public void Set(string key, byte[] value, ExtendedDistributedCacheEntryOptions options)
 {
     Set(key, value, options as DistributedCacheEntryOptions);
 }