├── Changelog.md ├── LICENSE ├── NamespacedCachePool.php ├── README.md └── composer.json /Changelog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. 4 | 5 | ## UNRELEASED 6 | 7 | ## 1.2.0 8 | 9 | * Support for PHP 8.1 10 | * Drop support for PHP < 7.4 11 | * Allow psr/cache: ^1.0 || ^2.0 12 | 13 | ## 1.1.0 14 | 15 | ### Added 16 | 17 | * Support for PHP 8 18 | 19 | ## 1.0.0 20 | 21 | ### Added 22 | 23 | * NamespacedCachePool implements HierarchicalPoolInterface 24 | 25 | ## 0.1.3 26 | 27 | ### Changed 28 | 29 | * Updated dependencies 30 | 31 | ## 0.1.2 32 | 33 | ### Fixed 34 | 35 | * Typos, documentation and general package improvements. 36 | 37 | ## 0.1.1 38 | 39 | ### Changed 40 | 41 | * Updated type hints for the cache pool. 42 | * Using `cache/hierarchical-cache:^0.3` 43 | 44 | ## 0.1.0 45 | 46 | * First release 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Aaron Scherer, Tobias Nyholm 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /NamespacedCachePool.php: -------------------------------------------------------------------------------- 1 | , Tobias Nyholm 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace Cache\Namespaced; 13 | 14 | use Cache\Hierarchy\HierarchicalPoolInterface; 15 | use Psr\Cache\CacheItemInterface; 16 | 17 | /** 18 | * Prefix all the stored items with a namespace. Also make sure you can clear all items 19 | * in that namespace. 20 | * 21 | * @author Tobias Nyholm 22 | */ 23 | class NamespacedCachePool implements HierarchicalPoolInterface 24 | { 25 | /** 26 | * @type HierarchicalPoolInterface 27 | */ 28 | private $cachePool; 29 | 30 | /** 31 | * @type string 32 | */ 33 | private $namespace; 34 | 35 | /** 36 | * @param HierarchicalPoolInterface $cachePool 37 | * @param string $namespace 38 | */ 39 | public function __construct(HierarchicalPoolInterface $cachePool, $namespace) 40 | { 41 | $this->cachePool = $cachePool; 42 | $this->namespace = $namespace; 43 | } 44 | 45 | /** 46 | * Add namespace prefix on the key. 47 | * 48 | * @param array $keys 49 | */ 50 | private function prefixValue(&$key) 51 | { 52 | // |namespace|key 53 | $key = HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace.HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$key; 54 | } 55 | 56 | /** 57 | * @param array $keys 58 | */ 59 | private function prefixValues(array &$keys) 60 | { 61 | foreach ($keys as &$key) { 62 | $this->prefixValue($key); 63 | } 64 | } 65 | 66 | /** 67 | * {@inheritdoc} 68 | */ 69 | public function getItem($key) 70 | { 71 | $this->prefixValue($key); 72 | 73 | return $this->cachePool->getItem($key); 74 | } 75 | 76 | /** 77 | * {@inheritdoc} 78 | */ 79 | public function getItems(array $keys = []) 80 | { 81 | $this->prefixValues($keys); 82 | 83 | return $this->cachePool->getItems($keys); 84 | } 85 | 86 | /** 87 | * {@inheritdoc} 88 | */ 89 | public function hasItem($key) 90 | { 91 | $this->prefixValue($key); 92 | 93 | return $this->cachePool->hasItem($key); 94 | } 95 | 96 | /** 97 | * {@inheritdoc} 98 | */ 99 | public function clear() 100 | { 101 | return $this->cachePool->deleteItem(HierarchicalPoolInterface::HIERARCHY_SEPARATOR.$this->namespace); 102 | } 103 | 104 | /** 105 | * {@inheritdoc} 106 | */ 107 | public function deleteItem($key) 108 | { 109 | $this->prefixValue($key); 110 | 111 | return $this->cachePool->deleteItem($key); 112 | } 113 | 114 | /** 115 | * {@inheritdoc} 116 | */ 117 | public function deleteItems(array $keys) 118 | { 119 | $this->prefixValues($keys); 120 | 121 | return $this->cachePool->deleteItems($keys); 122 | } 123 | 124 | /** 125 | * {@inheritdoc} 126 | */ 127 | public function save(CacheItemInterface $item) 128 | { 129 | return $this->cachePool->save($item); 130 | } 131 | 132 | /** 133 | * {@inheritdoc} 134 | */ 135 | public function saveDeferred(CacheItemInterface $item) 136 | { 137 | return $this->cachePool->saveDeferred($item); 138 | } 139 | 140 | /** 141 | * {@inheritdoc} 142 | */ 143 | public function commit() 144 | { 145 | return $this->cachePool->commit(); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Namespaced PSR-6 cache pool 2 | [![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) 3 | [![Latest Stable Version](https://poser.pugx.org/cache/namespaced-cache/v/stable)](https://packagist.org/packages/cache/namespaced-cache) 4 | [![codecov.io](https://codecov.io/github/php-cache/namespaced-cache/coverage.svg?branch=master)](https://codecov.io/github/php-cache/namespaced-cache?branch=master) 5 | [![Total Downloads](https://poser.pugx.org/cache/namespaced-cache/downloads)](https://packagist.org/packages/cache/namespaced-cache) 6 | [![Monthly Downloads](https://poser.pugx.org/cache/namespaced-cache/d/monthly.png)](https://packagist.org/packages/cache/namespaced-cache) 7 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 8 | 9 | This is a decorator for a PSR-6 hierarchical cache. It will allow you to use namespaces. 10 | 11 | It is a part of the PHP Cache organisation. To read about features like tagging and hierarchy support please read 12 | the shared documentation at [www.php-cache.com](http://www.php-cache.com). 13 | 14 | ### Install 15 | 16 | ```bash 17 | composer require cache/namespaced-cache 18 | ``` 19 | 20 | ### Use 21 | 22 | ```php 23 | $hierarchyPool = new RedisCachePool($client); 24 | $namespacedPool = new NamespacedCachePool($hierarchyPool, 'acme'); 25 | ``` 26 | 27 | ### Contribute 28 | 29 | Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or 30 | report any issues you find on the [issue tracker](http://issues.php-cache.com). 31 | 32 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cache/namespaced-cache", 3 | "description": "A decorator that makes your cache support namespaces", 4 | "license": "MIT", 5 | "type": "library", 6 | "keywords": [ 7 | "cache", 8 | "psr-6", 9 | "namespace" 10 | ], 11 | "authors": [ 12 | { 13 | "name": "Tobias Nyholm", 14 | "email": "tobias.nyholm@gmail.com", 15 | "homepage": "https://github.com/nyholm" 16 | } 17 | ], 18 | "homepage": "http://www.php-cache.com/en/latest/", 19 | "require": { 20 | "php": ">=7.4", 21 | "cache/hierarchical-cache": "^1.0", 22 | "psr/cache": "^1.0 || ^2.0" 23 | }, 24 | "require-dev": { 25 | "cache/memcached-adapter": "^1.0", 26 | "phpunit/phpunit": "^7.5.20 || ^9.5.10" 27 | }, 28 | "minimum-stability": "dev", 29 | "prefer-stable": true, 30 | "autoload": { 31 | "psr-4": { 32 | "Cache\\Namespaced\\": "" 33 | }, 34 | "exclude-from-classmap": [ 35 | "/Tests/" 36 | ] 37 | }, 38 | "extra": { 39 | "branch-alias": { 40 | "dev-master": "1.1-dev" 41 | } 42 | } 43 | } 44 | --------------------------------------------------------------------------------