public async Task SetAsync_RemoveAsync() { var key = "key"; var value = new byte[] { 0x20, 0x20, 0x20, }; var prefixedKey = $"{RedisCacheOptions.InstanceName}{key}"; await L1L2Cache.SetAsync(key, value); Assert.Equal( value, await L1L2Cache.GetAsync(key)); Assert.Equal( value, L1Cache.Get(prefixedKey)); Assert.Equal( value, await L2Cache.GetAsync(key)); await L1L2Cache.RemoveAsync(key); Assert.Null( await L1L2Cache.GetAsync(key)); Assert.Null( L1Cache.Get(prefixedKey)); Assert.Null( await L2Cache.GetAsync(key)); }
public void Set_Remove() { var key = "key"; var value = new byte[] { 0x20, 0x20, 0x20, }; var prefixedKey = $"{RedisCacheOptions.InstanceName}{key}"; L1L2Cache.Set(key, value); Assert.Equal( value, L1L2Cache.Get(key)); Assert.Equal( value, L1Cache.Get(prefixedKey)); Assert.Equal( value, L2Cache.Get(key)); L1L2Cache.Remove(key); Assert.Null( L1L2Cache.Get(key)); Assert.Null( L1Cache.Get(prefixedKey)); Assert.Null( L2Cache.Get(key)); }
/// <summary> /// Gets a value with the given key. /// </summary> /// <param name="key">A string identifying the requested value.</param> /// <param name="cancellationToken">Optional. The System.Threading.CancellationToken used to propagate notifications that the operation should be canceled.</param> /// <returns>The System.Threading.Tasks.Task that represents the asynchronous operation, containing the located value or null.</returns> public async Task <byte[]?> GetAsync( string key, CancellationToken cancellationToken = default) { var value = L1Cache.Get( $"{L1L2RedisCacheOptions.KeyPrefix}{key}") as byte[]; if (value == null) { if (await Database.Value.KeyExistsAsync( $"{L1L2RedisCacheOptions.KeyPrefix}{key}")) { var semaphore = await GetOrCreateLockAsync( key, null, cancellationToken); await semaphore.WaitAsync(cancellationToken); try { var hashEntries = await GetHashEntriesAsync(key); var distributedCacheEntryOptions = hashEntries .GetDistributedCacheEntryOptions(); value = hashEntries.GetRedisValue(); SetMemoryCache( key, value, distributedCacheEntryOptions); SetLock( key, semaphore, distributedCacheEntryOptions); } finally { semaphore.Release(); } } } return(value); }
/// <summary> /// Gets a value with the given key. /// </summary> /// <param name="key">A string identifying the requested value.</param> /// <returns>The located value or null.</returns> public byte[]? Get(string key) { var value = L1Cache.Get( $"{L1L2RedisCacheOptions.KeyPrefix}{key}") as byte[]; if (value == null) { if (Database.Value.KeyExists( $"{L1L2RedisCacheOptions.KeyPrefix}{key}")) { var semaphore = GetOrCreateLock( key, null); semaphore.Wait(); try { var hashEntries = GetHashEntries(key); var distributedCacheEntryOptions = hashEntries .GetDistributedCacheEntryOptions(); value = hashEntries.GetRedisValue(); SetMemoryCache( key, value, distributedCacheEntryOptions); SetLock( key, semaphore, distributedCacheEntryOptions); } finally { semaphore.Release(); } } } return(value); }
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); }