Class Lease

java.lang.Object
com.loomcache.server.lease.Lease

public class Lease extends Object
Represents an etcd-style time-bound TTL lease.

A lease has a TTL (time-to-live) that can be renewed. Keys can be attached to a lease, and when the lease expires, all attached keys are automatically revoked. This model is inspired by etcd's lease system.

Thread safety: Uses ConcurrentHashMap for attached keys tracking. Expiry checks use monotonic System.nanoTime() deadlines; wall-clock Instant values are retained only for observability. The lease is immutable after construction except for renewal and key attachment.

Since:
1.5
  • Constructor Details

    • Lease

      public Lease(long id, long ttlSeconds)
      Creates a new lease with the given ID and TTL.
      Parameters:
      id - unique lease ID (typically auto-generated, must be positive)
      ttlSeconds - time-to-live in seconds (must be positive)
      Throws:
      IllegalArgumentException - if id invalid input: '<'= 0 or ttlSeconds invalid input: '<'= 0
  • Method Details

    • getAttachedKeys

      public Set<String> getAttachedKeys()
      Gets an unmodifiable view of attached keys.
      Returns:
      an unmodifiable set of keys attached to this lease
    • isExpired

      public boolean isExpired()
      Checks if the lease has expired based on the current time.

      A lease is expired if the time elapsed since last renewal exceeds the TTL.

      Returns:
      true if the lease has expired, false otherwise
    • remainingTtl

      public long remainingTtl()
      Calculates the remaining time-to-live in seconds.

      Returns the number of seconds until the lease expires. If expired, returns 0 (never negative).

      Returns:
      remaining TTL in seconds, or 0 if expired
    • attachKey

      public void attachKey(String key)
      Attaches a key to this lease.

      A key can be attached to multiple leases, though typically each key is owned by one lease at a time.

      Parameters:
      key - the key to attach
      Throws:
      NullPointerException - if key is null
    • detachKey

      public void detachKey(@Nullable String key)
      Detaches a key from this lease.

      If the key is not attached, this is a no-op.

      Parameters:
      key - the key to detach (may be null)
    • renew

      public void renew()
      Renews the lease by resetting the lastRenewedAt timestamp.

      This extends the lease's lifetime by resetting the TTL countdown. The original TTL duration is used; the ttlSeconds field is immutable.