1. POM file dependency
<dependency> <groupId></groupId> <artifactId>caffeine</artifactId> <version>3.1.0</version> </dependency>
2. Declare cache
@Slf4j public class CaffeineCacheUtils { /** * Declare cached objects */ private static final Cache<String, Object> CACHE = () // Evict cache when neither key nor value is referenced .weakKeys() .weakValues() // Maximum capacity .maximumSize(10_000) // Fixed survival time 24 hours .expireAfterWrite(2 * 60 * 60 * 1000L, ) .build() // Build Caffeine cache ; }
3. Cache usage
/** * Add cache * * @param key key value * @param value value */ public static void put(String key, Object value) { (key, value); } /** * Get cached objects * * @param key key value * @return Return value */ public static Object get(String key) { return (key); }
4. Test cache
@Test public void testCache() { ("key", "value"); Object value = ("key"); ("value={}", value); }
5. Custom cache expiration time
expireAfterWrite specifies a fixed expiration time. If you want to specify the expiration time by yourself according to different key values, you can implement it in the following method.
Declare a cache value holder object
@Data public class CacheValueHolder implements Serializable { @Serial private static final long serialVersionUID = 1398783661313031605L; private Object value; private TimeUnit timeUnit; private long expire; }
Create a cache container
/** * Declare cached objects */ private static final Cache<String, CacheValueHolder> CACHE = () // Evict cache when neither key nor value is referenced .weakKeys() .weakValues() // Maximum capacity .maximumSize(10_000) .expireAfter(new Expiry<String, CacheValueHolder>() { @Override public long expireAfterCreate(String key, CacheValueHolder valueHolder, long currentTime) { ("Create a cache object,key={},value={},currentTime={}", key, (valueHolder), currentTime); return ().toNanos(()); } @Override public long expireAfterUpdate(String key, CacheValueHolder valueHolder, long currentTime, @NonNegative long currentDuration) { ("Update cache objects,key={},value={},currentTime={},currentDuration={}", key, (valueHolder), currentTime, currentDuration); return ().toNanos(()); } @Override public long expireAfterRead(String key, CacheValueHolder valueHolder, long currentTime, @NonNegative long currentDuration) { ("Read cached objects,key={},value={},currentTime={},currentDuration={}", key, (valueHolder), currentTime, currentDuration); return currentDuration; } }) // If the cache does not exist, synchronously query the product list of the business unit and add the cache at the same time .build() // Build Caffeine cache ;
Modify cache usage method
/** * Add cache * * @param key key value * @param value value * @param expire expiration time * @param timeUnit Time Type */ public static void put(String key, Object value, long expire, TimeUnit timeUnit) { CacheValueHolder valueHolder = new CacheValueHolder(); (value); (expire); (timeUnit); (key, valueHolder); } /** * Add cache * * @param key key value * @param value value */ public static void put(String key, Object value) { // The default cache existence time is 1000 milliseconds put(key, value, 1000, ); } /** * Get cached objects * * @param key key value * @return Return value */ public static Object get(String key) { CacheValueHolder valueHolder = (key); if ((valueHolder)) { return null; } return (); }
6. Test custom timeout time
@Test public void testCache() throws InterruptedException { ("key1", "value1", 1000L, ); ("key2", "value2", 2000L, ); Object value1 = ("key1"); Object value2 = ("key2"); ("value1={}", value1); ("value2={}", value2); (1000L); Object value11 = ("key1"); Object value22 = ("key2"); ("value1={}", value11); ("value2={}", value22); }
After the method is run, the log is:
23:44:21.398 [main] INFO - Create cache object, key=key1, value={"expire":1000,"timeUnit":"MILLISECONDS","value":"value1"}, currentTime=8165196876700
23:44:21.401 [main] INFO - Create cache object, key=key2, value={"expire":2000,"timeUnit":"MILLISECONDS","value":"value2"}, currentTime=8165240503500
23:44:21.401 [main] INFO - Read cache object, key=key1, value={"expire":1000,"timeUnit":"MILLISECONDS","value":"value1"}, currentTime=8165240686100, currentDuration=956190600
23:44:21.403 [main] INFO - Read cache object, key=key2, value={"expire":2000,"timeUnit":"MILLISECONDS","value":"value2"}, currentTime=8165242670700, currentDuration=1997832800
23:44:21.403 [main] INFO - value1=value1
23:44:21.403 [main] INFO - value2=value2
23:44:22.410 [main] INFO - Read cache object, key=key2, value={"expire":2000,"timeUnit":"MILLISECONDS","value":"value2"}, currentTime=8166249659500, currentDuration=990844000
23:44:22.411 [main] INFO - value1=null
23:44:22.412 [main] INFO - value2=value2
You can see two caches placed in the cache container at the same time, one is 1000 milliseconds expiration time and one is 2000 milliseconds expiration time. They can be obtained when they are just put in. After waiting for 1000 milliseconds, only the second cache can be obtained. The first cache object has expired and cannot be obtained, which realizes the user-defined cache object expiration time.
This is the end of this article about the implementation of CaffeineCache custom cache time in Java. For more related Java CaffeineCache content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!