vendor/interquest/frivol-common/src/Service/CacheService.php line 146

Open in your IDE?
  1. <?php
  2. namespace Frivol\Common\Service;
  3. use App\Event\Content\SingleContentCacheClearedEvent;
  4. use Symfony\Component\Cache\CacheItem;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Contracts\Cache\ItemInterface;
  7. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  8. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  9. class CacheService
  10. {
  11.     const VERSION_TAG_NAME "frivol___versionTag";
  12.     /**
  13.      * @var TagAwareCacheInterface
  14.      */
  15.     protected TagAwareCacheInterface $cache;
  16.     /**
  17.      * @var EventDispatcherInterface
  18.      */
  19.     protected EventDispatcherInterface $eventDispatcher;
  20.     /**
  21.      * CacheService constructor.
  22.      * @param TagAwareCacheInterface $cache
  23.      */
  24.     public function __construct(TagAwareCacheInterface $cacheEventDispatcherInterface $eventDispatcher)
  25.     {
  26.         $this->eventDispatcher $eventDispatcher;
  27.         $this->setCache($cache);
  28.     }
  29.     /**
  30.      * @return TagAwareCacheInterface
  31.      */
  32.     protected function getCache(): TagAwareCacheInterface
  33.     {
  34.         return $this->cache;
  35.     }
  36.     /**
  37.      * @param TagAwareCacheInterface $cache
  38.      * @return $this
  39.      */
  40.     protected function setCache(TagAwareCacheInterface $cache): self
  41.     {
  42.         $this->cache $cache;
  43.         return $this;
  44.     }
  45.     public function getEventDispatcher(): EventDispatcherInterface
  46.     {
  47.         return $this->eventDispatcher;
  48.     }
  49.     /**
  50.      * @param array $tags
  51.      * @return bool
  52.      * @throws \Psr\Cache\InvalidArgumentException
  53.      */
  54.     public function invalidateTags(array $tags): bool
  55.     {
  56.         return $this->getCache()->invalidateTags($tags);
  57.     }
  58.     public function getItem(string $key): CacheItem {
  59.         return $this->getCache()->getItem($key);
  60.     }
  61.     public function save(CacheItem $item): void {
  62.         $this->getCache()->save($item);
  63.     }
  64.     /**
  65.      * Updates the redis version tag, invalidating all old cache data by tag
  66.      *
  67.      * @return string new version tag
  68.      */
  69.     public function incrementVersionTag(): string {
  70.         $item $this->getCache()->getItem(self::VERSION_TAG_NAME);
  71.         $nb 0;
  72.         if($item->isHit() === true) {
  73.             $valueHelper explode("-"$item->get());
  74.             $nb = (int)array_pop($valueHelper);
  75.         }
  76.         if($nb === 100) {
  77.             $nb 0;
  78.         }
  79.         $nb++;
  80.         $toInvalidate = [];
  81.         for($i 0$i <= 100$i++) {
  82.             if ($i === $nb) {
  83.                 continue;
  84.             }
  85.             $toInvalidate[] = 'version-' $i;
  86.         }
  87.         $this->invalidateTags($toInvalidate);
  88.         $item->set('version-' $nb);
  89.         $this->getCache()->save($item);
  90.         return $item->get();
  91.     }
  92.     /**
  93.      * Returns the current active redis version tag
  94.      *
  95.      * @return string new version tag
  96.      */
  97.     public function getCurrentVersionTag(): string {
  98.         $version $this->getCache()->getItem(self::VERSION_TAG_NAME)->get();
  99.         if($version !== null) {
  100.             return $version;
  101.         }
  102.         return $this->incrementVersionTag();
  103.     }
  104.     /**
  105.      * Returns a single item from the cache. If it doesn't exist it gets
  106.      * created and the version tag added
  107.      *
  108.      * @param string $key key of the cache entry
  109.      * @param callable|null $cb callback setting the value if not present
  110.      * @return mixed cache value
  111.      */
  112.     public function get(string $key, callable $cb null): mixed {
  113.         $cacheResult $this->getCache()->getItem($key);
  114.         if($cacheResult->isHit() === true) {
  115.             return $cacheResult->get();
  116.         }
  117.         if($cb === null) {
  118.             return '';
  119.         }
  120.         $call = function(ItemInterface $item) use ($cb) {
  121.             $h $cb($item);
  122.             $item->tag([
  123.                 $this->getCurrentVersionTag()
  124.             ]);
  125.             return $h;
  126.         };
  127.         return $this->getCache()->get($key$call);
  128.     }
  129.     /**
  130.      * @param int $member
  131.      * @return bool
  132.      * @throws \Psr\Cache\InvalidArgumentException
  133.      */
  134.     public function clearMemberMediaForMember(int $member): bool
  135.     {
  136.         return $this->getCache()->invalidateTags([
  137.             'profilephotos-member-' $member,
  138.             'member-' $member
  139.         ]);
  140.     }
  141.     /**
  142.      * @param int $member
  143.      * @return bool
  144.      * @throws \Psr\Cache\InvalidArgumentException
  145.      */
  146.     public function clearMemberCoinsCache(int $member): bool
  147.     {
  148.         return $this->getCache()->delete('membercoins_' $member);
  149.     }
  150.     /**
  151.      * @param int $member
  152.      * @return bool
  153.      * @throws \Psr\Cache\InvalidArgumentException
  154.      */
  155.     public function clearMediaCenterCache(int $member): bool
  156.     {
  157.         return $this->getCache()->invalidateTags([
  158.             'mediacenter-' $member
  159.         ]);
  160.     }
  161.     public function clearMemberPropertyCache(int $memberId): bool
  162.     {
  163.         return $this->getCache()->invalidateTags([
  164.             $this->getMemberPropertyCacheTag($memberId)
  165.         ]);
  166.     }
  167.     public function clearWebmasterPropertyCache(int $webmasterId): bool
  168.     {
  169.         return $this->getCache()->invalidateTags([
  170.             $this->getWebmasterPropertyCacheTag($webmasterId)
  171.         ]);
  172.     }
  173.     public function clearPublicMemberPropertyCache(int $memberId): bool
  174.     {
  175.         return $this->getCache()->invalidateTags([
  176.             $this->getPublicMemberPropertyCacheTag($memberId)
  177.         ]);
  178.     }
  179.     public function getMemberPropertyCacheTag(int $memberId): string
  180.     {
  181.         return 'memberproperties-' $memberId;
  182.     }
  183.     /**
  184.      * @param int $webmasterId
  185.      * @return string
  186.      */
  187.     public function getWebmasterPropertyCacheTag(int $webmasterId): string
  188.     {
  189.         return 'webmasterproperties-' $webmasterId;
  190.     }
  191.     public function getPublicMemberPropertyCacheTag(int $memberId): string
  192.     {
  193.         return 'publicmemberproperties-' $memberId;
  194.     }
  195.     /**
  196.      * @param int $group
  197.      * @return bool
  198.      * @throws \Psr\Cache\InvalidArgumentException
  199.      */
  200.     public function clearUserGroupCache(int $group): bool
  201.     {
  202.         $this->getCache()->invalidateTags([
  203.             'group-' $group# for member_count stat in group overview
  204.         ]);
  205.         return $this->clearUserGroupMembershipCache($group);
  206.     }
  207.     /**
  208.      * @param int $group
  209.      * @return bool
  210.      * @throws \Psr\Cache\InvalidArgumentException
  211.      */
  212.     public function clearUserGroupMembershipCache(int $group): bool
  213.     {
  214.         return $this->getCache()->invalidateTags([
  215.             'group-members-' $group
  216.         ]);
  217.     }
  218.     /**
  219.      * @param int $group
  220.      * @return bool
  221.      * @throws \Psr\Cache\InvalidArgumentException
  222.      */
  223.     public function clearUserGroupThreadCache(int $group): bool
  224.     {
  225.         return $this->getCache()->invalidateTags([
  226.             'group-' $group# for post count stat in group overview
  227.             'group-threads-' $group,
  228.         ]);
  229.     }
  230.     /**
  231.      * @param int $group
  232.      * @param int $thread
  233.      * @return bool
  234.      * @throws \Psr\Cache\InvalidArgumentException
  235.      */
  236.     public function clearUserGroupThreadPostCache(int $groupint $thread): bool
  237.     {
  238.         return $this->getCache()->invalidateTags([
  239.             'group-' $group# for post count stat in group overview
  240.             'group-threads-' $group,
  241.             'group-posts-' $thread
  242.         ]);
  243.     }
  244.     /**
  245.      * @param int $imagesetId
  246.      * @return bool
  247.      * @throws \Psr\Cache\InvalidArgumentException
  248.      */
  249.     public function clearImagesetCache(int $imagesetId): bool
  250.     {
  251.         $this->getCache()->invalidateTags([
  252.             'imageset-' $imagesetId
  253.         ]);
  254.         return true;
  255.     }
  256.     /**
  257.      * @param int $imagesetId
  258.      * @return bool
  259.      * @throws \Psr\Cache\InvalidArgumentException
  260.      */
  261.     public function clearVideoCache(int $videoId): bool
  262.     {
  263.         $this->getCache()->invalidateTags([
  264.             'video-' $videoId
  265.         ]);
  266.         return true;
  267.     }
  268.     /**
  269.      * @param int $content
  270.      * @return bool
  271.      * @throws \Psr\Cache\InvalidArgumentException
  272.      * @throws \ReflectionException
  273.      * @throws NotFoundHttpException
  274.      */
  275.     public function clearContentCache(int $content): bool
  276.     {
  277.         $result $this->getCache()->invalidateTags([
  278.             'content-' $content
  279.         ]);
  280.         if($result) {
  281.             $this->getEventDispatcher()->dispatch(new SingleContentCacheClearedEvent($content));
  282.         }
  283.         return $result;
  284.     }
  285.     /**
  286.      * @param int $conversation
  287.      * @return bool
  288.      * @throws \Psr\Cache\InvalidArgumentException
  289.      */
  290.     public function clearConversationCache(int $conversation): bool
  291.     {
  292.         return $this->getCache()->invalidateTags([
  293.             'conversation-' $conversation,
  294.             'messenger-messages-' $conversation
  295.         ]);
  296.     }
  297.     public function clearImagesetPurchases(int $imagesetId): bool
  298.     {
  299.         return $this->getCache()->invalidateTags([
  300.             'imageset-purchase-' $imagesetId
  301.         ]);
  302.     }
  303.     public function clearAmateurStatistics(int $amateurMemberId): bool
  304.     {
  305.         return $this->getCache()->invalidateTags([
  306.             'statistics-' $amateurMemberId
  307.         ]);
  308.     }
  309.     public function clearGuestbookCache(int $memberId): bool
  310.     {
  311.         return $this->getCache()->invalidateTags([
  312.             'guestbook-' $memberId
  313.         ]);
  314.     }
  315.     /**
  316.      * @param int $memberId
  317.      * @return bool
  318.      * @throws \Psr\Cache\InvalidArgumentException
  319.      */
  320.     public function clearMassMessageMemberCache(int $memberId): bool
  321.     {
  322.         return $this->getCache()->invalidateTags([
  323.             'massmessage-member-' $memberId
  324.         ]);
  325.     }
  326.     /**
  327.      * @param int $massMessageId
  328.      * @return bool
  329.      * @throws \Psr\Cache\InvalidArgumentException
  330.      */
  331.     public function clearMassMessageCache(int $massMessageId): bool
  332.     {
  333.         return $this->getCache()->invalidateTags([
  334.             'massmessage',
  335.             'massmessage-' $massMessageId
  336.         ]);
  337.     }
  338.     public function clearActorsCacheFor(int $memberId nullint $contentId null): bool
  339.     {
  340.         $tags = [];
  341.         if ($memberId 0) {
  342.             $tags[] = 'member-actors-' $memberId;
  343.         }
  344.         if ($contentId 0) {
  345.             $tags[] = 'content-' $contentId;
  346.         }
  347.         if ($tags === []) {
  348.             return true;
  349.         }
  350.         return $this->getCache()->invalidateTags($tags);
  351.     }
  352.     public function clearAllActorsCache(): bool
  353.     {
  354.         return $this->getCache()->invalidateTags(['actors']);
  355.     }
  356.     /**
  357.      * @param int $member
  358.      * @return bool
  359.      * @throws \Psr\Cache\InvalidArgumentException
  360.      */
  361.     public function clearPhonenumberCache(int $member): bool
  362.     {
  363.         return $this->getCache()->invalidateTags([
  364.             'phonenumbers-' $member
  365.         ]);
  366.     }
  367.     /**
  368.      * @param int $member
  369.      * @return bool
  370.      * @throws \Psr\Cache\InvalidArgumentException
  371.      */
  372.     public function clearChatTemplateCache(int $member): bool
  373.     {
  374.         return $this->getCache()->invalidateTags([
  375.             'templates-member-' $member
  376.         ]);
  377.     }
  378.     /**
  379.      * @return bool
  380.      * @throws \Psr\Cache\InvalidArgumentException
  381.      */
  382.     public function clearMemberOnlineCache(): bool
  383.     {
  384.         return $this->getCache()->invalidateTags([
  385.             'memberonline'
  386.         ]);
  387.     }
  388. }