/// <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 { } } }
public virtual async Task Set(string key, object value, CachePolicyItem cachePolicy) { //清除一级缓存,写入二级缓存 await L1Cache.Remove(key); await L2Cache.Set(key, value, cachePolicy); //后台写入一级缓存。 Background(L1Cache.Set(key, value, cachePolicy)); }
/// <inheritdoc /> /// <summary> /// Retrieves an item from the Cache /// </summary> /// <typeparam name="T">The type of item</typeparam> /// <param name="key">Name of the item in cache</param> /// <param name="item">If the object is not found in the Cache, this will be populated</param> /// <returns>True if the object was found, False if not found</returns> public virtual bool Get <T>(string key, out T item) { // If in L1 cache, just return it if (Options.UseLocalCache && L1Cache.TryGetValue(key, out item)) { return(true); } // Check L2 cache if (Options.UseDistributedCache) { byte[] bytes = null; try { bytes = L2Cache.Get(key); } catch { } // Was not found if (bytes == null) { item = default; return(false); } // Object was found var obj = bytes.FromByteArray <L2CacheItem <T> >(); item = obj.Item; // Store the object back into L1 cache // ReSharper disable once InvertIf if (Options.UseLocalCache) { var options = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = obj.RemainingCacheTime(), Priority = obj.Priority }; L1Cache.Set(key, item, options); } // Return it now return(true); } item = default; return(false); }
private SemaphoreSlim SetLock( string key, SemaphoreSlim semaphore, DistributedCacheEntryOptions distributedCacheEntryOptions) { var memoryCacheEntryOptions = new MemoryCacheEntryOptions { AbsoluteExpiration = distributedCacheEntryOptions?.AbsoluteExpiration, AbsoluteExpirationRelativeToNow = distributedCacheEntryOptions?.AbsoluteExpirationRelativeToNow, SlidingExpiration = distributedCacheEntryOptions?.SlidingExpiration, }; return(L1Cache.Set( $"{L1L2RedisCacheOptions.LockKeyPrefix}{key}", semaphore, memoryCacheEntryOptions)); }
private void SetMemoryCache( string key, byte[] value, DistributedCacheEntryOptions distributedCacheEntryOptions) { var memoryCacheEntryOptions = new MemoryCacheEntryOptions { AbsoluteExpiration = distributedCacheEntryOptions?.AbsoluteExpiration, AbsoluteExpirationRelativeToNow = distributedCacheEntryOptions?.AbsoluteExpirationRelativeToNow, SlidingExpiration = distributedCacheEntryOptions?.SlidingExpiration, }; if (!memoryCacheEntryOptions.SlidingExpiration.HasValue) { L1Cache.Set( $"{L1L2RedisCacheOptions.KeyPrefix}{key}", value, memoryCacheEntryOptions); } }
public virtual async Task <object> Get(string key) { var value = await L1Cache.Get(key); if (value != null) { return(value); } CachePolicyItem cachePolicy; value = await L2Cache.Get(key, out cachePolicy); if (value == null) { return(null); } //后台写入一级缓存 Background(L1Cache.Set(key, value, cachePolicy)); return(value); }