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 SqlServerCache(new SqlServerCacheOptions() { 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(10))); 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(); }
public async Task RunSampleAsync() { var key = Guid.NewGuid().ToString(); var message = "Hello, World!"; var value = Encoding.UTF8.GetBytes(message); Console.WriteLine("Connecting to cache"); var cache = new SqlServerCache( new CacheOptions( new SqlServerCacheOptions() { ConnectionString = Configuration.Get("ConnectionString"), SchemaName = Configuration.Get("SchemaName"), TableName = Configuration.Get("TableName") })); await cache.ConnectAsync(); 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(10))); Console.WriteLine("Set"); Console.WriteLine("Getting value from cache"); value = await cache.GetAsync(key); if (value != null) { Console.WriteLine("Retrieved: " + Encoding.UTF8.GetString(value)); } 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)); } else { Console.WriteLine("Not Found"); } Console.ReadLine(); }