On 3.0.3 lib I noticed that GetResult.Expiry is always null, no matter how long the object lives.
Hi Artem,
The SDK needs a hint that you’re interested in the expiry, otherwise it won’t fetch it from the server. I’m not fluent in C#, but I think this is how you do it:
collection.Get("document-key", options => {
options.Expiry();
});
Thanks,
David
3 Likes
@david.nault is correct, and though he claims that he’s “not fluent in C#”, he taught me something new today about the Couchbase C# SDK
Here’s a full example:
using System;
using System.Threading.Tasks;
using Couchbase;
using Couchbase.KeyValue;
namespace ForumQuestions
{
class Program
{
static async Task Main(string[] args)
{
var cluster = await Cluster.ConnectAsync("couchbase://localhost", "Administrator", "password");
var bucket = await cluster.BucketAsync("tests");
var coll = bucket.DefaultCollection();
var key = Guid.NewGuid().ToString();
await coll.InsertAsync(key, new {foo = "bar"}, new InsertOptions().Expiry(new TimeSpan(0, 1, 0)));
var docWithoutExpiry = await coll.GetAsync(key);
Console.WriteLine($"DocWithoutExpiry: {docWithoutExpiry.Expiry}");
var docWithExpiry = await coll.GetAsync(key, options => options.Expiry());
Console.WriteLine($"DocWithExpiry: {docWithExpiry.Expiry}");
cluster.Dispose();
}
}
}
And here’s a screenshot of the output:
2 Likes