-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from oat-sa/feature/psr16-support
Feature/psr16 support
- Loading branch information
Showing
27 changed files
with
614 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @package generis | ||
* @deprecated Please use oat\oatbox\cache\SimpleCache | ||
*/ | ||
class common_cache_KeyValueCache extends ConfigurableService implements common_cache_Cache | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2020 (original work) Open Assessment Technologies SA | ||
* | ||
*/ | ||
|
||
use oat\oatbox\service\ConfigurableService; | ||
use Psr\SimpleCache\CacheInterface; | ||
use oat\oatbox\cache\SimpleCache; | ||
|
||
/** | ||
* Wrap the PSR simple cache implementation into the legacy interface | ||
* @deprecated Please use oat\oatbox\cache\SimpleCache | ||
*/ | ||
class common_cache_PsrWrapperCache extends ConfigurableService implements common_cache_Cache | ||
{ | ||
|
||
/** | ||
* puts "something" into the cache, | ||
* * If this is an object and implements Serializable, | ||
* * we use the serial provided by the object | ||
* * else a serial must be provided | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @param mixed $mixed | ||
* @param null $serial | ||
* @param null $ttl | ||
* @return bool | ||
* @throws common_exception_Error | ||
*/ | ||
public function put($mixed, $serial = null, $ttl = null) | ||
{ | ||
if ($mixed instanceof common_Serializable) { | ||
if (!is_null($serial) && $serial != $mixed->getSerial()) { | ||
throw new common_exception_Error('Serial mismatch for Serializable ' . $mixed->getSerial()); | ||
} | ||
$serial = $mixed->getSerial(); | ||
} | ||
|
||
return $this->getPsrSimpleCache()->set($serial, $mixed, $ttl); | ||
} | ||
|
||
/** | ||
* gets the entry associted to the serial | ||
* | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @param string serial | ||
* @return common_Serializable | ||
* @throws common_cache_NotFoundException | ||
*/ | ||
public function get($serial) | ||
{ | ||
$returnValue = $this->getPsrSimpleCache()->get($serial, false); | ||
if ($returnValue === false && !$this->getPsrSimpleCache()->has($serial)) { | ||
$msg = "No cache entry found for '" . $serial . "'."; | ||
throw new common_cache_NotFoundException($msg); | ||
} | ||
return $returnValue; | ||
} | ||
|
||
/** | ||
* test whenever an entry associated to the serial exists | ||
* | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @param string serial | ||
* @return boolean | ||
*/ | ||
public function has($serial) | ||
{ | ||
return $this->getPsrSimpleCache()->has($serial); | ||
} | ||
|
||
/** | ||
* removes an entry from the cache | ||
* | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @param string serial | ||
* @return mixed | ||
*/ | ||
public function remove($serial) | ||
{ | ||
return $this->getPsrSimpleCache()->delete($serial); | ||
} | ||
|
||
/** | ||
* empties the cache | ||
* | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @return mixed | ||
*/ | ||
public function purge() | ||
{ | ||
return $this->getPsrSimpleCache()->clear(); | ||
} | ||
|
||
protected function getPsrSimpleCache() : CacheInterface | ||
{ | ||
return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,20 +22,7 @@ | |
|
||
/** | ||
* basic interface a cache implementation has to implement | ||
* | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @package generis | ||
*/ | ||
|
||
|
||
/** | ||
* basic interface a cache implementation has to implement | ||
* | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @package generis | ||
* @deprecated please use oat\oatbox\cache\SimpleCache | ||
*/ | ||
interface common_cache_Cache | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2020 (original work) Open Assessment Technologies SA | ||
* | ||
*/ | ||
|
||
namespace oat\oatbox\cache; | ||
|
||
use oat\oatbox\service\ConfigurableService; | ||
use common_persistence_KeyValuePersistence; | ||
use oat\generis\persistence\PersistenceManager; | ||
use common_exception_NotImplemented; | ||
use DateInterval; | ||
use DateTimeImmutable; | ||
|
||
/** | ||
* Caches data in a key-value store | ||
* | ||
* @access public | ||
* @author Jerome Bogaerts, <[email protected]> | ||
* @package generis | ||
*/ | ||
class KeyValueCache extends ConfigurableService implements SimpleCache | ||
{ | ||
use MultipleCacheTrait; | ||
|
||
const OPTION_PERSISTENCE = 'persistence'; | ||
|
||
/** @var common_persistence_KeyValuePersistence */ | ||
private $persistence; | ||
|
||
public function set($key, $value, $ttl = null) | ||
{ | ||
if ($ttl instanceof DateInterval) { | ||
$ttl = $this->dateIntervalToSeconds($ttl); | ||
} | ||
return $this->getPersistence()->set($key, $value, $ttl); | ||
} | ||
|
||
public function clear() | ||
{ | ||
try { | ||
return $this->getPersistence()->purge(); | ||
} catch (common_exception_NotImplemented $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function delete($key) | ||
{ | ||
return $this->getPersistence()->del($key); | ||
} | ||
|
||
public function get($key, $default = null) | ||
{ | ||
$returnValue = $this->getPersistence()->get($key); | ||
// persistence can return false on a value of false or a not found key | ||
return ($returnValue !== false || $this->has($key)) | ||
? $returnValue | ||
: $default; | ||
} | ||
|
||
public function has($key) | ||
{ | ||
return $this->getPersistence()->exists($key); | ||
} | ||
|
||
protected function dateIntervalToSeconds(DateInterval $dateInterval): int | ||
{ | ||
$reference = new DateTimeImmutable; | ||
$endTime = $reference->add($dateInterval); | ||
return $endTime->getTimestamp() - $reference->getTimestamp(); | ||
} | ||
|
||
/** | ||
* @return common_persistence_KeyValuePersistence | ||
*/ | ||
protected function getPersistence() | ||
{ | ||
if (is_null($this->persistence)) { | ||
$this->persistence = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID)->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); | ||
} | ||
return $this->persistence; | ||
} | ||
} |
Oops, something went wrong.