|
| 1 | +using Couchbase; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace DevGuide |
| 9 | +{ |
| 10 | + public class Expiration : ConnectionBase |
| 11 | + { |
| 12 | + public override async Task ExecuteAsync() |
| 13 | + { |
| 14 | + var key = "dotnetDevguideExampleExpiration-" + DateTime.Now.Ticks; |
| 15 | + |
| 16 | + // Creating a document with an time-to-live (expiration) of 2 seconds |
| 17 | + await _bucket.UpsertAsync(key, "Hello world!", TimeSpan.FromSeconds(2)); |
| 18 | + |
| 19 | + // Retrieving immediately |
| 20 | + var result = await _bucket.GetAsync<string>(key); |
| 21 | + Console.WriteLine("[{0:HH:mm:ss.fff}] Got: '{1}', Status: {2}", DateTime.Now, result.Value, result.Status); |
| 22 | + |
| 23 | + // Waiting 4 seconds |
| 24 | + await Task.Delay(4000); |
| 25 | + |
| 26 | + // Retrieving after a 4 second delay |
| 27 | + var result2 = await _bucket.GetAsync<string>(key); |
| 28 | + Console.WriteLine("[{0:HH:mm:ss.fff}] Got: '{1}', Status: {2}", DateTime.Now, result2.Value, result2.Status); |
| 29 | + |
| 30 | + |
| 31 | + // Creating an item with 1 second TTL |
| 32 | + await _bucket.UpsertAsync(key, "Hello world!", TimeSpan.FromSeconds(1)); |
| 33 | + |
| 34 | + // Retrieving the item and extending the TTL to 2 seconds with getAndTouch |
| 35 | + var result3 = await _bucket.GetAndTouchAsync<string>(key, TimeSpan.FromSeconds(2)); |
| 36 | + Console.WriteLine("[{0:HH:mm:ss.fff}] Got: '{1}', Status: {2}", DateTime.Now, result3.Value, result3.Status); |
| 37 | + |
| 38 | + // Waiting 4 seconds again |
| 39 | + await Task.Delay(4000); |
| 40 | + |
| 41 | + var result4 = await _bucket.GetAsync<string>(key); |
| 42 | + Console.WriteLine("[{0:HH:mm:ss.fff}] Got: '{1}', Status: {2}", DateTime.Now, result4.Value, result4.Status); |
| 43 | + |
| 44 | + |
| 45 | + // Creating an item without expiration |
| 46 | + await _bucket.UpsertAsync(key, "Hello world!"); |
| 47 | + |
| 48 | + // Updating the TTL with Touch |
| 49 | + var result5 = await _bucket.TouchAsync(key, TimeSpan.FromSeconds(2)); |
| 50 | + Console.WriteLine("[{0:HH:mm:ss.fff}] Got: '{1}', Status: {2}", DateTime.Now, "N/A", result5.Status); |
| 51 | + |
| 52 | + // Waiting 4 seconds yet again |
| 53 | + await Task.Delay(4000); |
| 54 | + |
| 55 | + var result6 = await _bucket.GetAsync<string>(key); |
| 56 | + Console.WriteLine("[{0:HH:mm:ss.fff}] Got: '{1}', Status: {2}", DateTime.Now, result6.Value, result6.Status); |
| 57 | + } |
| 58 | + |
| 59 | + static void Main(string[] args) |
| 60 | + { |
| 61 | + new Expiration().ExecuteAsync().Wait(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments