Caching Examples¶
Morphium provides entity‑level caching via @Cache and supports cluster synchronization and JCache.
1) Basic entity caching
import de.caluga.morphium.annotations.caching.Cache;
@Entity
@Cache(
timeout = 30_000, // 30s TTL
clearOnWrite = true, // clear type cache on writes
strategy = Cache.ClearStrategy.LRU,
readCache = true,
syncCache = Cache.SyncCacheStrategy.CLEAR_TYPE_CACHE
)
public class Product { ... }
2) Per‑type TTL override
// Set a longer TTL for Product results at runtime
morphium.getCache().setValidCacheTime(Product.class, 120_000);
3) Cross‑node cache synchronization — messaging‑driven
// Initialize messaging via factory and start it
MorphiumMessaging messaging = morphium.createMessaging();
messaging.start();
// Attach synchronizer: clears caches on other nodes when writes occur *through Morphium*
MessagingCacheSynchronizer sync = new MessagingCacheSynchronizer(messaging, morphium);
// Optional: send a manual clear‑all
sync.sendClearAllMessage("maintenance");
3b) Cross‑node cache synchronization — watching (change‑stream‑driven)
// Watches the cached collections directly via a MongoDB Change Stream — no
// messaging setup needed, and it also catches writes made by other, non‑Morphium
// processes. Requires a replica set (Change Streams need an oplog).
WatchingCacheSynchronizer sync = new WatchingCacheSynchronizer(morphium);
4) Switch to JCache implementation
// Use javax.cache‑based cache impl
MorphiumCacheJCacheImpl jcache = new MorphiumCacheJCacheImpl();
cfg.cacheSettings().setCache(jcache);
// Optional: plug in a custom CacheManager
// jcache.setCacheManager(myCacheManager);
5) Manual cache operations
// Clear one type
morphium.clearCachefor(Product.class);
// Reset all caches
morphium.getCache().resetCache();
// Inspect an object by ID from cache (if present)
Product p = morphium.getCache().getFromIDCache(Product.class, productId);
6) Cache listeners (observe additions/removals)
morphium.getCache().addCacheListener(new CacheListener() {
@Override public CacheEntry wouldAddToCache(String key, CacheEntry ce, boolean update) { return ce; }
@Override public boolean wouldClearCache(Class<?> type) { return true; }
@Override public boolean wouldRemoveEntryFromCache(String key, CacheEntry ce, boolean expired) { return true; }
});
Notes
- @Cache.syncCache controls how remote nodes update their caches:
- CLEAR_TYPE_CACHE: clear entire type cache on write
- REMOVE_ENTRY_FROM_TYPE_CACHE: remove single entry by ID
- UPDATE_ENTRY: re‑read updated entries
- Two synchronizer implementations exist: MessagingCacheSynchronizer (needs messaging running on all nodes, catches only writes made through Morphium) and WatchingCacheSynchronizer (needs a replica set, catches any write to the watched collection). Pick one per deployment — see Cache Patterns.
See also: Cache Patterns, Developer Guide