‪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\RateLimiter\LimiterStateInterface;
21 use Symfony\Component\RateLimiter\Policy\SlidingWindow;
22 use Symfony\Component\RateLimiter\Policy\TokenBucket;
23 use Symfony\Component\RateLimiter\Policy\Window;
24 use Symfony\Component\RateLimiter\Storage\StorageInterface;
27 
33 class ‪CachingFrameworkStorage implements StorageInterface
34 {
36 
38  {
39  $this->cacheInstance = ‪$cacheInstance->getCache('ratelimiter');
40  $this->cacheInstance->‪collectGarbage();
41  }
42 
43  public function ‪save(LimiterStateInterface $limiterState): void
44  {
45  $this->cacheInstance->set(
46  sha1($limiterState->getId()),
47  serialize($limiterState),
48  [],
49  $limiterState->getExpirationTime()
50  );
51  }
52 
53  public function ‪fetch(string $limiterStateId): ?LimiterStateInterface
54  {
55  $cacheItem = $this->cacheInstance->get(sha1($limiterStateId));
56  if ($cacheItem) {
57  $value = unserialize($cacheItem, ['allowed_classes' => [Window::class, SlidingWindow::class, TokenBucket::class]]);
58  if ($value instanceof LimiterStateInterface) {
59  return $value;
60  }
61  }
62 
63  return null;
64  }
65 
66  public function delete(string $limiterStateId): void
67  {
68  $this->cacheInstance->remove(sha1($limiterStateId));
69  }
70 }
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\__construct
‪__construct(CacheManager $cacheInstance)
Definition: CachingFrameworkStorage.php:37
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\save
‪save(LimiterStateInterface $limiterState)
Definition: CachingFrameworkStorage.php:43
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\$cacheInstance
‪FrontendInterface $cacheInstance
Definition: CachingFrameworkStorage.php:35
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage
Definition: CachingFrameworkStorage.php:34
‪TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage\fetch
‪fetch(string $limiterStateId)
Definition: CachingFrameworkStorage.php:53
‪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()