├── Changelog.md ├── Exception ├── CacheException.php └── InvalidArgumentException.php ├── LICENSE ├── README.md ├── SimpleCacheBridge.php └── 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 | * No changes since 0.1.1 22 | 23 | ## 0.1.1 24 | 25 | ### Fixed 26 | 27 | * Bugs with iterators 28 | 29 | ## 0.1.0 30 | 31 | * First release 32 | -------------------------------------------------------------------------------- /Exception/CacheException.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\Bridge\SimpleCache\Exception; 13 | 14 | class CacheException extends \RuntimeException implements \Psr\SimpleCache\CacheException 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /Exception/InvalidArgumentException.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\Bridge\SimpleCache\Exception; 13 | 14 | class InvalidArgumentException extends CacheException implements \Psr\SimpleCache\InvalidArgumentException 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSR-6 to PSR-16 Bridge (Simple cache) 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/simple-cache-bridge/v/stable)](https://packagist.org/packages/cache/simple-cache-bridge) 4 | [![codecov.io](https://codecov.io/github/php-cache/simple-cache-bridge/coverage.svg?branch=master)](https://codecov.io/github/array-cache/apc-adapter?branch=master) 5 | [![Total Downloads](https://poser.pugx.org/cache/simple-cache-bridge/downloads)](https://packagist.org/packages/cache/simple-cache-bridge) 6 | [![Monthly Downloads](https://poser.pugx.org/cache/simple-cache-bridge/d/monthly.png)](https://packagist.org/packages/cache/simple-cache-bridge) 7 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 8 | 9 | This is a bridge that converts a PSR-6 cache implementation to PSR-16 (SimpleCache). It is a part of the PHP Cache organisation. To read about 10 | features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](https://www.php-cache.com). 11 | 12 | ### Install 13 | 14 | ```bash 15 | composer require cache/simple-cache-bridge 16 | ``` 17 | 18 | ### Use 19 | 20 | You need an existing PSR-6 pool as a constructor argument to the bridge. 21 | 22 | ```php 23 | $psr6pool = new ArrayCachePool(); 24 | $simpleCache = new SimpleCacheBridge($psr6pool); 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 | -------------------------------------------------------------------------------- /SimpleCacheBridge.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\Bridge\SimpleCache; 13 | 14 | use Cache\Bridge\SimpleCache\Exception\InvalidArgumentException; 15 | use Psr\Cache\CacheItemInterface; 16 | use Psr\Cache\CacheItemPoolInterface; 17 | use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException; 18 | use Psr\SimpleCache\CacheInterface; 19 | 20 | /** 21 | * Adds a SimpleCache interface on a PSR-6 cache pool. 22 | * 23 | * @author Magnus Nordlander 24 | */ 25 | class SimpleCacheBridge implements CacheInterface 26 | { 27 | /** 28 | * @type CacheItemPoolInterface 29 | */ 30 | protected $cacheItemPool; 31 | 32 | /** 33 | * SimpleCacheBridge constructor. 34 | */ 35 | public function __construct(CacheItemPoolInterface $cacheItemPool) 36 | { 37 | $this->cacheItemPool = $cacheItemPool; 38 | } 39 | 40 | /** 41 | * {@inheritdoc} 42 | */ 43 | public function get($key, $default = null) 44 | { 45 | try { 46 | $item = $this->cacheItemPool->getItem($key); 47 | } catch (CacheInvalidArgumentException $e) { 48 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 49 | } 50 | 51 | if (!$item->isHit()) { 52 | return $default; 53 | } 54 | 55 | return $item->get(); 56 | } 57 | 58 | /** 59 | * {@inheritdoc} 60 | */ 61 | public function set($key, $value, $ttl = null) 62 | { 63 | try { 64 | $item = $this->cacheItemPool->getItem($key); 65 | $item->expiresAfter($ttl); 66 | } catch (CacheInvalidArgumentException $e) { 67 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 68 | } 69 | 70 | $item->set($value); 71 | 72 | return $this->cacheItemPool->save($item); 73 | } 74 | 75 | /** 76 | * {@inheritdoc} 77 | */ 78 | public function delete($key) 79 | { 80 | try { 81 | return $this->cacheItemPool->deleteItem($key); 82 | } catch (CacheInvalidArgumentException $e) { 83 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 84 | } 85 | } 86 | 87 | /** 88 | * {@inheritdoc} 89 | */ 90 | public function clear() 91 | { 92 | return $this->cacheItemPool->clear(); 93 | } 94 | 95 | /** 96 | * {@inheritdoc} 97 | */ 98 | public function getMultiple($keys, $default = null) 99 | { 100 | if (!is_array($keys)) { 101 | if (!$keys instanceof \Traversable) { 102 | throw new InvalidArgumentException('$keys is neither an array nor Traversable'); 103 | } 104 | 105 | // Since we need to throw an exception if *any* key is invalid, it doesn't 106 | // make sense to wrap iterators or something like that. 107 | $keys = iterator_to_array($keys, false); 108 | } 109 | 110 | try { 111 | $items = $this->cacheItemPool->getItems($keys); 112 | } catch (CacheInvalidArgumentException $e) { 113 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 114 | } 115 | 116 | return $this->generateValues($default, $items); 117 | } 118 | 119 | /** 120 | * @param $default 121 | * @param $items 122 | * 123 | * @return \Generator 124 | */ 125 | private function generateValues($default, $items) 126 | { 127 | foreach ($items as $key => $item) { 128 | /** @type $item CacheItemInterface */ 129 | if (!$item->isHit()) { 130 | yield $key => $default; 131 | } else { 132 | yield $key => $item->get(); 133 | } 134 | } 135 | } 136 | 137 | /** 138 | * {@inheritdoc} 139 | */ 140 | public function setMultiple($values, $ttl = null) 141 | { 142 | if (!is_array($values)) { 143 | if (!$values instanceof \Traversable) { 144 | throw new InvalidArgumentException('$values is neither an array nor Traversable'); 145 | } 146 | } 147 | 148 | $keys = []; 149 | $arrayValues = []; 150 | foreach ($values as $key => $value) { 151 | if (is_int($key)) { 152 | $key = (string) $key; 153 | } 154 | 155 | if (!is_string($key)) { 156 | throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', gettype($key))); 157 | } 158 | 159 | if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) { 160 | throw new InvalidArgumentException(sprintf('Invalid key: "%s". The key contains one or more characters reserved for future extension: {}()/\@:', $key)); 161 | } 162 | 163 | $keys[] = $key; 164 | $arrayValues[$key] = $value; 165 | } 166 | 167 | try { 168 | $items = $this->cacheItemPool->getItems($keys); 169 | } catch (CacheInvalidArgumentException $e) { 170 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 171 | } 172 | 173 | $itemSuccess = true; 174 | 175 | foreach ($items as $key => $item) { 176 | /* @var $item CacheItemInterface */ 177 | $item->set($arrayValues[$key]); 178 | 179 | try { 180 | $item->expiresAfter($ttl); 181 | } catch (CacheInvalidArgumentException $e) { 182 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 183 | } 184 | 185 | $itemSuccess = $itemSuccess && $this->cacheItemPool->saveDeferred($item); 186 | } 187 | 188 | return $itemSuccess && $this->cacheItemPool->commit(); 189 | } 190 | 191 | /** 192 | * {@inheritdoc} 193 | */ 194 | public function deleteMultiple($keys) 195 | { 196 | if (!is_array($keys)) { 197 | if (!$keys instanceof \Traversable) { 198 | throw new InvalidArgumentException('$keys is neither an array nor Traversable'); 199 | } 200 | 201 | // Since we need to throw an exception if *any* key is invalid, it doesn't 202 | // make sense to wrap iterators or something like that. 203 | $keys = iterator_to_array($keys, false); 204 | } 205 | 206 | try { 207 | return $this->cacheItemPool->deleteItems($keys); 208 | } catch (CacheInvalidArgumentException $e) { 209 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 210 | } 211 | } 212 | 213 | /** 214 | * {@inheritdoc} 215 | */ 216 | public function has($key) 217 | { 218 | try { 219 | return $this->cacheItemPool->hasItem($key); 220 | } catch (CacheInvalidArgumentException $e) { 221 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); 222 | } 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cache/simple-cache-bridge", 3 | "description": "A PSR-6 bridge to PSR-16. This will make any PSR-6 cache compatible with SimpleCache.", 4 | "license": "MIT", 5 | "type": "library", 6 | "keywords": [ 7 | "cache", 8 | "psr-6", 9 | "psr-16", 10 | "bridge" 11 | ], 12 | "authors": [ 13 | { 14 | "name": "Magnus Nordlander", 15 | "email": "magnus@fervo.se", 16 | "homepage": "https://github.com/magnusnordlander" 17 | } 18 | ], 19 | "homepage": "http://www.php-cache.com/en/latest/", 20 | "require": { 21 | "php": ">=7.4", 22 | "psr/cache": "^1.0 || ^2.0", 23 | "psr/simple-cache": "^1.0" 24 | }, 25 | "require-dev": { 26 | "cache/integration-tests": "^0.17", 27 | "mockery/mockery": "^1.0", 28 | "phpunit/phpunit": "^7.5.20 || ^9.5.10", 29 | "symfony/cache": "^3.2" 30 | }, 31 | "provide": { 32 | "psr/simple-cache-implementation": "^1.0" 33 | }, 34 | "minimum-stability": "dev", 35 | "prefer-stable": true, 36 | "autoload": { 37 | "psr-4": { 38 | "Cache\\Bridge\\SimpleCache\\": "" 39 | }, 40 | "exclude-from-classmap": [ 41 | "/Tests/" 42 | ] 43 | }, 44 | "extra": { 45 | "branch-alias": { 46 | "dev-master": "1.1-dev" 47 | } 48 | } 49 | } 50 | --------------------------------------------------------------------------------