Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
15 / 21 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| KeyValueCache | |
71.43% |
15 / 21 |
|
57.14% |
4 / 7 |
15.36 | |
0.00% |
0 / 1 |
| set | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| clear | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
3.19 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dateIntervalToSeconds | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getPersistence | |
100.00% |
6 / 6 |
|
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 | |
| 22 | namespace oat\oatbox\cache; |
| 23 | |
| 24 | use common_exception_NotImplemented; |
| 25 | use common_persistence_KeyValuePersistence; |
| 26 | use DateInterval; |
| 27 | use DateTimeImmutable; |
| 28 | use oat\generis\persistence\PersistenceManager; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | |
| 31 | /** |
| 32 | * Caches data in a key-value store |
| 33 | * |
| 34 | * @access public |
| 35 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 36 | * @package generis |
| 37 | */ |
| 38 | class KeyValueCache extends ConfigurableService implements SimpleCache |
| 39 | { |
| 40 | use MultipleCacheTrait; |
| 41 | |
| 42 | public const OPTION_PERSISTENCE = 'persistence'; |
| 43 | |
| 44 | /** @var common_persistence_KeyValuePersistence */ |
| 45 | private $persistence; |
| 46 | |
| 47 | public function set($key, $value, $ttl = null) |
| 48 | { |
| 49 | if ($ttl instanceof DateInterval) { |
| 50 | $ttl = $this->dateIntervalToSeconds($ttl); |
| 51 | } |
| 52 | return $this->getPersistence()->set($key, $value, $ttl); |
| 53 | } |
| 54 | |
| 55 | public function clear() |
| 56 | { |
| 57 | try { |
| 58 | return $this->getPersistence()->purge(); |
| 59 | } catch (common_exception_NotImplemented $e) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function delete($key) |
| 65 | { |
| 66 | return $this->getPersistence()->del($key); |
| 67 | } |
| 68 | |
| 69 | public function get($key, $default = null) |
| 70 | { |
| 71 | $returnValue = $this->getPersistence()->get($key); |
| 72 | // persistence can return false on a value of false or a not found key |
| 73 | return ($returnValue !== false || $this->has($key)) |
| 74 | ? $returnValue |
| 75 | : $default; |
| 76 | } |
| 77 | |
| 78 | public function has($key) |
| 79 | { |
| 80 | return $this->getPersistence()->exists($key); |
| 81 | } |
| 82 | |
| 83 | protected function dateIntervalToSeconds(DateInterval $dateInterval): int |
| 84 | { |
| 85 | $reference = new DateTimeImmutable(); |
| 86 | $endTime = $reference->add($dateInterval); |
| 87 | return $endTime->getTimestamp() - $reference->getTimestamp(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return common_persistence_KeyValuePersistence |
| 92 | */ |
| 93 | protected function getPersistence() |
| 94 | { |
| 95 | if (is_null($this->persistence)) { |
| 96 | $this->persistence = $this |
| 97 | ->getServiceLocator() |
| 98 | ->get(PersistenceManager::SERVICE_ID) |
| 99 | ->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); |
| 100 | } |
| 101 | return $this->persistence; |
| 102 | } |
| 103 | } |