@sumaiazaman merged PR #59850 into laravel/framework for the 13.x branch, implementing the CanFlushLocks interface on NullStore and MemoizedStore.
After #59738 added CanFlushLocks to FailoverStore, every major cache lock provider implemented both LockProvider and CanFlushLocks except two. NullStore and MemoizedStore both implemented LockProvider but were missing CanFlushLocks, which meant Cache::supportsFlushingLocks() returned false for those stores even though the other five (Redis, File, Array, Database, Failover) all returned true.
| Store | Before | After |
|---|---|---|
NullStore |
LockProvider only |
LockProvider, CanFlushLocks |
MemoizedStore |
LockProvider only |
LockProvider, CanFlushLocks |
NullStore uses NoLock internally, which never persists anything. There is nothing to flush, so flushLocks() simply returns true and hasSeparateLockStore() returns false.
1// NullStore now satisfies CanFlushLocks2$store = Cache::store('null');3 4$store->flushLocks(); // returns true, no-op5$store->hasSeparateLockStore(); // returns false
MemoizedStore wraps another store, so its CanFlushLocks implementation passes the call through. If the underlying store implements CanFlushLocks, flushLocks() delegates to it. If it does not, a BadMethodCallException is thrown, consistent with how lock() and restoreLock() already handle unsupported underlying stores.
1// Underlying store supports flushing locks2$store = Cache::store('memoized'); // backed by Redis, Array, etc.3 4$store->flushLocks(); // delegates to the underlying store5$store->hasSeparateLockStore(); // delegates to the underlying store6 7// Underlying store does not support CanFlushLocks8$store->flushLocks(); // throws BadMethodCallException
@sumaiazaman added five new tests covering both stores:
CacheNullStoreTest::testLocksCanBeFlushedCacheNullStoreTest::testHasSeparateLockStoreCacheMemoizedStoreTest::testLocksCanBeFlushedWhenUnderlyingStoreSupportsItCacheMemoizedStoreTest::testFlushLocksThrowsWhenUnderlyingStoreDoesNotSupportItCacheMemoizedStoreTest::testHasSeparateLockStoreDelegatestoUnderlyingStoreAnyone writing code that calls Cache::supportsFlushingLocks() and branching on the result can now rely on consistent behavior across all built-in cache stores, including null and memoized configurations.
If you enjoyed this article, please consider supporting our work for as low as $5 / month.
Sponsor
Written by
Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.
Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.