private async Task AssertGetCacheItemFromDatabaseAsync( PostgreSqlCache cache, string key, byte[] expectedValue, TimeSpan?slidingExpiration, DateTimeOffset?absoluteExpiration, DateTimeOffset expectedExpirationTime) { var value = await cache.GetAsync(key); Assert.NotNull(value); Assert.Equal(expectedValue, value); var cacheItemInfo = await GetCacheItemFromDatabaseAsync(key); Assert.NotNull(cacheItemInfo); Assert.Equal(slidingExpiration, cacheItemInfo.SlidingExpirationInSeconds); Assert.Equal(absoluteExpiration, cacheItemInfo.AbsoluteExpiration); Assert.Equal(expectedExpirationTime, cacheItemInfo.ExpiresAtTime); }
public static async Task RunSampleAsync() { var configurationBuilder = new ConfigurationBuilder(); var configuration = configurationBuilder .AddJsonFile("config.json") .AddEnvironmentVariables() .Build(); var key = Guid.NewGuid().ToString(); var message = "Hello, World!"; var value = Encoding.UTF8.GetBytes(message); Console.WriteLine("Connecting to cache"); var cache = new PostgreSqlCache(new PostgreSqlCacheOptions() { ConnectionString = configuration["ConnectionString"], SchemaName = configuration["SchemaName"], TableName = configuration["TableName"] }); Console.WriteLine("Connected"); Console.WriteLine("Cache item key: {0}", key); Console.WriteLine($"Setting value '{message}' in cache"); await cache.SetAsync( key, value, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(600))); Console.WriteLine("Set"); Console.WriteLine("Getting value from cache"); value = await cache.GetAsync(key); if (value != null) { Console.WriteLine("Retrieved: " + Encoding.UTF8.GetString(value, 0, value.Length)); } else { Console.WriteLine("Not Found"); } Console.WriteLine("Refreshing value in cache"); await cache.RefreshAsync(key); Console.WriteLine("Refreshed"); Console.WriteLine("Removing value from cache"); await cache.RemoveAsync(key); Console.WriteLine("Removed"); Console.WriteLine("Getting value from cache again"); value = await cache.GetAsync(key); if (value != null) { Console.WriteLine("Retrieved: " + Encoding.UTF8.GetString(value, 0, value.Length)); } else { Console.WriteLine("Not Found"); } Console.ReadLine(); }