Apache Ignite:SQLQuery与TouchedExpiryPolicy协同异常咨询
Troubleshooting TouchedExpiryPolicy + SQLQuery Cache Expiry Quirk
I’ve run into this exact issue before with certain cache implementations—turns out it’s not exactly a "compatibility bug" but rather a nuance in how sqlQuery interacts with the expiry policy’s access tracking. Let me break this down:
Why this is happening
The core problem lies in how sqlQuery handles cache entry access compared to other methods:
- When you use
scanQueryorcache.getAll, the cache directly accesses each individual entry in the store. Every access triggers theTouchedExpiryPolicy’s logic to reset the expiry timer for that entry, which is exactly what you’d expect. sqlQuery, however, often uses an optimized path—like querying an underlying index or a snapshot of cached data—instead of hitting each entry directly. This means the cache never registers those query results as "active accesses" to the individual entries. As a result, their expiry timers keep counting down from the original creation time, so they expire exactly 10 seconds after creation no matter how many times you run the query.
Fixes you can try right now
- Manually touch entries after running sqlQuery
The simplest workaround is to loop through your query results and explicitly call the cache’stouchmethod for each entry’s key. This manually resets the expiry timer just like a direct access would:// Example code (adjust based on your cache API) List<YourCachedObject> results = cache.sqlQuery("SELECT * FROM YourTable WHERE ..."); for (YourCachedObject item : results) { cache.touch(item.getCacheKey()); } - Check your cache provider’s query settings
Some cache tools (like Hazelcast, Ignite, etc.) have configuration flags that force query operations to update entry access timestamps. Look for settings named something liketrack-query-accessorexpiry-on-queryin your provider’s docs—enabling this might fix the issue without manual code changes. - Switch to an access-aware expiry policy (if supported)
If your cache provider offers it, consider using anAccessedExpiryPolicyinstead (or a variant explicitly designed to account for query-based access). Some policies are built to recognize when entries are retrieved via queries and reset their timers automatically.
How to verify the fix
Enable debug logging for your cache’s expiry and access events. You’ll notice that before the fix, sqlQuery doesn’t log any "entry accessed" events, while scanQuery does. After applying your fix, those events should appear for sqlQuery results, confirming the expiry timers are being reset correctly.
内容的提问来源于stack exchange,提问作者Mitn
相关产品推荐
相关产品推荐

