-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/psr16 support #791
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
00c8704
Add psr16 dependency
jbout cc11077
Fixed header use statement
jbout bdfdb83
Add keyvalue mock trait
jbout 089108a
Provide Psr16 cache
jbout 919834b
Wrap PSR 16 for current cache interface
jbout 3013a25
Version Bump
jbout 16c41f8
Added deprecation warning
jbout d5b1233
Hardcode persistence id to be able to remove oldcache later
jbout fc4fa03
Use new cache for purging
jbout 061126a
replace usage of old cache with new
jbout 265d31a
Added deprecation notice to cache
jbout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not create new classes in legacy namespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class provides the legacy interface and should be discouraged from being used, as is present for backward compatibility only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case it has to be marked as deprecated.
As we are going to get rid of legacy autoloader it's better to not create new classes in the legacy namespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce in the PR a new deprecated class and then configure it to be used by default does not seem to me like a good practice!
How difficult will be to change it's usage to not catch
common_cache_NotFoundException
? I found 14 catches of that exception in three extensions:tao
taoAct
taoMpArt (that one can be omitted)
I could help with cleaning that up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed all references to old cache from generis, so that we can move quicker when it comes to cleaning up old references, but I still don't want to make this feature backward incompatible, even if we could fix all other known extensions quickly