Package io.github.dhruv1110.jcachex
Class CacheEntry<V>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.CacheEntry<V>
-
- Type Parameters:
V
- the type of the cached value
public class CacheEntry<V> extends Object
Represents a cache entry containing a value along with metadata for cache management.This class encapsulates both the cached value and important metadata such as creation time, last access time, access count, weight, and expiration information. This metadata is used by eviction strategies, expiration policies, and statistics collection.
Key Features:
- Value Storage: Holds the actual cached value
- Access Tracking: Maintains access count and last access time for LRU/LFU eviction
- Weight Support: Stores entry weight for weight-based eviction strategies
- Expiration: Tracks creation time and expiration time for TTL policies
- Thread Safety: Access count updates are atomic and thread-safe
Usage in Cache Operations:
// Cache entries are created internally when putting values cache.put("user123", user); // Creates CacheEntry<User> internally // Eviction strategies use entry metadata // LRU strategy checks lastAccessTime // LFU strategy checks accessCount // Weight-based strategy checks weight // Expiration policies use creation and expiration times if (entry.isExpired()) { cache.remove(key); // Entry is automatically removed }
Integration with Eviction Strategies:
// Custom eviction strategy example public class CustomEvictionStrategy<K, V> implements EvictionStrategy<K, V> { @Override public K selectEvictionCandidate(Map<K, CacheEntry<V>> entries) { return entries.entrySet().stream() .min((e1, e2) -> { CacheEntry<V> entry1 = e1.getValue(); CacheEntry<V> entry2 = e2.getValue(); // Evict entry with lowest access count return Long.compare(entry1.getAccessCount(), entry2.getAccessCount()); }) .map(Map.Entry::getKey) .orElse(null); } // ... other methods }
- Since:
- 1.0.0
- See Also:
EvictionStrategy
,CacheConfig
-
-
Constructor Summary
Constructors Constructor Description CacheEntry(V value, long weight, Instant expirationTime)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getAccessCount()
Instant
getCreationTime()
Instant
getExpirationTime()
Instant
getLastAccessTime()
V
getValue()
long
getWeight()
void
incrementAccessCount()
boolean
isExpired()
-
-
-
Method Detail
-
getValue
public V getValue()
-
getWeight
public long getWeight()
-
isExpired
public boolean isExpired()
-
getExpirationTime
public Instant getExpirationTime()
-
getAccessCount
public long getAccessCount()
-
incrementAccessCount
public void incrementAccessCount()
-
getLastAccessTime
public Instant getLastAccessTime()
-
getCreationTime
public Instant getCreationTime()
-
-