see on GitHub

Compare Exchange Expiration


@expires is a metadata property that holds a DateTime value. Once the specified date and time has passed, the compare exchange value is set for deletion by the expiration feature. The exact time this happens depends on the expiration frequency and other expiration configurations.

To set a compare exchange value to expire, simply put a DateTime value (in UTC format) in the @expires field, then to send it to the server.


Creating a new key with CreateCompareExchangeValue():

using (IAsyncDocumentSession session = store.OpenAsyncSession())
    // Set a time exactly one week from now
    DateTime expirationTime = DateTime.UtcNow.AddDays(7);

    // Create a new compare exchange value
    var cmpxchgValue = session.Advanced.ClusterTransaction.CreateCompareExchangeValue("key", "value");

    // Edit Metadata
    cmpxchgValue.Metadata[Constants.Documents.Metadata.Expires] = expirationTime;

    // Send to server

Updating an existing key with PutCompareExchangeValueOperation<T>:

// Retrieve an existing key
CompareExchangeValue<string> cmpxchgValue = store.Operations.Send(
        new GetCompareExchangeValueOperation<string>("key"));
// Set time
DateTime expirationTime = DateTime.UtcNow.AddDays(7);

// Edit Metadata
cmpxchgValue.Metadata[Constants.Documents.Metadata.Expires] = expirationTime;
// Update value. Index must match the index on the server side,
// or the operation will fail.
CompareExchangeResult<string> result = store.Operations.Send(
        new PutCompareExchangeValueOperation<string>(