TTL Index
Using Time-To-Live Index to automatically deleting entities.
Last updated
Using Time-To-Live Index to automatically deleting entities.
Last updated
Imagine you have an entity, which should be deleted after a specific time. Instead of creating a repeated task in java through a Timer
or an ScheduledExecutorService
and using the resources of the application, en2do offers to use MongoDBs time-to-live indexes.
First of all you need to create at least one field of the type java.util.Date
in your entity.
After that you can decide between two TTLIndex
options:
Delete at timeStamp = {ttl} {unit} + {timeStamp of field}
Delete at timeStamp = {timeStamp of field}
Example of both time-to-live indexes:
// Other imports go here...
import java.util.Date;
// Lombok's annotations go here..
// en2do - Expires 10 seconds after timeStamp of "createDate"
@TTLIndex(value = "createTime", ttl = 10, unit =