Example #1
0
        /// <inheritdoc />
        /// <summary>
        /// Sets an item into cache
        /// </summary>
        /// <typeparam name="T">The type of item</typeparam>
        /// <param name="key">Name of the item in cache</param>
        /// <param name="item">The item being set into cache</param>
        public virtual void Set <T>(string key, T item)
        {
            var expiration = DateTimeOffset.Now.AddSeconds(Options.Seconds);

            // Optionally place in L1 Cache
            if (Options.UseLocalCache)
            {
                var l1Options = new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = expiration,
                    Priority           = Options.Priority
                };
                L1Cache.Set(key, item, l1Options);
            }

            // Optionally place in L2 Cache
            if (Options.UseDistributedCache)
            {
                // If these calls fail, do nothing
                try
                {
                    var obj       = new L2CacheItem <T>(Options.Seconds, Options.Priority, item);
                    var l2Options = new DistributedCacheEntryOptions {
                        AbsoluteExpiration = expiration
                    };
                    L2Cache.Set(key, obj.ToByteArray(), l2Options);
                }
                catch { }
            }
        }
Example #2
0
        /// <inheritdoc />
        /// <summary>
        /// Sets an item into cache
        /// </summary>
        /// <typeparam name="T">The type of item</typeparam>
        /// <param name="key">Name of the item in cache</param>
        /// <param name="item">The item being set into cache</param>
        public virtual void Set <T>(string key, T item)
        {
            var expiration = DateTimeOffset.Now.AddSeconds(Options.Seconds);

            // If these calls fail, do nothing
            try
            {
                var obj       = new L2CacheItem <T>(Options.Seconds, Options.Priority, item);
                var l2Options = new DistributedCacheEntryOptions {
                    AbsoluteExpiration = expiration
                };
                L2Cache.Set(key, obj.ToByteArray(), l2Options);
            }
            catch { }
        }
    /// <summary>
    /// Sets a value with the given key.
    /// </summary>
    /// <param name="key">A string identifying the requested value.</param>
    /// <param name="value">The value to set in the cache.</param>
    /// <param name="distributedCacheEntryOptions">The cache options for the value.</param>
    public void Set(
        string key,
        byte[] value,
        DistributedCacheEntryOptions distributedCacheEntryOptions)
    {
        var semaphore = GetOrCreateLock(
            key, distributedCacheEntryOptions);

        semaphore.Wait();
        try
        {
            L2Cache.Set(
                key, value, distributedCacheEntryOptions);
            SetMemoryCache(
                key, value, distributedCacheEntryOptions);
            MessagePublisher.Publish(key);
        }
        finally
        {
            semaphore.Release();
        }
    }