SimpleCacheBridge::setMultiple() not compatible with NamespacedCachePool? #139
Comments
As a related question: Why does $pool = new NamespacedCachePool(
new ArrayCachePool(),
'my_prefix'
);
$data = [
'key1' => 'val1',
'key2' => 'val2',
];
foreach ($data as $key =>$val) {
$item = $pool->getItem($key);
$item->set($val);
$pool->save($item);
}
$identityTest = $pool->getItems(array_keys($data));
// Expected: $identityTest === $data
// Actual: $identityTest !== $data (Keys in $identityTest contain `|my_prefix|` whereas keys in $data do not.) Correcting this could be another possible fix for the current issue, but seems like much more of a breaking change. |
As a workaround (and in case it helps anyone else), I managed to override the <?php
/**
* Fixes https://github.com/php-cache/issues/issues/139
*
* @TODO: Remove this class when php-cache/issues#139 is fixed.
*/
namespace App\Lib;
use Cache\Hierarchy\HierarchicalPoolInterface;
use Cache\Namespaced\NamespacedCachePool as NamespacedCachePoolOrig;
use \Traversable;
/**
* \App\Lib\NamespacedCachePool
*/
class NamespacedCachePool extends NamespacedCachePoolOrig
{
/**
* Store the calculated prefix string.
*
* @var string
*/
protected $prefix;
/**
* Have to override the constructor to get access to $namespace because
* ::$namespace and ::prefixValue() are both declared as `private` in
* the parent class.
*
* @param HierarchicalPoolInterface $cachePool
* @param string $namespace
*/
public function __construct(HierarchicalPoolInterface $cachePool, $namespace)
{
parent::__construct($cachePool, $namespace);
$this->prefix = HierarchicalPoolInterface::HIERARCHY_SEPARATOR
. $namespace
. HierarchicalPoolInterface::HIERARCHY_SEPARATOR;
}
/**
* Overrides the normal ::getItems() to properly strip the namespace prefix
* from the returned indices.
*
* @param array $keys
* @return array Sets of [un-prefixed-keys => values].
*/
public function getItems(array $keys = [])
{
return iterator_to_array($this->yieldWithoutPrefix(parent::getItems($keys)));
}
/**
* Generator that does the work of stripping the namespace prefix from keys in $items.
*
* @param array|\Traversable $items The results returned from the underlying cache store.
* @return array Same results with key names stripped of the namespace prefix.
*/
protected function yieldWithoutPrefix($items)
{
foreach ($items as $key => $val) {
yield str_replace($this->prefix, '', $key) => $val;
}
}
} |
Thanks @beporter ! Just chiming in to report that this issue is also present in the |
I will take a look on this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've encountered an issue trying to combine a
NamespacedCachePool
with theSimpleCacheBridge
.Environment:
Example php file to reproduce:
Error:
The issue seems to be here:
In
$arrayValues[$key]
, the$key
being used includes the prefix that was auto-added byNamespacedCachePool:: prefixValue($key)
, but$arrayValues
is provided by the end user and is therefore not prefixed yet (nor should it be.) One possible fix might be to somehow strip the prefix (if present) from$key
before attempting to access$arrayValues[$key]
.The text was updated successfully, but these errors were encountered: