22
33namespace 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 ;
810use 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 }
0 commit comments