Skip to content

Commit 96d5e48

Browse files
committed
Use the symfony cache contracts
1 parent 25057f0 commit 96d5e48

28 files changed

+245
-276
lines changed

wcfsetup/install/files/lib/acp/page/CacheListPage.class.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use wcf\acp\action\CacheClearAction;
66
use wcf\page\AbstractPage;
7+
use wcf\system\cache\adapter\DiskCacheAdapter;
8+
use wcf\system\cache\adapter\RedisCacheAdapter;
79
use wcf\system\cache\CacheHandler;
8-
use wcf\system\cache\source\DiskCacheSource;
9-
use wcf\system\cache\source\RedisCacheSource;
1010
use wcf\system\exception\SystemException;
1111
use wcf\system\Regex;
1212
use wcf\system\request\LinkHandler;
@@ -62,25 +62,23 @@ public function readData()
6262

6363
// init cache data
6464
$this->cacheData = [
65-
'source' => \get_class(CacheHandler::getInstance()->getCacheSource()),
65+
'source' => \get_class(CacheHandler::getInstance()->getCacheAdapter()),
6666
'version' => '',
6767
'size' => 0,
6868
'files' => 0,
6969
];
7070

7171
switch ($this->cacheData['source']) {
72-
case DiskCacheSource::class:
72+
case DiskCacheAdapter::class:
7373
// set version
7474
$this->cacheData['version'] = WCF_VERSION;
7575

7676
$this->readCacheFiles('data', FileUtil::unifyDirSeparator(WCF_DIR . 'cache'));
7777
break;
7878

79-
case RedisCacheSource::class:
79+
case RedisCacheAdapter::class:
8080
// set version
81-
$cacheSource = CacheHandler::getInstance()->getCacheSource();
82-
\assert($cacheSource instanceof RedisCacheSource);
83-
$this->cacheData['version'] = $cacheSource->getRedisVersion();
81+
$this->cacheData['version'] = RedisCacheAdapter::getRedisVersion();
8482
break;
8583
}
8684

wcfsetup/install/files/lib/system/WCF.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ protected function initCoreObjects(): void
748748
return;
749749
}
750750

751-
self::$coreObjectCache = (new CoreObjectCache())->getCache();
751+
self::$coreObjectCache = (new CoreObjectCache())->get();
752752
}
753753

754754
/**

wcfsetup/install/files/lib/system/background/job/TolerantCacheRebuildBackgroundJob.class.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

wcfsetup/install/files/lib/system/box/WhoWasOnlineBoxController.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function readObjects()
9797
{
9898
EventHandler::getInstance()->fireAction($this, 'readObjects');
9999

100-
$userIDs = (new WhoWasOnlineCache())->getCache();
100+
$userIDs = (new WhoWasOnlineCache())->get();
101101

102102
if (!empty($userIDs)) {
103103
$this->users = \array_filter(

wcfsetup/install/files/lib/system/cache/CacheHandler.class.php

Lines changed: 40 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,140 +2,100 @@
22

33
namespace wcf\system\cache;
44

5-
use wcf\system\cache\builder\ICacheBuilder;
6-
use wcf\system\cache\source\DiskCacheSource;
7-
use wcf\system\exception\SystemException;
5+
use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
6+
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
7+
use Symfony\Contracts\Cache\TagAwareCacheInterface;
8+
use wcf\system\cache\adapter\DiskCacheAdapter;
9+
use wcf\system\cache\adapter\ICacheAdapter;
810
use wcf\system\SingletonFactory;
911

1012
/**
1113
* Manages transparent cache access.
1214
*
13-
* @author Alexander Ebert, Marcel Werk
14-
* @copyright 2001-2019 WoltLab GmbH
15+
* @author Olaf Braun, Alexander Ebert, Marcel Werk
16+
* @copyright 2001-2025 WoltLab GmbH
1517
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
1618
*/
17-
class CacheHandler extends SingletonFactory
19+
final class CacheHandler extends SingletonFactory
1820
{
19-
/**
20-
* cache source object
21-
* @var \wcf\system\cache\source\ICacheSource
22-
*/
23-
protected $cacheSource;
21+
private TagAwareCacheInterface & TagAwareAdapterInterface $cache;
2422

2523
/**
2624
* Creates a new CacheHandler object.
2725
*/
2826
protected function init()
2927
{
30-
// init cache source object
3128
try {
32-
$className = 'wcf\system\cache\source\\' . \ucfirst(CACHE_SOURCE_TYPE) . 'CacheSource';
33-
if (\class_exists($className)) {
34-
$this->cacheSource = new $className();
35-
} else {
36-
// fallback to disk cache
37-
$this->cacheSource = new DiskCacheSource();
29+
$className = 'wcf\system\cache\adapter\\' . \ucfirst(CACHE_SOURCE_TYPE) . 'CacheAdapter';
30+
if (!\is_subclass_of($className, ICacheAdapter::class)) {
31+
$className = DiskCacheAdapter::class;
3832
}
39-
} catch (SystemException $e) {
33+
34+
$this->cache = $className::getAdapter();
35+
} catch (\Exception $e) {
4036
if (CACHE_SOURCE_TYPE != 'disk') {
4137
// fallback to disk cache
42-
$this->cacheSource = new DiskCacheSource();
38+
$this->cache = DiskCacheAdapter::getAdapter();
4339
} else {
4440
throw $e;
4541
}
4642
}
4743
}
4844

49-
/**
50-
* Flush cache for given resource.
51-
*
52-
* @param ICacheBuilder $cacheBuilder
53-
* @param array $parameters
54-
*/
55-
public function flush(ICacheBuilder $cacheBuilder, array $parameters)
56-
{
57-
$this->getCacheSource()->flush($this->getCacheName($cacheBuilder, $parameters), empty($parameters));
58-
}
59-
6045
/**
6146
* Flushes the entire cache.
6247
*/
63-
public function flushAll()
48+
public function flushAll(): void
6449
{
65-
$this->getCacheSource()->flushAll();
50+
$this->cache->clear();
6651
}
6752

6853
/**
69-
* Returns cached value for given resource, false if no cache exists.
54+
* Returns the cache item for the given key.
7055
*
71-
* @param ICacheBuilder $cacheBuilder
72-
* @param array $parameters
73-
* @return mixed
56+
* @template T of array|object
57+
*
58+
* @param ICacheCallback<T> $callback
59+
*
60+
* @return T
7461
*/
75-
public function get(ICacheBuilder $cacheBuilder, array $parameters)
62+
public function get(string $key, ICacheCallback $callback, bool $forceRebuild = false): array|object
7663
{
77-
return $this->getCacheSource()->get(
78-
$this->getCacheName($cacheBuilder, $parameters),
79-
$cacheBuilder->getMaxLifetime()
80-
);
64+
return $this->cache->get($key, $callback, $forceRebuild ? \INF : null);
8165
}
8266

8367
/**
84-
* Caches a value for given resource,
85-
*
86-
* @param ICacheBuilder $cacheBuilder
87-
* @param array $parameters
88-
* @param array $data
68+
* Deletes the cache item for the given key.
8969
*/
90-
public function set(ICacheBuilder $cacheBuilder, array $parameters, array $data)
70+
public function delete(string $key): bool
9171
{
92-
$this->getCacheSource()->set(
93-
$this->getCacheName($cacheBuilder, $parameters),
94-
$data,
95-
$cacheBuilder->getMaxLifetime()
96-
);
72+
return $this->cache->delete($key);
9773
}
9874

9975
/**
100-
* Returns cache index hash.
76+
* Invalidates the cache items with the associated tags.
10177
*
102-
* @param array $parameters
103-
* @return string
78+
* @param list<string> $tags
10479
*/
105-
public function getCacheIndex(array $parameters)
80+
public function invalidateTags(array $tags): bool
10681
{
107-
return \sha1(\serialize($this->orderParameters($parameters)));
82+
return $this->cache->invalidateTags($tags);
10883
}
10984

11085
/**
111-
* Builds cache name.
112-
*
113-
* @param ICacheBuilder $cacheBuilder
114-
* @param array $parameters
115-
* @return string
86+
* Returns cache index hash.
11687
*/
117-
protected function getCacheName(ICacheBuilder $cacheBuilder, array $parameters = [])
88+
public function getCacheIndex(array $parameters): string
11889
{
119-
$cacheName = \str_replace(
120-
['\\', 'system_cache_builder_'],
121-
['_', ''],
122-
\get_class($cacheBuilder)
123-
);
124-
if (!empty($parameters)) {
125-
$cacheName .= '-' . $this->getCacheIndex($parameters);
126-
}
127-
128-
return $cacheName;
90+
return \sha1(\serialize($this->orderParameters($parameters)));
12991
}
13092

13193
/**
132-
* Returns the cache source object.
133-
*
134-
* @return \wcf\system\cache\source\ICacheSource
94+
* Returns the cache adapter.
13595
*/
136-
public function getCacheSource()
96+
public function getCacheAdapter(): TagAwareCacheInterface & TagAwareAdapterInterface
13797
{
138-
return $this->cacheSource;
98+
return $this->cache;
13999
}
140100

141101
/**
@@ -162,7 +122,7 @@ public function sanityCheck(): bool
162122
{
163123
if (
164124
CACHE_SOURCE_TYPE != 'disk'
165-
&& \get_class(CacheHandler::getInstance()->getCacheSource()) === \wcf\system\cache\source\DiskCacheSource::class
125+
&& \get_class($this->cache) === FilesystemTagAwareAdapter::class
166126
) {
167127
return false;
168128
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace wcf\system\cache;
4+
5+
use Symfony\Contracts\Cache\ItemInterface;
6+
7+
/**
8+
* @author Olaf Braun
9+
* @copyright 2001-2025 WoltLab GmbH
10+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
* @since 6.2
12+
*
13+
* @template T of array|object
14+
*/
15+
interface ICacheCallback
16+
{
17+
/**
18+
* Generates the cache data and returns it.
19+
* This method MUST NOT rely on any (runtime) cache at any point because those could be stale.
20+
*
21+
* @return T
22+
*/
23+
public function __invoke(ItemInterface $item): array|object;
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace wcf\system\cache\adapter;
4+
5+
use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
6+
7+
/**
8+
* @author Olaf Braun
9+
* @copyright 2001-2025 WoltLab GmbH
10+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
* @since 6.2
12+
*/
13+
final class DiskCacheAdapter implements ICacheAdapter
14+
{
15+
#[\Override]
16+
public static function getAdapter(): FilesystemTagAwareAdapter
17+
{
18+
return new FilesystemTagAwareAdapter(namespace: 'cache', directory: WCF_DIR);
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace wcf\system\cache\adapter;
4+
5+
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
6+
use Symfony\Contracts\Cache\TagAwareCacheInterface;
7+
8+
/**
9+
* @author Olaf Braun
10+
* @copyright 2001-2025 WoltLab GmbH
11+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12+
* @since 6.2
13+
*/
14+
interface ICacheAdapter
15+
{
16+
public static function getAdapter(): TagAwareCacheInterface & TagAwareAdapterInterface;
17+
}

0 commit comments

Comments
 (0)