Record Class CacheEntry

java.lang.Object
java.lang.Record
com.tnsai.llm.cache.CacheEntry

public record CacheEntry(String id, String promptHash, float[] embedding, String originalPrompt, Optional<String> systemPrompt, String response, Instant createdAt, Instant lastAccessedAt, long hitCount, Map<String,Object> metadata) extends Record
Represents a cached LLM response entry.

CacheEntry stores all the necessary information to replay a cached response, including the original request parameters and the response.

  • Constructor Details

    • CacheEntry

      public CacheEntry(String id, String promptHash, float[] embedding, String originalPrompt, Optional<String> systemPrompt, String response, Instant createdAt, Instant lastAccessedAt, long hitCount, Map<String,Object> metadata)
      Creates an instance of a CacheEntry record class.
      Parameters:
      id - the value for the id record component
      promptHash - the value for the promptHash record component
      embedding - the value for the embedding record component
      originalPrompt - the value for the originalPrompt record component
      systemPrompt - the value for the systemPrompt record component
      response - the value for the response record component
      createdAt - the value for the createdAt record component
      lastAccessedAt - the value for the lastAccessedAt record component
      hitCount - the value for the hitCount record component
      metadata - the value for the metadata record component
  • Method Details

    • create

      public static CacheEntry create(String id, String promptHash, float[] embedding, String originalPrompt, Optional<String> systemPrompt, String response)
      Creates a new cache entry with current timestamp.
    • withAccess

      public CacheEntry withAccess()
      Creates a copy with updated access time and hit count.
    • isExpired

      public boolean isExpired(long ttlSeconds)
      Checks if this entry has expired.
      Parameters:
      ttlSeconds - Time-to-live in seconds
      Returns:
      true if entry has expired
    • getAgeSeconds

      public long getAgeSeconds()
      Returns the age of this entry in seconds.
    • withSpec

      public CacheEntry withSpec(String key, Object value)
      Creates a copy with additional metadata.
      Parameters:
      key - The metadata key
      value - The metadata value
      Returns:
      New CacheEntry with updated metadata
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • id

      public String id()
      Returns the value of the id record component.
      Returns:
      the value of the id record component
    • promptHash

      public String promptHash()
      Returns the value of the promptHash record component.
      Returns:
      the value of the promptHash record component
    • embedding

      public float[] embedding()
      Returns the value of the embedding record component.
      Returns:
      the value of the embedding record component
    • originalPrompt

      public String originalPrompt()
      Returns the value of the originalPrompt record component.
      Returns:
      the value of the originalPrompt record component
    • systemPrompt

      public Optional<String> systemPrompt()
      Returns the value of the systemPrompt record component.
      Returns:
      the value of the systemPrompt record component
    • response

      public String response()
      Returns the value of the response record component.
      Returns:
      the value of the response record component
    • createdAt

      public Instant createdAt()
      Returns the value of the createdAt record component.
      Returns:
      the value of the createdAt record component
    • lastAccessedAt

      public Instant lastAccessedAt()
      Returns the value of the lastAccessedAt record component.
      Returns:
      the value of the lastAccessedAt record component
    • hitCount

      public long hitCount()
      Returns the value of the hitCount record component.
      Returns:
      the value of the hitCount record component
    • metadata

      public Map<String,Object> metadata()
      Returns the value of the metadata record component.
      Returns:
      the value of the metadata record component