-
Notifications
You must be signed in to change notification settings - Fork 45
/
Counter.cs
43 lines (36 loc) · 1.67 KB
/
Counter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Couchbase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DevGuide
{
public class Counter : ConnectionBase
{
public override async Task ExecuteAsync()
{
var key = "dotnetDevguideExampleCounter-" + DateTime.Now.Ticks;
// Try to increment a counter that doesn't exist.
// This will create the counter with an initial value of 1 regardless of delta specified
var counter = await _bucket.IncrementAsync(key, 10);
Console.WriteLine("Initial value = N/A, Increment = 10, Counter value: " + counter.Value);
// Remove the counter so we can try again
await _bucket.RemoveAsync(key);
Console.WriteLine("Trying again.");
// Create a counter with an initial value of 13. Again, delta is ignored in this case.
var counter2 = await _bucket.IncrementAsync(key, 10, 13);
Console.WriteLine("Initial value = 13, Increment = 10, Counter value: " + counter2.Value);
// Increment the counter by 10. If the counter exists, the inital value is ignored.
var counter3 = await _bucket.IncrementAsync(key, 10, 13);
Console.WriteLine("Initial value = 13, Increment = 10, Counter value: " + counter3.Value);
// Decrement the counter by 20.
var counter4 = await _bucket.DecrementAsync(key, 20, 13);
Console.WriteLine("Initial value = 13, Decrement = 20, Counter value: " + counter4.Value);
}
static void Main(string[] args)
{
new Counter().ExecuteAsync().Wait();
}
}
}