colinmollenhour / Cm_Cache_Backend_Redis

A Zend_Cache backend for Redis with full support for tags (works great with Magento)
Other
390 stars 142 forks source link

`getMetadatas` returns `false` #170

Closed convenient closed 1 year ago

convenient commented 1 year ago

Hello

I have been trying to debug something using getMetadata but no matter what I do it seems to always return false.

Can you see that I am doing anything wrong here?

Test script

  1. Save to cache
  2. Read from cache, to verify save worked
  3. Try and get metadatas expect to see tags and lifetime, seen nothing
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

/** @var \Magento\Framework\App\CacheInterface $cache */
$cache = $obj->get(\Magento\Framework\App\CacheInterface::class);
/** @var \Magento\Framework\Cache\Core $lowLevelCache */
$lowLevelCache = $cache->getFrontend()->getLowLevelFrontend();

$key = 'SomeSillyKey';

$result = $cache->save('cache entry value ' . time(), $key, ['Fun_Tags', 'Here'], 123);
echo "save result: " . var_export($result, true) . PHP_EOL;

$readResult = $cache->load($key);
echo "read result: " . $readResult . PHP_EOL;

/** @var Cm_Cache_Backend_Redis $backend */
$backend = $lowLevelCache->getBackend();
echo "backend class: " . get_class($backend) . PHP_EOL;

echo "metadatas result from lowlevelcache: " . var_export($lowLevelCache->getMetadatas($key), true) . PHP_EOL;
echo "metadatas result from backend: " . var_export($backend->getMetadatas($key), true) . PHP_EOL;

Output

save result: true
read result: cache entry value 1668692479
backend class: Cm_Cache_Backend_Redis
metadatas result from lowlevelcache: false
metadatas result from backend: false
convenient commented 1 year ago

I do apologise, seems i needed to strtoupper the key 🤔

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

/** @var \Magento\Framework\App\CacheInterface $cache */
$cache = $obj->get(\Magento\Framework\App\CacheInterface::class);
/** @var \Magento\Framework\Cache\Core $lowLevelCache */
$lowLevelCache = $cache->getFrontend()->getLowLevelFrontend();

$key = strtoupper('SomeSillyKey');

$result = $cache->save('cache entry value ' . time(), $key, ['Fun_Tags', 'Here'], 123);
echo "save result: " . var_export($result, true) . PHP_EOL;

$readResult = $cache->load($key);
echo "read result: " . $readResult . PHP_EOL;

/** @var Cm_Cache_Backend_Redis $backend */
$backend = $lowLevelCache->getBackend();
echo "backend class: " . get_class($backend) . PHP_EOL;

echo "metadatas result from lowlevelcache: " . var_export($lowLevelCache->getMetadatas($key), true) . PHP_EOL;
echo "metadatas result from backend: " . var_export($backend->getMetadatas($key), true) . PHP_EOL;
save result: true
read result: cache entry value 1668695167
backend class: Cm_Cache_Backend_Redis
metadatas result from lowlevelcache: array (
  'expire' => 1668695290,
  'tags' => 
  array (
    0 => 'd1e_FUN_TAGS',
    1 => 'd1e_HERE',
    2 => 'd1e_MAGE',
  ),
  'mtime' => '1668695167',
)
metadatas result from backend: false
colinmollenhour commented 1 year ago

I think the Magento wrapper uppercases the keys so when you bypass the Magento wrapper you have to uppercase the keys yourself.