├── .github └── workflows │ ├── continuous-integration.yml │ └── release-on-milestone-closed-triggering-release-event.yml ├── LICENSE ├── README.md ├── composer.json ├── namespace-bc-aliases.php └── src ├── Exception ├── CacheException.php └── InvalidArgumentException.php └── SimpleCacheAdapter.php /.github/workflows/continuous-integration.yml: -------------------------------------------------------------------------------- 1 | # See https://github.com/laminas/laminas-continuous-integration-action 2 | # Generates a job matrix based on current dependencies and supported version 3 | # ranges, then runs all those jobs 4 | name: "Continuous Integration" 5 | 6 | on: 7 | pull_request: 8 | push: 9 | 10 | jobs: 11 | matrix: 12 | name: Generate job matrix 13 | runs-on: ubuntu-latest 14 | outputs: 15 | matrix: ${{ steps.matrix.outputs.matrix }} 16 | steps: 17 | - name: Gather CI configuration 18 | id: matrix 19 | uses: laminas/laminas-ci-matrix-action@1.22.1 20 | 21 | qa: 22 | name: QA Checks 23 | needs: [ matrix ] 24 | runs-on: ${{ matrix.operatingSystem }} 25 | strategy: 26 | fail-fast: false 27 | matrix: ${{ fromJSON(needs.matrix.outputs.matrix) }} 28 | steps: 29 | - name: ${{ matrix.name }} 30 | uses: laminas/laminas-continuous-integration-action@1.32.0 31 | env: 32 | "GITHUB_TOKEN": ${{ secrets.GITHUB_TOKEN }} 33 | "INFECTION_DASHBOARD_API_KEY": ${{ secrets.INFECTION_DASHBOARD_API_KEY }} 34 | "STRYKER_DASHBOARD_API_KEY": ${{ secrets.STRYKER_DASHBOARD_API_KEY }} 35 | with: 36 | job: ${{ matrix.job }} 37 | -------------------------------------------------------------------------------- /.github/workflows/release-on-milestone-closed-triggering-release-event.yml: -------------------------------------------------------------------------------- 1 | # Alternate workflow example. 2 | # This one is identical to the one in release-on-milestone.yml, with one change: 3 | # the Release step uses the ORGANIZATION_ADMIN_TOKEN instead, to allow it to 4 | # trigger a release workflow event. This is useful if you have other actions 5 | # that intercept that event. 6 | 7 | name: "Automatic Releases" 8 | 9 | on: 10 | milestone: 11 | types: 12 | - "closed" 13 | 14 | jobs: 15 | release: 16 | name: "GIT tag, release & create merge-up PR" 17 | runs-on: ubuntu-latest 18 | 19 | steps: 20 | - name: "Checkout" 21 | uses: "actions/checkout@v4" 22 | 23 | - name: "Release" 24 | uses: "laminas/automatic-releases@v1" 25 | with: 26 | command-name: "laminas:automatic-releases:release" 27 | env: 28 | "GITHUB_TOKEN": ${{ secrets.ORGANIZATION_ADMIN_TOKEN }} 29 | "SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }} 30 | "GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }} 31 | "GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }} 32 | 33 | - name: "Create Merge-Up Pull Request" 34 | uses: "laminas/automatic-releases@v1" 35 | with: 36 | command-name: "laminas:automatic-releases:create-merge-up-pull-request" 37 | env: 38 | "GITHUB_TOKEN": ${{ secrets.GITHUB_TOKEN }} 39 | "SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }} 40 | "GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }} 41 | "GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }} 42 | 43 | - name: "Create and/or Switch to new Release Branch" 44 | uses: "laminas/automatic-releases@v1" 45 | with: 46 | command-name: "laminas:automatic-releases:switch-default-branch-to-next-minor" 47 | env: 48 | "GITHUB_TOKEN": ${{ secrets.ORGANIZATION_ADMIN_TOKEN }} 49 | "SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }} 50 | "GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }} 51 | "GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }} 52 | 53 | - name: "Create new milestones" 54 | uses: "laminas/automatic-releases@v1" 55 | with: 56 | command-name: "laminas:automatic-releases:create-milestones" 57 | env: 58 | "GITHUB_TOKEN": ${{ secrets.GITHUB_TOKEN }} 59 | "SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }} 60 | "GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }} 61 | "GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }} 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Roave, LLC. 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Doctrine SimpleCache adapter 2 | 3 | [![Build Status](https://travis-ci.org/Roave/DoctrineSimpleCache.svg?branch=master)](https://travis-ci.org/Roave/DoctrineSimpleCache) 4 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Roave/DoctrineSimpleCache/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Roave/DoctrineSimpleCache/?branch=master) 5 | [![Code Coverage](https://scrutinizer-ci.com/g/Roave/DoctrineSimpleCache/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Roave/DoctrineSimpleCache/?branch=master) 6 | [![Latest Stable Version](https://poser.pugx.org/roave/doctrine-simplecache/v/stable)](https://packagist.org/packages/roave/doctrine-simplecache) 7 | [![License](https://poser.pugx.org/roave/doctrine-simplecache/license)](https://packagist.org/packages/roave/doctrine-simplecache) 8 | 9 | [PSR-16 SimpleCache](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-16-simple-cache.md) 10 | implementation that accepts a Doctrine Cache and adapts it for the PSR-16 standards. 11 | 12 | ## Installation 13 | 14 | This will install `doctrine/cache` if not already installed. 15 | 16 | ```bash 17 | $ composer require roave/doctrine-simplecache 18 | ``` 19 | 20 | ## Usage 21 | 22 | Create your Doctrine Cache the usual way and inject it into `SimpleCacheAdapter`, for example: 23 | 24 | ```php 25 | doctrineCache = $doctrineCache; 26 | 27 | if (!$this->doctrineCache instanceof ClearableCache) { 28 | throw Exception\CacheException::fromNonClearableCache($this->doctrineCache); 29 | } 30 | 31 | if (!$this->doctrineCache instanceof MultiOperationCache) { 32 | throw Exception\CacheException::fromNonMultiOperationCache($this->doctrineCache); 33 | } 34 | } 35 | 36 | /** 37 | * @param mixed $key 38 | * @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException 39 | */ 40 | private function validateKey($key) : void 41 | { 42 | if (!is_string($key)) { 43 | throw Exception\InvalidArgumentException::fromInvalidType($key); 44 | } 45 | 46 | if ('' === $key) { 47 | throw Exception\InvalidArgumentException::fromEmptyKey(); 48 | } 49 | 50 | if (preg_match('/[' . preg_quote('{}()/\@:', '/') . ']/', $key)) { 51 | throw Exception\InvalidArgumentException::fromInvalidKeyCharacters($key); 52 | } 53 | } 54 | 55 | /** 56 | * @param mixed $keys 57 | * @return array 58 | * @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException 59 | */ 60 | private function filterValidateMultipleKeys($keys) : array 61 | { 62 | if ($keys instanceof \Traversable) { 63 | $keys = iterator_to_array($keys, false); 64 | } 65 | 66 | if (!is_array($keys)) { 67 | throw Exception\InvalidArgumentException::fromNonIterableKeys($keys); 68 | } 69 | 70 | array_map([$this, 'validateKey'], $keys); 71 | 72 | return $keys; 73 | } 74 | 75 | /** 76 | * {@inheritDoc} 77 | */ 78 | public function get($key, $default = null) 79 | { 80 | $this->validateKey($key); 81 | 82 | $value = $this->doctrineCache->fetch($key); 83 | if ($value === false) { 84 | // Doctrine cache returns `false` when cache doesn't contain, but also `false` if the value stored is 85 | // `false`, so check to see if the cache contains the key; if so, we probably meant to return `false` 86 | if ($this->doctrineCache->contains($key)) { 87 | return false; 88 | } 89 | return $default; 90 | } 91 | 92 | return $value; 93 | } 94 | 95 | /** 96 | * {@inheritDoc} 97 | */ 98 | public function set($key, $value, $ttl = null) : bool 99 | { 100 | $this->validateKey($key); 101 | 102 | if ($ttl === null) { 103 | return $this->doctrineCache->save($key, $value); 104 | } 105 | 106 | if ($ttl instanceof \DateInterval) { 107 | $ttl = $this->convertDateIntervalToInteger($ttl); 108 | } 109 | 110 | if (!is_int($ttl)) { 111 | throw InvalidArgumentException::fromKeyAndInvalidTTL($key, $ttl); 112 | } 113 | 114 | if ($ttl <= 0) { 115 | return $this->delete($key); 116 | } 117 | 118 | return $this->doctrineCache->save($key, $value, $ttl); 119 | } 120 | 121 | /** 122 | * {@inheritDoc} 123 | */ 124 | public function delete($key) : bool 125 | { 126 | $this->validateKey($key); 127 | return $this->doctrineCache->delete($key); 128 | } 129 | 130 | /** 131 | * {@inheritDoc} 132 | */ 133 | public function clear() : bool 134 | { 135 | return $this->doctrineCache->deleteAll(); 136 | } 137 | 138 | /** 139 | * @param array|\Traversable $keys 140 | * @param mixed $default 141 | * @return array 142 | * @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException 143 | */ 144 | public function getMultiple($keys, $default = null) : array 145 | { 146 | $keys = $this->filterValidateMultipleKeys($keys); 147 | return array_merge(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys)); 148 | } 149 | 150 | /** 151 | * @param array|\Traversable $values 152 | * @param null|int|\DateInterval $ttl 153 | * @return bool 154 | * @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException 155 | * @throws \Exception 156 | */ 157 | public function setMultiple($values, $ttl = null) : bool 158 | { 159 | if (!$values instanceof \Traversable && !is_array($values)) { 160 | throw Exception\InvalidArgumentException::fromNonIterableKeys($values); 161 | } 162 | 163 | $validatedValues = []; 164 | foreach ($values as $k => $v) { 165 | $this->validateKey($k); 166 | $validatedValues[$k] = $v; 167 | } 168 | 169 | if ($ttl === null) { 170 | return $this->doctrineCache->saveMultiple($validatedValues); 171 | } 172 | 173 | if ($ttl instanceof \DateInterval) { 174 | $ttl = $this->convertDateIntervalToInteger($ttl); 175 | } 176 | 177 | if (!is_int($ttl)) { 178 | throw InvalidArgumentException::fromKeyAndInvalidTTL(key($validatedValues), $ttl); 179 | } 180 | 181 | if ($ttl <= 0) { 182 | return $this->deleteMultiple(array_keys($validatedValues)); 183 | } 184 | 185 | return $this->doctrineCache->saveMultiple($validatedValues, $ttl); 186 | } 187 | 188 | /** 189 | * @param array|\Traversable $keys 190 | * @return bool 191 | * @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException 192 | */ 193 | public function deleteMultiple($keys) : bool 194 | { 195 | return $this->doctrineCache->deleteMultiple($this->filterValidateMultipleKeys($keys)); 196 | } 197 | 198 | /** 199 | * {@inheritDoc} 200 | */ 201 | public function has($key) : bool 202 | { 203 | $this->validateKey($key); 204 | return $this->doctrineCache->contains($key); 205 | } 206 | 207 | /** 208 | * @throws \Exception 209 | */ 210 | private function convertDateIntervalToInteger(\DateInterval $ttl) : int 211 | { 212 | // Timestamp has 2038 year limitation, but it's unlikely to set TTL that long. 213 | return (new \DateTime()) 214 | ->setTimestamp(0) 215 | ->add($ttl) 216 | ->getTimestamp(); 217 | } 218 | } 219 | --------------------------------------------------------------------------------