Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.06% |
33 / 34 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
| ItemPoolSimpleCacheAdapter | |
97.06% |
33 / 34 |
|
90.91% |
10 / 11 |
17 | |
0.00% |
0 / 1 |
| getItem | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getItems | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| hasItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteItems | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| saveDeferred | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| commit | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getCache | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| store | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\cache; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use Psr\Cache\CacheItemInterface; |
| 27 | use Psr\Cache\CacheItemPoolInterface; |
| 28 | use Psr\SimpleCache\CacheInterface; |
| 29 | use Throwable; |
| 30 | |
| 31 | class ItemPoolSimpleCacheAdapter extends ConfigurableService implements CacheItemPoolInterface |
| 32 | { |
| 33 | /** @var CacheItemInterface[] */ |
| 34 | private $deferred; |
| 35 | |
| 36 | public const OPTION_CACHE_SERVICE = 'cacheService'; |
| 37 | |
| 38 | /** |
| 39 | * @inheritdoc |
| 40 | */ |
| 41 | public function getItem($key) |
| 42 | { |
| 43 | $item = $this->getCache()->get($key); |
| 44 | |
| 45 | if ($item) { |
| 46 | $cacheItem = new CacheItem($key, true); |
| 47 | $cacheItem->set($item); |
| 48 | |
| 49 | return $cacheItem; |
| 50 | } |
| 51 | |
| 52 | return new CacheItem($key); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @inheritdoc |
| 57 | * |
| 58 | * @return CacheItemInterface[] |
| 59 | */ |
| 60 | public function getItems(array $keys = []) |
| 61 | { |
| 62 | $items = []; |
| 63 | |
| 64 | foreach ($keys as $key) { |
| 65 | $items[] = $this->getItem($key); |
| 66 | } |
| 67 | |
| 68 | return $items; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @inheritdoc |
| 73 | */ |
| 74 | public function hasItem($key) |
| 75 | { |
| 76 | return $this->getCache()->has($key); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @inheritdoc |
| 81 | */ |
| 82 | public function clear() |
| 83 | { |
| 84 | return $this->getCache()->clear(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @inheritdoc |
| 89 | */ |
| 90 | public function deleteItem($key) |
| 91 | { |
| 92 | return $this->getCache()->delete($key); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @inheritdoc |
| 97 | */ |
| 98 | public function deleteItems(array $keys) |
| 99 | { |
| 100 | return $this->getCache()->deleteMultiple($keys); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @inheritdoc |
| 105 | */ |
| 106 | public function save(CacheItemInterface $item) |
| 107 | { |
| 108 | return $this->store($item); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @inheritdoc |
| 113 | */ |
| 114 | public function saveDeferred(CacheItemInterface $item) |
| 115 | { |
| 116 | $this->deferred[$item->getKey()] = $item; |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @inheritdoc |
| 123 | */ |
| 124 | public function commit() |
| 125 | { |
| 126 | foreach ($this->deferred as $item) { |
| 127 | if (!$this->store($item)) { |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | private function getCache(): CacheInterface |
| 136 | { |
| 137 | if ($this->hasOption(self::OPTION_CACHE_SERVICE)) { |
| 138 | return $this->getOption(self::OPTION_CACHE_SERVICE); |
| 139 | } |
| 140 | |
| 141 | return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); |
| 142 | } |
| 143 | |
| 144 | private function store(CacheItemInterface $item): bool |
| 145 | { |
| 146 | try { |
| 147 | return $this->getCache()->set($item->getKey(), $item->get()); |
| 148 | } catch (Throwable $exception) { |
| 149 | $this->getLogger()->error( |
| 150 | sprintf( |
| 151 | 'Cache value for %s key has not been saved. %s', |
| 152 | $item->getKey(), |
| 153 | $exception->getMessage() |
| 154 | ) |
| 155 | ); |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | } |