|
| 1 | +# Migrating from WoltLab Suite 6.1 - Tolerant and Eager Caching |
| 2 | + |
| 3 | +In the upcoming version of WoltLab Suite, the caching system has been reworked to provide a more flexible and efficient way to cache data. |
| 4 | + |
| 5 | +To solve the problems: |
| 6 | + |
| 7 | +- Resetting a cache causes the next request that needs this cache to trigger a synchronous rebuild. |
| 8 | +- Non-critical caches can sometimes be expensive to generate, causing dips in response times. |
| 9 | +- The same rebuild can take place simultaneously by concurrent requests. |
| 10 | + |
| 11 | +## General Guidelines |
| 12 | + |
| 13 | +- MUST NOT rely on any (runtime) cache |
| 14 | +- Return a `CacheData` object instead of an `array` |
| 15 | +- Optional, parameterized caches with state |
| 16 | + - Use `readonly` properties in the constructor |
| 17 | + ```PHP |
| 18 | + final class FooCache extends AbstractTolerantCache { |
| 19 | + public function __construct( |
| 20 | + public readonly int $categoryID |
| 21 | + ) {} |
| 22 | + // Additional methods … |
| 23 | + } |
| 24 | + ``` |
| 25 | + |
| 26 | +## Eager Caches |
| 27 | + |
| 28 | +The eager cache has no lifetime and must be updated manually by the developer if the data changes. |
| 29 | +`(new FooEagerCache())->rebuild()` |
| 30 | + |
| 31 | +#### Example |
| 32 | + |
| 33 | +```PHP |
| 34 | +/** |
| 35 | +* @extends AbstractEagerCache<\stdClass> |
| 36 | +*/ |
| 37 | +final class FooEagerCache extends AbstractEagerCache |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + public readonly int $categoryID, |
| 41 | + ) {} |
| 42 | + |
| 43 | + #[\Override] |
| 44 | + protected function getCacheData(): \stdClass |
| 45 | + { |
| 46 | + // Load cache data from database |
| 47 | + return new \stdClass(); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +The parameter `$categoryID` is certainly not required. |
| 53 | +It can also be omitted. |
| 54 | + |
| 55 | +This cache can be used as follows |
| 56 | + |
| 57 | +```PHP |
| 58 | +$cache = (new FooEagerCache(1))->getCache(); |
| 59 | +// without a state |
| 60 | +$cache = (new FooEagerCache())->getCache(); |
| 61 | +``` |
| 62 | + |
| 63 | +## Tolerant Caches |
| 64 | + |
| 65 | +The tolerant cache is a special cache that can return outdated content. |
| 66 | +This must not cause any problems at runtime. |
| 67 | + |
| 68 | +The cache is updated by a background job when the lifetime expires or by a [probabilistic early expiration](https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration). |
| 69 | + |
| 70 | +#### Example |
| 71 | + |
| 72 | +```PHP |
| 73 | +/** |
| 74 | +* @extends AbstractTolerantCache<\stdClass> |
| 75 | +*/ |
| 76 | +final class FooAsyncCache extends AbstractTolerantCache |
| 77 | +{ |
| 78 | + #[\Override] |
| 79 | + public function getLifetime(): int |
| 80 | + { |
| 81 | + return 6000; |
| 82 | + } |
| 83 | + |
| 84 | + #[\Override] |
| 85 | + protected function rebuildCacheData(): \stdClass |
| 86 | + { |
| 87 | + // Load cache data from database |
| 88 | + return new \stdClass(); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +This cache can be used as follows |
| 94 | + |
| 95 | +```PHP |
| 96 | +$cache = (new FooAsyncCache())->getCache(); |
| 97 | +// with a state |
| 98 | +$cache = (new FooAsyncCache($parameterOne, $parameterTwo, …))->getCache(); |
| 99 | +``` |
0 commit comments