TiKV provides the Time To Live (TTL) support via the RawKV API. This document provides two examples to show you how to set TTL via the RawKV API.

Enable TTL

Before you set TTL via RawKV API, you must enable TTL in your TiKV cluster. TTL is disabled by default. To enable it, set the following TiKV configuration to true.

  1. [storage]
  2. enable-ttl = true

Use TTL in Java client

After TTL is enabled in TiKV, you can set it in Java client via the put API or CAS API. The following two examples show how to set TTL via the put API and CAS API.

Set TTL in the put API

In the following examples, these operations are performed:

  1. Two key-value pairs, (k1, v1) and (k2, v2), are written into TiKV via the put API. (k1, v1) is written with a TTL of 10 seconds. (k2, v2) is written without TTL.
  2. Try to read k1 and k2 from TiKV. Both values are returned.
  3. Let TiKV sleep for 10 seconds, which is the time of TTL.
  4. Try to read k1 and k2 from TiKV. v2 is returned, but v1 is not returned because the TTL has expired.
  1. import java.util.Optional;
  2. import org.tikv.common.TiConfiguration;
  3. import org.tikv.common.TiSession;
  4. import org.tikv.raw.RawKVClient;
  5. import org.tikv.shade.com.google.protobuf.ByteString;
  6. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  7. TiSession session = TiSession.create(conf);
  8. RawKVClient client = session.createRawClient();
  9. // Writes the (k1, v1) into TiKV with a TTL of 10 seconds.
  10. client.put(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("v1"), 10);
  11. // Writes the (k2, v2) into TiKV without TTL.
  12. client.put(ByteString.copyFromUtf8("k2"), ByteString.copyFromUtf8("v2"));
  13. // Reads k1 from TiKV. v1 is returned.
  14. Optional<ByteString> result1 = client.get(ByteString.copyFromUtf8("k1"));
  15. assert(result1.isPresent());
  16. assert("v1".equals(result1.get().toStringUtf8()));
  17. System.out.println(result1.get().toStringUtf8());
  18. // Reads k2 from TiKV. v2 is returned.
  19. Optional<ByteString> result2 = client.get(ByteString.copyFromUtf8("k2"));
  20. assert(result2.isPresent());
  21. assert("v2".equals(result2.get().toStringUtf8()));
  22. System.out.println(result2.get().toStringUtf8());
  23. // Let TiKV sleep for 10 seconds.
  24. System.out.println("Sleep 10 seconds.");
  25. Thread.sleep(10000);
  26. // Reads k1 from TiKV. NULL is returned, because k1's TTL has expired.
  27. result1 = client.get(ByteString.copyFromUtf8("k1"));
  28. assert(!result1.isPresent());
  29. // Reads k2 from TiKV. v2 is returned.
  30. result2 = client.get(ByteString.copyFromUtf8("k2"));
  31. assert(result2.isPresent());
  32. assert("v2".equals(result2.get().toStringUtf8()));
  33. System.out.println(result2.get().toStringUtf8());
  34. // Close
  35. client.close();
  36. session.close();

Set TTL in the CAS API

You can also set TTL via the CAS API. See the following example:

  1. import java.util.Optional;
  2. import org.tikv.common.TiConfiguration;
  3. import org.tikv.common.TiSession;
  4. import org.tikv.raw.RawKVClient;
  5. import org.tikv.shade.com.google.protobuf.ByteString;
  6. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  7. // Enables AtomicForCAS when using RawKVClient.compareAndSet or RawKVClient.putIfAbsent
  8. conf.setEnableAtomicForCAS(true);
  9. TiSession session = TiSession.create(conf);
  10. RawKVClient client = session.createRawClient();
  11. ByteString key = ByteString.copyFromUtf8("Hello");
  12. ByteString value = ByteString.copyFromUtf8("CAS+TTL");
  13. ByteString newValue = ByteString.copyFromUtf8("NewValue");
  14. // Writes data.
  15. client.put(key, value);
  16. // CAS with TTL = 10 seconds
  17. client.compareAndSet(key, Optional.of(value), newValue, 10);
  18. // Reads data.
  19. Optional<ByteString> result = client.get(key);
  20. assert(result.isPresent());
  21. assert("NewValue".equals(result.get().toStringUtf8()));
  22. System.out.println(result.get().toStringUtf8());
  23. // Let TiKV sleep for 10 seconds.
  24. System.out.println("Sleep 10 seconds.");
  25. Thread.sleep(10000);
  26. // Reads data.
  27. result = client.get(key);
  28. assert(!result.isPresent());
  29. // Close
  30. client.close();
  31. session.close();

The example code above is available here.