public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options) { DateTimeOffset? absoluteExpiration = null; var currentTime = DateTimeOffset.UtcNow; if (options.AbsoluteExpirationRelativeToNow.HasValue) { absoluteExpiration = currentTime.Add(options.AbsoluteExpirationRelativeToNow.Value); } else if (options.AbsoluteExpiration.HasValue) { if (options.AbsoluteExpiration.Value <= currentTime) { throw new ArgumentOutOfRangeException( nameof(options.AbsoluteExpiration), options.AbsoluteExpiration.Value, "The absolute expiration value must be in the future."); } absoluteExpiration = options.AbsoluteExpiration; } var item = new CachedItem(partitionKey, key, value) { LastAccessTime = currentTime }; if (absoluteExpiration.HasValue) { item.AbsolutExperiation = absoluteExpiration; } if (options.SlidingExpiration.HasValue) { item.SlidingExperiation = options.SlidingExpiration; } var op = TableOperation.InsertOrReplace(item); return this.azuretable.ExecuteAsync(op); }
private bool ShouldDelete(CachedItem data) { var currentTime = DateTimeOffset.UtcNow; if (data.AbsolutExperiation != null && data.AbsolutExperiation.Value <= currentTime) { return true; } if (data.SlidingExperiation.HasValue && data.LastAccessTime.HasValue && data.LastAccessTime.Value.Add(data.SlidingExperiation.Value) < currentTime) { return true; } return false; }