‪TYPO3CMS  ‪main
CachingFrameworkStorage.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
21 use Symfony\Component\RateLimiter\LimiterStateInterface;
22 use Symfony\Component\RateLimiter\Policy\SlidingWindow;
23 use Symfony\Component\RateLimiter\Policy\TokenBucket;
24 use Symfony\Component\RateLimiter\Policy\Window;
25 use Symfony\Component\RateLimiter\Storage\StorageInterface;
28 
34 #[Autoconfigure(public: true)]
35 class ‪CachingFrameworkStorage implements StorageInterface
36 {
38 
40  {
41  $this->cacheInstance = ‪$cacheInstance->getCache('ratelimiter');
42  $this->cacheInstance->‪collectGarbage();
43  }
44 
45  public function ‪save(LimiterStateInterface $limiterState): void
46  {
47  $this->cacheInstance->set(
48  sha1($limiterState->getId()),
49  serialize($limiterState),
50  [],
51  $limiterState->getExpirationTime()
52  );
53  }
54 
55  public function ‪fetch(string $limiterStateId): ?LimiterStateInterface
56  {
57  $cacheItem = $this->cacheInstance->get(sha1($limiterStateId));
58  if ($cacheItem) {
59  $value = unserialize($cacheItem, ['allowed_classes' => [Window::class, SlidingWindow::class, TokenBucket::class]]);
60  if ($value instanceof LimiterStateInterface) {
61  return $value;
62  }
63  }
64 
65  return null;
66  }
67 
68  public function delete(string $limiterStateId): void
69  {
70  $this->cacheInstance->remove(sha1($limiterStateId));
71  }
72 }
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\__construct
‪__construct(CacheManager $cacheInstance)
Definition: CachingFrameworkStorage.php:39
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\save
‪save(LimiterStateInterface $limiterState)
Definition: CachingFrameworkStorage.php:45
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\$cacheInstance
‪FrontendInterface $cacheInstance
Definition: CachingFrameworkStorage.php:37
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage
Definition: CachingFrameworkStorage.php:36
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\fetch
‪fetch(string $limiterStateId)
Definition: CachingFrameworkStorage.php:55
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\RateLimiter\Storage
Definition: CachingFrameworkStorage.php:18
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface\collectGarbage
‪collectGarbage()