Skip to content

Commit

Permalink
TTLCache is new. IT supports a minimum Time-To-Live (TTL) for cache…
Browse files Browse the repository at this point in the history
… entries. Entries older than TTL will be dropped. Can also be limited to `maxSize` entries to support LRU capability. Each `TTLCache` can have its own TTL setting, yet, they share a single `ScheduledExecutorService` across all instances. Call the static `shutdown()` method on `TTLCache` when your application or service is ending.

`LRUCache` updated to use a single `ScheduledExecutorService` across all instances, regardless of the individual time settings. Call the static `shutdown()` method on `LRUCache` when your application or service is ending.
  • Loading branch information
jdereg committed Sep 30, 2024
1 parent 7b57fb3 commit d81b42a
Show file tree
Hide file tree
Showing 9 changed files with 1,214 additions and 96 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ implementation 'com.cedarsoftware:java-util:2.14.0'
- **[CompactCIHashMap](/src/main/java/com/cedarsoftware/util/CompactCIHashMap.java)** - A compact, case-insensitive `Map` expanding to a `HashMap`.
- **[CaseInsensitiveMap](/src/main/java/com/cedarsoftware/util/CaseInsensitiveMap.java)** - Treats `String` keys in a case-insensitive manner.
- **[LRUCache](/src/main/java/com/cedarsoftware/util/LRUCache.java)** - Thread-safe LRU cache which implements the Map API. Supports "locking" or "threaded" strategy (selectable).
- **[TTLCache](/src/main/java/com/cedarsoftware/util/TTLCache.java)** - Thread-safe TTL cache which implements the Map API. Entries older than Time-To-Live will be evicted. Also supports a `maxSize` (LRU capability).
- **[TrackingMap](/src/main/java/com/cedarsoftware/util/TrackingMap.java)** - Tracks access patterns to its keys, aiding in performance optimizations.
- **[SealableMap](/src/main/java/com/cedarsoftware/util/SealableMap.java)** - Allows toggling between sealed (read-only) and unsealed (writable) states, managed externally.
- **[SealableNavigableMap](/src/main/java/com/cedarsoftware/util/SealableNavigableMap.java)** - Extends `SealableMap` features to `NavigableMap`, managing state externally.
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
### Revision History
#### 2.15.0-SNAPSHOT
> * `TTLCache` is new. IT supports a minimum Time-To-Live (TTL) for cache entries. Entries older than TTL will be dropped. Can also be limited to `maxSize` entries to support LRU capability. Each `TTLCache` can have its own TTL setting, yet, they share a single `ScheduledExecutorService` across all instances. Call the static `shutdown()` method on `TTLCache` when your application or service is ending.
> * `LRUCache` updated to use a single `ScheduledExecutorService` across all instances, regardless of the individual time settings. Call the static `shutdown()` method on `LRUCache` when your application or service is ending.
#### 2.14.0
> * `ClassUtilities.addPermanentClassAlias()` - add an alias that `.forName()` can use to instantiate class (e.g. "date" for `java.util.Date`)
> * `ClassUtilities.removePermanentClassAlias()` - remove an alias that `.forName()` can no longer use.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<packaging>bundle</packaging>
<version>2.14.0</version>
<version>2.15.0-SNAPSHOT</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down
18 changes: 7 additions & 11 deletions src/main/java/com/cedarsoftware/util/LRUCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;

import com.cedarsoftware.util.cache.LockingLRUCacheStrategy;
import com.cedarsoftware.util.cache.ThreadedLRUCacheStrategy;
Expand Down Expand Up @@ -82,7 +81,7 @@ public LRUCache(int capacity) {
*/
public LRUCache(int capacity, StrategyType strategyType) {
if (strategyType == StrategyType.THREADED) {
strategy = new ThreadedLRUCacheStrategy<>(capacity, 10, null);
strategy = new ThreadedLRUCacheStrategy<>(capacity, 10);
} else if (strategyType == StrategyType.LOCKING) {
strategy = new LockingLRUCacheStrategy<>(capacity);
} else {
Expand All @@ -99,13 +98,10 @@ public LRUCache(int capacity, StrategyType strategyType) {
* @param capacity int maximum number of entries in the cache.
* @param cleanupDelayMillis int number of milliseconds after a put() call when a scheduled task should run to
* trim the cache to no more than capacity. The default is 10ms.
* @param scheduler ScheduledExecutorService which can be null, in which case one will be created for you, or you
* can supply your own. If one is created for you, when shutdown() is called, it will be shutdown
* for you.
* @see com.cedarsoftware.util.cache.ThreadedLRUCacheStrategy
*/
public LRUCache(int capacity, int cleanupDelayMillis, ScheduledExecutorService scheduler) {
strategy = new ThreadedLRUCacheStrategy<>(capacity, cleanupDelayMillis, scheduler);
public LRUCache(int capacity, int cleanupDelayMillis) {
strategy = new ThreadedLRUCacheStrategy<>(capacity, cleanupDelayMillis);
}

@Override
Expand Down Expand Up @@ -183,16 +179,16 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
if (!(obj instanceof Map)) { // covers null check too
return false;
}
LRUCache<?, ?> other = (LRUCache<?, ?>) obj;
return strategy.equals(other.strategy);
Map<?, ?> other = (Map<?, ?>) obj;
return strategy.equals(other);
}

public void shutdown() {
if (strategy instanceof ThreadedLRUCacheStrategy) {
((ThreadedLRUCacheStrategy<K, V>) strategy).shutdown();
ThreadedLRUCacheStrategy.shutdown();
}
}
}
Loading

0 comments on commit d81b42a

Please sign in to comment.