Skip to content

Commit ca3775f

Browse files
committed
Update changelog for release
Cr: Pete
1 parent 319b701 commit ca3775f

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 32.0-SNAPSHOT - unreleased
44

5+
## 31.0.3 - 2025-06-27
6+
* Performance improvements for Cache lookup
7+
58
## 31.0.2 - 2025-06-09
69

710
### 🐞 Bug Fixes

src/main/groovy/io/xh/hoist/cache/Cache.groovy

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Cache<K, V> implements LogSupport, AdminStats {
122122
/** @returns the cached Entry at key. */
123123
CacheEntry<V> getEntry(K key) {
124124
def ret = _map[key]
125-
if (ret && shouldExpire(ret)) {
125+
if (ret != null && shouldExpire(ret)) {
126126
remove(key)
127127
return null
128128
}
@@ -132,7 +132,7 @@ class Cache<K, V> implements LogSupport, AdminStats {
132132
/** @returns cached value for key, or lazily creates if needed. */
133133
V getOrCreate(K key, Closure<V> c) {
134134
CacheEntry<V> entry = _map[key]
135-
if (!entry || shouldExpire(entry)) {
135+
if (entry == null || shouldExpire(entry)) {
136136
def val = c(key)
137137
put(key, val)
138138
return val
@@ -166,7 +166,8 @@ class Cache<K, V> implements LogSupport, AdminStats {
166166

167167
/** @returns the timestamp of the cached Entry at key. */
168168
Long getTimestamp(K key) {
169-
return getEntryTimestamp(_map[key])
169+
def entry = _map[key]
170+
return entry != null ? getEntryTimestamp(entry) : null
170171
}
171172

172173
/**
@@ -263,20 +264,18 @@ class Cache<K, V> implements LogSupport, AdminStats {
263264
}
264265

265266
private boolean shouldExpire(CacheEntry<V> entry) {
266-
if (expireFn) return expireFn.call(entry)
267+
if (expireFn != null) return expireFn.call(entry)
267268

268-
if (expireTime) {
269+
if (expireTime != null) {
269270
Long timestamp = getEntryTimestamp(entry),
270271
expire = (expireTime instanceof Closure ? expireTime.call() : expireTime) as Long
271-
return intervalElapsed(expire, timestamp)
272+
return currentTimeMillis() > timestamp + expire
272273
}
273274
return false
274275
}
275276

276277
private Long getEntryTimestamp(CacheEntry<V> entry) {
277-
if (!entry) return null
278-
if (timestampFn) return asEpochMilli(timestampFn.call(entry.value))
279-
return entry.dateEntered
278+
return timestampFn != null ? asEpochMilli(timestampFn.call(entry.value)) : entry.dateEntered
280279
}
281280

282281
private void fireOnChange(Object key, V oldValue, V value) {

src/main/groovy/io/xh/hoist/util/DateTimeUtils.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class DateTimeUtils {
5353
*/
5454
static Long asEpochMilli(Object timestamp) {
5555
if (timestamp == null) return null
56+
if (timestamp instanceof Long) return (Long) timestamp
5657
if (timestamp instanceof Date) return ((Date) timestamp).time
5758
if (timestamp instanceof Instant) return ((Instant) timestamp).toEpochMilli()
58-
if (timestamp instanceof Long) return (Long) timestamp
5959
throw new IllegalArgumentException("Invalid timestamp: ${timestamp}")
6060
}
6161

0 commit comments

Comments
 (0)