Shilpi
Shilpi

Reputation: 39

How to set TTL for Couchbase document dynamically for every save operation using springframework CrudRepository?

I want to set different TTL value on every save operation of particular document. Like I have one document containing employee and designation fields.

For every new employee, based on designation value TTL should be set accordingly. Like if employee is Solution Architect then document expiry should be set for 30 Days. If designation is "Senior Architect" then expiry should be set as 20 Days.

I know how to set expiry through spring Framework, but how to set it dynamically? PFB:

@Document(expiry = Constants.COUCHBASE_RESOURCE_TTL)
public class EmployeeResource { 
}

Upvotes: 3

Views: 1507

Answers (2)

juanlumn
juanlumn

Reputation: 7135

You can touch the document just after saving it and set the TTL defined by a variable.

Let's say that you have a method called getTTLByEmployee which will return the TTL (int) by Employee type. You can do something like:

Employee savedEmployee = yourRepository.save(employee);
yourRepository.getCouchbaseOperations().getCouchbaseBucket()
          .touch(savedEmployee.getId(), getTTLByEmployee(employee.getEmployeeType()));

Upvotes: 2

PKV
PKV

Reputation: 484

You can use http://docs.couchbase.com/sdk-api/couchbase-java-client-2.2.4/com/couchbase/client/java/Bucket.html#touch-java.lang.String-int- method to extend/renew the TTL for the document at runtime, in addition to the default setting.

Upvotes: 0

Related Questions