/// <summary> /// Removes all entities from the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> public void Clear(CacheDatabaseType database) { EndPoint[] endpoints; IServer server; try { if (!IsEnabled) { return; } if (connection == null) { connection = ConnectionMultiplexer.Connect( service.Configuration.RedisCacheConnectionString); } endpoints = connection.GetEndPoints(true); foreach (EndPoint ep in endpoints) { server = connection.GetServer(ep); server.FlushDatabase((int)database); } } finally { endpoints = null; server = null; } }
/// <summary> /// Removes all entities from the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> public async Task ClearAsync(CacheDatabaseType database) { EndPoint[] endpoints; IServer server; try { if (!this.IsEnabled) { return; } if (this.connection == null) { this.connection = await ConnectionMultiplexer.ConnectAsync( this.service.Configuration.RedisCacheConnectionString); } endpoints = this.connection.GetEndPoints(true); foreach (EndPoint ep in endpoints) { server = this.connection.GetServer(ep); await server.FlushDatabaseAsync((int)database); } } finally { endpoints = null; server = null; } }
/// <summary> /// Fetches the specified entity from the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <returns> /// The entity associated with the specified key. /// </returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public TEntity Fetch <TEntity>(CacheDatabaseType database, string key) where TEntity : class { key.AssertNotEmpty(nameof(key)); IDatabase cache = GetCacheReference(database); RedisValue value = cache.StringGet(key); return(value.HasValue ? DecompressEntity <TEntity>(value) : null); }
/// <summary> /// Fetches the specified entity from the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <returns> /// The entity associated with the specified key. /// </returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public async Task <TEntity> FetchAsync <TEntity>(CacheDatabaseType database, string key) where TEntity : class { key.AssertNotEmpty(nameof(key)); IDatabase cache = await GetCacheReferenceAsync(database); RedisValue value = await cache.StringGetAsync(key); return(value.HasValue ? DecompressEntity <TEntity>(value) : null); }
/// <summary> /// Obtains a reference to the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A reference to the appropriate cache database.</returns> private IDatabase GetCacheReference(CacheDatabaseType database) { if (connection == null) { connection = ConnectionMultiplexer.Connect( provider.Configuration.RedisCacheConnectionString.ToUnsecureString()); } return(connection.GetDatabase((int)database)); }
/// <summary> /// Obtains a reference to the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A reference to the appropriate cache database.</returns> private async Task <IDatabase> GetCacheReferenceAsync(CacheDatabaseType database) { if (connection == null) { connection = await ConnectionMultiplexer.ConnectAsync( provider.Configuration.RedisCacheConnectionString.ToUnsecureString()); } return(connection.GetDatabase((int)database)); }
/// <summary> /// Stores the specified entity in the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data should be stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <param name="entity">The object to be cached.</param> /// <param name="expiration">When the entity in the cache should expire.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> /// <exception cref="ArgumentNullException"> /// entity /// </exception> public async Task StoreAsync <TEntity>(CacheDatabaseType database, string key, TEntity entity, TimeSpan?expiration = null) where TEntity : class { key.AssertNotEmpty(nameof(key)); entity.AssertNotNull(nameof(entity)); IDatabase cache = await GetCacheReferenceAsync(database); await cache.StringSetAsync( key, CompressEntity(entity), expiration); }
/// <summary> /// Stores the specified entity in the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data should be stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <param name="entity">The object to be cached.</param> /// <param name="expiration">When the entity in the cache should expire.</param> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> /// <exception cref="ArgumentNullException"> /// entity /// </exception> public void Store <TEntity>(CacheDatabaseType database, string key, TEntity entity, TimeSpan?expiration = null) where TEntity : class { key.AssertNotEmpty(nameof(key)); entity.AssertNotNull(nameof(entity)); IDatabase cache = GetCacheReference(database); cache.StringSet( key, CompressEntity(entity), expiration); }
/// <summary> /// Deletes the entity with specified key. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">The key of the entity to be deleted.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public async Task DeleteAsync(CacheDatabaseType database, string key) { key.AssertNotEmpty(nameof(key)); if (!IsEnabled) { return; } IDatabase cache = this.GetCacheReference(database); await cache.KeyDeleteAsync(key); }
/// <summary> /// Deletes the entity with specified key. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">The key of the entity to be deleted.</param> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public void Delete(CacheDatabaseType database, string key) { key.AssertNotEmpty(nameof(key)); if (!this.IsEnabled) { return; } IDatabase cache = this.GetCacheReference(database); cache.KeyDelete(key); }
/// <summary> /// Fetches the specified entity from the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <returns> /// The entity associated with the specified key. /// </returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public async ValueTask <TEntity> FetchAsync <TEntity>(CacheDatabaseType database, string key) where TEntity : class { key.AssertNotEmpty(nameof(key)); if (!this.IsEnabled) { return(null); } IDatabase cache = this.GetCacheReference(database); RedisValue value = await cache.StringGetAsync(key); return(value.HasValue ? JsonConvert.DeserializeObject <TEntity>(this.protector.Unprotect(value)) : null); }
/// <summary> /// Fetches the specified entity from the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <returns> /// The entity associated with the specified key. /// </returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public TEntity Fetch <TEntity>(CacheDatabaseType database, string key) where TEntity : class { key.AssertNotEmpty(nameof(key)); if (!IsEnabled) { return(null); } IDatabase cache = GetCacheReference(database); RedisValue value = cache.StringGet(key); return(value.HasValue ? JsonConvert.DeserializeObject <TEntity>(protector.Unprotect(value)) : null); }
/// <summary> /// Stores the specified entity in the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="database">Cache database type where the data should be stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <param name="entity">The object to be cached.</param> /// <param name="expiration">When the entity in the cache should expire.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> /// <exception cref="ArgumentNullException"> /// entity /// </exception> public async Task StoreAsync <TEntity>(CacheDatabaseType database, string key, TEntity entity, TimeSpan?expiration = null) where TEntity : class { key.AssertNotEmpty(nameof(key)); entity.AssertNotNull(nameof(entity)); if (!this.IsEnabled) { return; } IDatabase cache = await GetCacheReferenceAsync(database); await cache.StringSetAsync( key, protector.Protect(JsonConvert.SerializeObject(entity)), expiration); }
/// <summary> /// Stores the specified entity in the cache. /// </summary> /// <typeparam name="TEntity">The type of the entity in cache.</typeparam> /// <param name="cacheDatabase">Cache database type where the data should be stored.</param> /// <param name="key">A unique identifier for the cache entry.</param> /// <param name="entity">The object to be cached.</param> /// <param name="expiration">When the entity in the cache should expire.</param> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> /// <exception cref="ArgumentNullException"> /// entity /// </exception> public void Store <TEntity>(CacheDatabaseType cacheDatabase, string key, TEntity entity, TimeSpan?expiration = null) where TEntity : class { key.AssertNotEmpty(nameof(key)); entity.AssertNotNull(nameof(entity)); if (!this.IsEnabled) { return; } IDatabase cache = this.GetCacheReference(cacheDatabase); cache.StringSet( key, this.protector.Protect(JsonConvert.SerializeObject(entity)), expiration); }
/// <summary> /// Obtains a reference to the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A reference to the appropriate cache database.</returns> private async Task <IDatabase> GetCacheReferenceAsync(CacheDatabaseType database) { if (connection != null) { return(connection.GetDatabase((int)database)); } try { connection = await ConnectionMultiplexer.ConnectAsync( service.Configuration.RedisCacheConnectionString); return(connection.GetDatabase((int)database)); } catch (RedisConnectionException ex) { throw new CacheException(Resources.RedisCacheConnectionException, ex); } }
/// <summary> /// Obtains a reference to the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A reference to the appropriate cache database.</returns> private async Task <IDatabase> GetCacheReferenceAsync(CacheDatabaseType database) { if (this.connection != null) { return(this.connection.GetDatabase((int)database)); } try { this.connection = await ConnectionMultiplexer.ConnectAsync( this.service.Configuration.RedisCacheConnectionString); return(this.connection.GetDatabase((int)database)); } catch (RedisConnectionException ex) { throw new CacheException("Failed to connect to the instance of Redis Cache.", ex); } }
/// <summary> /// Removes all entities from the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> public async Task ClearAsync(CacheDatabaseType database) { await Task.FromResult(0).ConfigureAwait(false); }
/// <summary> /// Deletes the entity with specified key. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">The key of the entity to be deleted.</param> /// <returns>An instance of <see cref="Task"/> that represents the asynchronous operation.</returns> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public async Task DeleteAsync(CacheDatabaseType database, string key = null) { IDatabase cache = await GetCacheReferenceAsync(database); await cache.KeyDeleteAsync(key); }
/// <summary> /// Removes all entities from the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> public async Task ClearAsync(CacheDatabaseType database) { await Task.FromResult(0); }
/// <summary> /// Deletes the entity with specified key. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> /// <param name="key">The key of the entity to be deleted.</param> /// <exception cref="ArgumentException"> /// <paramref name="key"/> is empty or null. /// </exception> public void Delete(CacheDatabaseType database, string key = null) { IDatabase cache = GetCacheReference(database); cache.KeyDelete(key); }
/// <summary> /// Removes all entities from the specified cache database. /// </summary> /// <param name="database">Cache database type where the data is stored.</param> public void Clear(CacheDatabaseType database) { }