How to set expiry of Document in minutes

Hi,

I need to set the Expiry as configurable; which can be either in minutes / days.
But while creating a document, if I give as below, its taking it Expiry as 10 days.

            TimeSpan ts = new TimeSpan(0, 10, 0);
       
            var contentDoc = new Document<string>
            {
                Id = docId,
                Content = content,
                Expiry = Convert.ToUInt32(ts.Minutes)
            };

So how can we set minutes as Expiry time for document.

Thanks
Ramya

Iā€™m not .NET developer, so I cannot tell you about TimeSpan + Convert.ToUInt32 combination.

The rules of the interpretation of this UInt32 value by the server described in the documentation:
https://developer.couchbase.com/documentation/server/current/sdk/core-operations.html#devguide_kvcore_generic__expiry

Basically you can rewrite your sample as this to get 10 minutes:

var contentDoc = new Document<string>
{
    Id = docId,
    Content = content,
    Expiry = 10*60
};

@ramyakrishna.nandula

TimeSpan has static methods to assist with building values, for example:

var expiry = TimeSpan.FromMinutes(10);
var expiry = TimeSpan.FromHours(1);
var expiry = TimeSpan.FromSeconds(100);

The reason why your code is not working as expected is when assigning the value from the timespan to the expiry value, you need to use the TotalSeconds property as unit. We have provided an extension method to assist with getting that value, example below:

var expiry = TimeSpan.FromMinutes(10);
var contentDoc = new Document<string>
{
    Id = docId,
    Content = content,
    Expiry = expiry.ToTtl();
};

Hope that helps and sorry for the slow reply :slight_smile:

1 Like

Expiry property does not work correct in the couchbase-net-client if I set seconds value there.
It works correct if I set milliseconds. But it`s not possible to use any timestamp in that way. When I convert any timestamp to milliseconds (multiply by 1000) there is uint overfrlow and value is changed to unexpected value.

So only one option is available for now is to set short TTL. E.g. value for 1 day TTL will be 86400000 milliseconds. Developers are better to change type of Expiry to ulong.

As a workaround I used method override with TimaSpan.
_bucket.UpsertAsync(GetDocId(doc), doc, TimeSpan.FromDays(90));