public void Set([NotNull] string key, byte[] value, DistributedCacheEntryOptions options) { var memoryCacheEntryOptions = new MemoryCacheEntryOptions(); memoryCacheEntryOptions.AbsoluteExpiration = options.AbsoluteExpiration; memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; memoryCacheEntryOptions.SlidingExpiration = options.SlidingExpiration; _memCache.Set(key, value, memoryCacheEntryOptions); }
public void Set([NotNull] string key, byte[] value, DistributedCacheEntryOptions options) { Connect(); var creationTime = DateTimeOffset.UtcNow; var result = _cache.ScriptEvaluate(SetScript, new RedisKey[] { _instance + key }, new RedisValue[] { options.AbsoluteExpiration?.Ticks ?? NotPresent, options.SlidingExpiration?.Ticks ?? NotPresent, GetExpirationInSeconds(creationTime, options) ?? NotPresent, value }); }
protected DateTimeOffset? GetAbsoluteExpiration(DateTimeOffset utcNow, DistributedCacheEntryOptions options) { // calculate absolute expiration DateTimeOffset? absoluteExpiration = null; if (options.AbsoluteExpirationRelativeToNow.HasValue) { absoluteExpiration = utcNow.Add(options.AbsoluteExpirationRelativeToNow.Value); } else if (options.AbsoluteExpiration.HasValue) { if (options.AbsoluteExpiration.Value <= utcNow) { throw new InvalidOperationException("The absolute expiration value must be in the future."); } absoluteExpiration = options.AbsoluteExpiration.Value; } return absoluteExpiration; }
public virtual async Task SetCacheItemAsync(string key, byte[] value, DistributedCacheEntryOptions options) { var utcNow = SystemClock.UtcNow; var absoluteExpiration = GetAbsoluteExpiration(utcNow, options); ValidateOptions(options.SlidingExpiration, absoluteExpiration); using (var connection = new SqlConnection(ConnectionString)) { var upsertCommand = new SqlCommand(SqlQueries.SetCacheItem, connection); upsertCommand.Parameters .AddCacheItemId(key) .AddCacheItemValue(value) .AddSlidingExpirationInSeconds(options.SlidingExpiration) .AddAbsoluteExpiration(absoluteExpiration) .AddWithValue("UtcNow", SqlDbType.DateTimeOffset, utcNow); await connection.OpenAsync(); try { await upsertCommand.ExecuteNonQueryAsync(); } catch (SqlException ex) { if (IsDuplicateKeyException(ex)) { // There is a possibility that multiple requests can try to add the same item to the cache, in // which case we receive a 'duplicate key' exception on the primary key column. } else { throw ex; } } } }
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options) { throw new NotImplementedException(); }
public Task SetAsync([NotNull] string key, byte[] value, DistributedCacheEntryOptions options) { Set(key, value, options); return CompletedTask; }
private static long? GetExpirationInSeconds(DateTimeOffset creationTime, DistributedCacheEntryOptions options) { // Calculate absolute expiration time DateTimeOffset? absoluteExpiration = null; if (options.AbsoluteExpirationRelativeToNow.HasValue) { absoluteExpiration = creationTime + options.AbsoluteExpirationRelativeToNow; } else if (options.AbsoluteExpiration.HasValue) { if (options.AbsoluteExpiration <= creationTime) { throw new ArgumentOutOfRangeException( nameof(DistributedCacheEntryOptions.AbsoluteExpiration), options.AbsoluteExpiration.Value, "The absolute expiration value must be in the future."); } absoluteExpiration = options.AbsoluteExpiration; } if (absoluteExpiration.HasValue && options.SlidingExpiration.HasValue) { return (long)Math.Min( (absoluteExpiration.Value - creationTime).TotalSeconds, options.SlidingExpiration.Value.TotalSeconds); } else if (absoluteExpiration.HasValue) { return (long)(absoluteExpiration.Value - creationTime).TotalSeconds; } else if (options.SlidingExpiration.HasValue) { return (long)options.SlidingExpiration.Value.TotalSeconds; } return null; }
public async Task SetAsync( 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)); } await _dbOperations.SetCacheItemAsync(key, value, options); ScanForExpiredItemsIfRequired(); }
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options) { if (key == null) { throw new ArgumentNullException(nameof(key)); } Set(key, value, options); return CompletedTask; }
public void Set(string key, byte[] value, DistributedCacheEntryOptions options) { if (key == null) { throw new ArgumentNullException(nameof(key)); } var memoryCacheEntryOptions = new MemoryCacheEntryOptions(); memoryCacheEntryOptions.AbsoluteExpiration = options.AbsoluteExpiration; memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow = options.AbsoluteExpirationRelativeToNow; memoryCacheEntryOptions.SlidingExpiration = options.SlidingExpiration; _memCache.Set(key, value, memoryCacheEntryOptions); }
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options) { if (key == null) { throw new ArgumentNullException(nameof(key)); } await ConnectAsync(); var creationTime = DateTimeOffset.UtcNow; await _cache.ScriptEvaluateAsync(SetScript, new RedisKey[] { _instance + key }, new RedisValue[] { options.AbsoluteExpiration?.Ticks ?? NotPresent, options.SlidingExpiration?.Ticks ?? NotPresent, GetExpirationInSeconds(creationTime, options) ?? NotPresent, value }); }