From 352e42140b4ce311903abc3015c4cebb606fb123 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 27 Jan 2020 13:12:30 +0100 Subject: [PATCH] Add own storage backend to respect tags when caches are saved in fs --- Classes/Cache/Backend/TaggableFileBackend.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Classes/Cache/Backend/TaggableFileBackend.php diff --git a/Classes/Cache/Backend/TaggableFileBackend.php b/Classes/Cache/Backend/TaggableFileBackend.php new file mode 100644 index 0000000..ed81fe8 --- /dev/null +++ b/Classes/Cache/Backend/TaggableFileBackend.php @@ -0,0 +1,84 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use TYPO3\CMS\Core\Cache\Backend\FileBackend as CoreFileBackend; +use TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +class TaggableFileBackend extends CoreFileBackend implements TaggableBackendInterface +{ + public function flushByTag($tag) + { + array_walk($this->findIdentifiersByTag($tag), [$this, 'remove']); + } + + public function flushByTags(array $tags) + { + foreach ($tags as $tag) { + $this->flushByTag($tag); + } + } + + public function findIdentifiersByTag($tag) + { + $files = GeneralUtility::getFilesInDir($this->getCacheDirectory() . 'tag-' . $tag); + + if ($files === []) { + return []; + } + + return array_values($files); + } + + public function set($entryIdentifier, $data, array $tags = [], $lifetime = null) + { + parent::set($entryIdentifier, $data, $tags, $lifetime); + + foreach ($tags as $tag) { + $folder = $this->getCacheDirectory() . 'tag-' . $tag; + if (!is_dir($folder)) { + GeneralUtility::mkdir_deep($folder); + } + touch($folder . DIRECTORY_SEPARATOR . $entryIdentifier); + } + + file_put_contents( + $this->getCacheDirectory() . $entryIdentifier . '-tags', + serialize($tags) + ); + } + + public function remove($entryIdentifier) + { + $pathAndFilename = $this->getCacheDirectory() . $entryIdentifier . '-tags'; + if (file_exists($pathAndFilename)) { + $tags = unserialize(file_get_contents($pathAndFilename)); + foreach ($tags as $tag) { + $tagFile = $this->getCacheDirectory() . 'tag-' . $tag . DIRECTORY_SEPARATOR . $entryIdentifier; + file_exists($tagFile) && unlink($tagFile); + } + } + + return parent::remove($entryIdentifier); + } +}