├── Changelog.md ├── LICENSE ├── PredisCachePool.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 | ### Fixed 22 | 23 | * Fixed `$path` variable not initialized in `clearOneObjectFromCache`. 24 | 25 | ## 0.5.0 26 | 27 | ### Added 28 | 29 | * Support for the new `TaggableCacheItemPoolInterface`. 30 | * Support for PSR-16 SimpleCache 31 | 32 | ### Changed 33 | 34 | * The behavior of `CacheItem::getTags()` has changed. It will not return the tags stored in the cache storage. 35 | 36 | ### Removed 37 | 38 | * `CacheItem::getExpirationDate()`. Use `CacheItem::getExpirationTimestamp()` 39 | * `CacheItem::getTags()`. Use `CacheItem::getPreviousTags()` 40 | * `CacheItem::addTag()`. Use `CacheItem::setTags()` 41 | 42 | ## 0.4.2 43 | 44 | ### Changed 45 | 46 | * Rely on `Predis\ClientInterface` instead of `Predis\Client` in `PredisCachePool` 47 | 48 | ## 0.4.1 49 | 50 | ### Changed 51 | 52 | * The `PredisCachePool::$cache` is now protected instead of private 53 | * Using `cache/hierarchical-cache:^0.3` 54 | 55 | ## 0.4.0 56 | 57 | * No changelog before this version 58 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /PredisCachePool.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\Adapter\Predis; 13 | 14 | use Cache\Adapter\Common\AbstractCachePool; 15 | use Cache\Adapter\Common\PhpCacheItem; 16 | use Cache\Hierarchy\HierarchicalCachePoolTrait; 17 | use Cache\Hierarchy\HierarchicalPoolInterface; 18 | use Predis\ClientInterface as Client; 19 | 20 | /** 21 | * @author Tobias Nyholm 22 | */ 23 | class PredisCachePool extends AbstractCachePool implements HierarchicalPoolInterface 24 | { 25 | use HierarchicalCachePoolTrait; 26 | 27 | /** 28 | * @type Client 29 | */ 30 | protected $cache; 31 | 32 | /** 33 | * @param Client $cache 34 | */ 35 | public function __construct(Client $cache) 36 | { 37 | $this->cache = $cache; 38 | } 39 | 40 | /** 41 | * {@inheritdoc} 42 | */ 43 | protected function fetchObjectFromCache($key) 44 | { 45 | if (false === $result = unserialize($this->cache->get($this->getHierarchyKey($key)))) { 46 | return [false, null, [], null]; 47 | } 48 | 49 | return $result; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | protected function clearAllObjectsFromCache() 56 | { 57 | return 'OK' === $this->cache->flushdb()->getPayload(); 58 | } 59 | 60 | /** 61 | * {@inheritdoc} 62 | */ 63 | protected function clearOneObjectFromCache($key) 64 | { 65 | $path = null; 66 | $keyString = $this->getHierarchyKey($key, $path); 67 | if ($path) { 68 | $this->cache->incr($path); 69 | } 70 | $this->clearHierarchyKeyCache(); 71 | 72 | return $this->cache->del($keyString) >= 0; 73 | } 74 | 75 | /** 76 | * {@inheritdoc} 77 | */ 78 | protected function storeItemInCache(PhpCacheItem $item, $ttl) 79 | { 80 | if ($ttl < 0) { 81 | return false; 82 | } 83 | 84 | $key = $this->getHierarchyKey($item->getKey()); 85 | $data = serialize([true, $item->get(), $item->getTags(), $item->getExpirationTimestamp()]); 86 | 87 | if ($ttl === null || $ttl === 0) { 88 | return 'OK' === $this->cache->set($key, $data)->getPayload(); 89 | } 90 | 91 | return 'OK' === $this->cache->setex($key, $ttl, $data)->getPayload(); 92 | } 93 | 94 | /** 95 | * {@inheritdoc} 96 | */ 97 | protected function getDirectValue($key) 98 | { 99 | return $this->cache->get($key); 100 | } 101 | 102 | /** 103 | * {@inheritdoc} 104 | */ 105 | protected function appendListItem($name, $value) 106 | { 107 | $this->cache->lpush($name, $value); 108 | } 109 | 110 | /** 111 | * {@inheritdoc} 112 | */ 113 | protected function getList($name) 114 | { 115 | return $this->cache->lrange($name, 0, -1); 116 | } 117 | 118 | /** 119 | * {@inheritdoc} 120 | */ 121 | protected function removeList($name) 122 | { 123 | return $this->cache->del($name); 124 | } 125 | 126 | /** 127 | * {@inheritdoc} 128 | */ 129 | protected function removeListItem($name, $key) 130 | { 131 | return $this->cache->lrem($name, 0, $key); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Predis 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/predis-adapter/v/stable)](https://packagist.org/packages/cache/predis-adapter) 4 | [![codecov.io](https://codecov.io/github/php-cache/predis-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/predis-adapter?branch=master) 5 | [![Total Downloads](https://poser.pugx.org/cache/predis-adapter/downloads)](https://packagist.org/packages/cache/predis-adapter) 6 | [![Monthly Downloads](https://poser.pugx.org/cache/predis-adapter/d/monthly.png)](https://packagist.org/packages/cache/predis-adapter) 7 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 8 | 9 | This is a PSR-6 cache implementation using Predis. 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](http://www.php-cache.com). 11 | 12 | This implementation is using [Predis](https://github.com/nrk/predis). If you want an adapter with 13 | [PhpRedis](https://github.com/phpredis/phpredis) you should look at our [Redis adapter](https://github.com/php-cache/redis-adapter). 14 | 15 | ### Install 16 | 17 | ```bash 18 | composer require cache/predis-adapter 19 | ``` 20 | 21 | ### Use 22 | 23 | To create an instance of `PredisCachePool` you need to configure a `\Predis\Client` object. 24 | 25 | ```php 26 | $client = new \Predis\Client('tcp:/127.0.0.1:6379'); 27 | $pool = new PredisCachePool($client); 28 | ``` 29 | 30 | ### Contribute 31 | 32 | Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or 33 | report any issues you find on the [issue tracker](http://issues.php-cache.com). 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cache/predis-adapter", 3 | "description": "A PSR-6 cache implementation using Redis (Predis). This implementation supports tags", 4 | "license": "MIT", 5 | "type": "library", 6 | "keywords": [ 7 | "cache", 8 | "psr-6", 9 | "redis", 10 | "predis", 11 | "tag" 12 | ], 13 | "authors": [ 14 | { 15 | "name": "Aaron Scherer", 16 | "email": "aequasi@gmail.com", 17 | "homepage": "https://github.com/aequasi" 18 | }, 19 | { 20 | "name": "Tobias Nyholm", 21 | "email": "tobias.nyholm@gmail.com", 22 | "homepage": "https://github.com/nyholm" 23 | } 24 | ], 25 | "homepage": "http://www.php-cache.com/en/latest/", 26 | "require": { 27 | "php": ">=7.4", 28 | "cache/adapter-common": "^1.0", 29 | "cache/hierarchical-cache": "^1.0", 30 | "predis/predis": "^1.1", 31 | "psr/cache": "^1.0 || ^2.0", 32 | "psr/simple-cache": "^1.0" 33 | }, 34 | "require-dev": { 35 | "cache/integration-tests": "^0.17", 36 | "phpunit/phpunit": "^7.5.20 || ^9.5.10" 37 | }, 38 | "provide": { 39 | "psr/cache-implementation": "^1.0", 40 | "psr/simple-cache-implementation": "^1.0" 41 | }, 42 | "minimum-stability": "dev", 43 | "prefer-stable": true, 44 | "autoload": { 45 | "psr-4": { 46 | "Cache\\Adapter\\Predis\\": "" 47 | }, 48 | "exclude-from-classmap": [ 49 | "/Tests/" 50 | ] 51 | }, 52 | "extra": { 53 | "branch-alias": { 54 | "dev-master": "1.1-dev" 55 | } 56 | } 57 | } 58 | --------------------------------------------------------------------------------