Problem setting/reading an item
Configuration:
PhpFastCache version: 6.1.1
PHP version: 5.6
Operating system: Windows 10
Issue description:
Hi, I would like to use PHPFastCache with PHP Slim and I created the FC instance in Slim config, this way
// PHP FastCache $container['Cache'] = function($c) {
$cacheConfig = array(
"path" => dirname(__FILE__) . "/files",
);
\phpFastCache\CacheManager::setDefaultConfig($cacheConfig);
$cache = \phpFastCache\CacheManager::getInstance('files');
//$cache->clear();
return $cache;
};
Then i check if an key exist and return its value or if does not exist i saved in the cache
$app->post('/printCertificate', function(Request $request, Response $response) { try { $data = $request->getParsedBody(); $token = $data['token']; $certificateID = $data['certificateID'];
// Chequea si este certificado esta en el cache
$key = "pc_$certificateID";
echo $key;
$cachedString = $this->Cache->getItem($key);
var_dump($cachedString->get());
if (is_null($cachedString->get())) {
echo "No consegui la data, colocar en el cache<br>" . PHP_EOL;
$pdfPath = $this->WsPhoenix->printCertificate($token, $certificateID);
$result = ResponseHelper::buildOKResponse($pdfPath);
$cachedString->set($result)->expiresAfter(5);
$this->Cache->save($cachedString);
//var_dump($cachedString->get());
die();
} else {
echo "consegui la data<br>" . PHP_EOL;
echo "saved data";
$result = $cachedString->get()[0];
}
} catch (Exception $ex) {
$result = ResponseHelper::buildErrorResponse($ex->getMessage());
}
return ResponseHelper::buildJSONResponse($response, $result);
});
And it always found cachedString as null. I checked in the files directory and the value is there...
When I re-run my method it reports
Warning: rmdir(C:\xampp\htdocs\PSS\v1\files\Auto\Files\14\77): Resource temporarily unavailable in C:\xampp\htdocs\PSS\vendor\phpfastcache\phpfastcache\src\phpFastCache\Drivers\Files\Driver.php on line 123
Any suggestion...
regards, Yamil
Sorry, my mistake ... Thanks!
Hello,
$cachedString->set($result)->expiresAfter(5);
Are you aware that this put in cache for 5 seconds only ?
Secondly:
if (is_null($cachedString->get())) {
Use isHit() instead this will improve performance.
Finally,
The error you mentioned
Warning: rmdir(C:\xampp\htdocs\PSS\v1\files\Auto\Files\14\77): Resource temporarily unavailable in
Just looks like a chmod/chown issue.
Cheers, Georges.
And globally don't use is_null() but $var !== null.
This is faster since this is parsed by the lexer, not the runtime compiler.
Cheers, Georges