I have memcached selected as my cache driver. However , ran into a weird issue.
Once I am doing:
Cache::put('name','John',15);
In the very next line if I give
var_dump(Cache::get('name'))
it shows me :
bool(false)
Couldn’t understand what’s going wrong here. I have memcached running on port 11211 on my localhost which I can telnet.
Also phpinfo() shows php-memcached library is installed.
My config/cache.php file reads:
'default' => env('CACHE_DRIVER', 'memcached'),
'stores' => [
'apc' => [
'driver' => 'apc',
],
'array' => [
'driver' => 'array',
],
'database' => [
'driver' => 'database',
'table' => env('CACHE_DATABASE_TABLE', 'cache'),
'connection' => env('CACHE_DATABASE_CONNECTION', null),
],
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'), 'port' => env('MEMCACHED_PORT', 11211), 'weight' => 100,
],
],
],
'redis' => [
'driver' => 'redis',
'connection' => env('CACHE_REDIS_CONNECTION', 'default'),
],
],
'prefix' => env('CACHE_PREFIX', 'laravel'),
Please help.
3
Answers
Finally after an entire day of Googling , I found the solution.
It seems I had to add the following line to bootstrap/app.php :
Also , please note that if you are running your application inside a VM/docker container , you need to provide the Host ip .
You have a typo. The method to set a value in the cache is
put()
, but you usedget()
twice. Try this:i spent 3 hours figuring out why my code was not working and can’t get the data from the cache and finally i figured out why :
Cache::put('name','John',15);
and when you do that You put that on the Cache for Only 15s or maybe 15ms depends on your configuration on the code.
You have to also check if you have permission on the storage Folder :
sudo CHOWN -R www:data:www:data storage
You can verify that you inserted the data on cache manually and you will see that you already cached that data but the expire date ttl is expired.
Good luck