Skip to content

Commit 22597b7

Browse files
committed
Add counter and ttl examples
1 parent afbf31b commit 22597b7

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

dotnet/Counter.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Counter : ConnectionBase
11+
{
12+
public override async Task ExecuteAsync()
13+
{
14+
var key = "dotnetDevguideExampleCounter-" + DateTime.Now.Ticks;
15+
16+
// Try to increment a counter that doesn't exist.
17+
// This will create the counter with an initial value of 1 regardless of delta specified
18+
var counter = await _bucket.IncrementAsync(key, 10);
19+
Console.WriteLine("Initial value = N/A, Increment = 10, Counter value: " + counter.Value);
20+
21+
// Remove the counter so we can try again
22+
await _bucket.RemoveAsync(key);
23+
Console.WriteLine("Trying again.");
24+
25+
// Create a counter with an initial value of 13. Again, delta is ignored in this case.
26+
var counter2 = await _bucket.IncrementAsync(key, 10, 13);
27+
Console.WriteLine("Initial value = 13, Increment = 10, Counter value: " + counter2.Value);
28+
29+
// Increment the counter by 10. If the counter exists, the inital value is ignored.
30+
var counter3 = await _bucket.IncrementAsync(key, 10, 13);
31+
Console.WriteLine("Initial value = 13, Increment = 10, Counter value: " + counter3.Value);
32+
33+
// Decrement the counter by 20.
34+
var counter4 = await _bucket.DecrementAsync(key, 20, 13);
35+
Console.WriteLine("Initial value = 13, Decrement = 20, Counter value: " + counter4.Value);
36+
}
37+
38+
static void Main(string[] args)
39+
{
40+
new Counter().ExecuteAsync().Wait();
41+
}
42+
}
43+
}

dotnet/DevGuide.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<PropertyGroup>
36-
<StartupObject>DevGuide.Program</StartupObject>
36+
<StartupObject>DevGuide.Expiration</StartupObject>
3737
</PropertyGroup>
3838
<ItemGroup>
3939
<Reference Include="Common.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
@@ -64,11 +64,13 @@
6464
<ItemGroup>
6565
<Compile Include="ConnectionConfig.cs" />
6666
<Compile Include="ConnectionBase.cs" />
67+
<Compile Include="Expiration.cs" />
6768
<Compile Include="Data.cs" />
6869
<Compile Include="Program.cs" />
6970
<Compile Include="Properties\AssemblyInfo.cs" />
7071
<Compile Include="BulkInsert.cs" />
7172
<Compile Include="BulkGet.cs" />
73+
<Compile Include="Counter.cs" />
7274
<Compile Include="Update.cs" />
7375
<Compile Include="Retrieve.cs" />
7476
</ItemGroup>

dotnet/Expiration.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)