### Configure Lazy Entry Loading with EntryLoader Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet shows how to configure an ExpiringMap to lazily load entries using an `EntryLoader`. When `get` is called for a non-existent key, the loader creates and inserts the value, which then expires after a set time, effectively caching loaded resources. ```java Map connections = ExpiringMap.builder() .expiration(10, TimeUnit.MINUTES) .entryLoader(address -> new Connection(address)) .build(); // Loads a new connection into the map via the EntryLoader connections.get("jodah.net"); ``` -------------------------------- ### Change Expiration Policy for an Existing Entry Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet illustrates how to dynamically change the expiration policy for an existing entry in the ExpiringMap. This allows adapting expiration behavior after an entry has been added, for example, switching from creation-based to access-based expiration. ```java map.setExpirationPolicy("connection", ExpirationPolicy.ACCESSED); ``` -------------------------------- ### Get Expected Expiration Time for an Entry Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet shows how to retrieve the expected expiration time for a specific entry in the ExpiringMap. The returned value indicates when the entry is scheduled to expire, useful for monitoring or pre-emptive actions. ```java long expiration = map.getExpectedExpiration("jodah.net"); ``` -------------------------------- ### Get Configured Expiration Duration for an Entry Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet illustrates how to retrieve the configured expiration duration for an individual entry in the ExpiringMap. This returns the original expiration time set for that specific entry, not the remaining time until expiration. ```java map.getExpiration("jodah.net"); ``` -------------------------------- ### Lazy Load Entries with Variable Expiration Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet demonstrates how to lazily load entries into an ExpiringMap where each loaded entry can have its own variable expiration time. The `expiringEntry` method allows the loader to return an `ExpiringValue`, providing fine-grained control over individual entry lifespans. ```java Map connections = ExpiringMap.builder() .expiringEntry(address -> new ExpiringValue(new Connection(address), 5, TimeUnit.MINUTES)) .build(); ``` -------------------------------- ### Create an ExpiringMap with Max Size and Expiration Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet demonstrates how to create an ExpiringMap instance with a maximum size and a default expiration time for entries. Entries expire after the specified duration or when the map exceeds its maximum size, with the oldest entry being removed. ```java Map map = ExpiringMap.builder() .maxSize(123) .expiration(30, TimeUnit.SECONDS) .build(); // Expires after 30 seconds or as soon as a 124th element is added and this is the next one to expire based on the expiration policy map.put("connection", connection); ``` -------------------------------- ### Configure ExpiringMap for Google App Engine Integration Source: https://github.com/jhalterman/expiringmap/blob/master/README.md Google App Engine users must specify a `ThreadFactory` prior to constructing an `ExpiringMap` instance to avoid runtime permission errors. This snippet demonstrates how to set the `ThreadFactory` using `com.google.appengine.api.ThreadManager.currentRequestThreadFactory()` before creating an `ExpiringMap`. ```java ExpiringMap.setThreadFactory(com.google.appengine.api.ThreadManager.currentRequestThreadFactory()); ExpiringMap.create(); ``` -------------------------------- ### Dynamically Change Expiration Time and Policy Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet demonstrates how to modify both the expiration time and policy for an existing entry in an ExpiringMap on the fly. This provides flexibility to adjust entry expiration behavior post-insertion, such as extending its lifespan or changing its expiration trigger. ```java map.setExpiration(connection, 5, TimeUnit.MINUTES); map.setExpirationPolicy(connection, ExpirationPolicy.ACCESSED); ``` -------------------------------- ### Configure Variable Expiration for Individual Entries Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet shows how to enable variable expiration for an ExpiringMap, allowing individual entries to have their own expiration times and policies. An entry is added with a specific access-based expiration of 5 minutes, independent of the map's default settings. ```java ExpiringMap map = ExpiringMap.builder() .variableExpiration() .build(); map.put("connection", connection, ExpirationPolicy.ACCESSED, 5, TimeUnit.MINUTES); ``` -------------------------------- ### Configure Global Expiration Policy for ExpiringMap Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet shows how to set a global expiration policy for an ExpiringMap, determining whether entries expire based on creation time or last access time. Here, entries expire based on their last access, meaning activity on an entry resets its expiration timer. ```java Map map = ExpiringMap.builder() .expirationPolicy(ExpirationPolicy.ACCESSED) .build(); ``` -------------------------------- ### Reset Expiration Timer for an Entry Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet demonstrates how to reset the internal expiration timer for a given entry in the ExpiringMap. This effectively extends the entry's lifespan based on its configured expiration policy, useful for 'touching' an entry to keep it alive. ```java map.resetExpiration("jodah.net"); ``` -------------------------------- ### Set Maximum Size for ExpiringMap Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet illustrates how to configure an ExpiringMap to enforce a maximum number of entries. When the map reaches this size, adding new entries will cause the oldest or least-accessed entry to expire, depending on the configured expiration policy. ```java Map map = ExpiringMap.builder() .maxSize(123) .build(); ``` -------------------------------- ### Dynamically Add and Remove Expiration Listeners Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet illustrates how to add and remove expiration listeners from an ExpiringMap at runtime. This provides flexibility to manage listener subscriptions based on application needs, allowing listeners to be registered or unregistered as required. ```java ExpirationListener connectionCloser = (key, connection) -> connection.close(); map.addExpirationListener(connectionCloser); map.removeExpirationListener(connectionCloser); ``` -------------------------------- ### Add Synchronous Expiration Listener to ExpiringMap Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet shows how to register a synchronous expiration listener that is notified when an entry expires. The listener's action, such as closing a connection, is executed on the same thread that caused the expiration, blocking map write operations until completion. ```java Map map = ExpiringMap.builder() .expirationListener((key, connection) -> connection.close()) .build(); ``` -------------------------------- ### Set Individual Entry Expiration Policy in ExpiringMap Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet demonstrates how to apply a specific expiration policy to an individual entry when it's added to the map. This allows for fine-grained control over how each entry expires, overriding the map's global policy for that specific entry. ```java map.put("connection", connection, ExpirationPolicy.CREATED); ``` -------------------------------- ### Add Asynchronous Expiration Listener to ExpiringMap Source: https://github.com/jhalterman/expiringmap/blob/master/README.md This snippet demonstrates how to configure an asynchronous expiration listener, which executes in a separate thread pool. This prevents listener operations from blocking map write operations, improving performance and responsiveness for applications with long-running listener tasks. ```java Map map = ExpiringMap.builder() .asyncExpirationListener((key, connection) -> connection.close()) .build(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.