Put does not return the value. Instead use remember()
Laravel - Unable to cache the data
I want to cache some data in my Laravel Application. What I am doing is :
use Illuminate\Support\Facades\Cache;
$put = Cache::put('your_product', array(1,2,3,4), 3000 );
If I do var_dump I got false and Its not caching the data.
echo '<pre>';
var_dump( $put );
echo '</pre>';
Is there another way to use the cache in laravel? Or I am doing wrong?
BTW, I am using latest laravel version.
@Sinnbeck Ok. Actually I don't need the return value but Its should be catch the data. So Cache::put(). is wrong here?
@Shibbir It does cache the data? If you want to get it from cache, later you do
echo '<pre>';
var_dump( Cache::get('your_product') );
echo '</pre>';
@Sinnbeck Yes I did and return me empty array like this:
array(0) {
}
@Shibbir I cannot recreate that
Cache::put('your_product', array(1,2,3,4), 3000 );
dd(Cache::get('your_product)); //shows 1,2,3,4 as an array
@Sinnbeck I understand but this really strange for me. It's showing me null :(
BTW, it's but vue + laravel application. When I get the product from the ajax call I use that put function to cache the data.
@Shibbir Did you do them right after each other like in my example? Or somewhere completely different?
@Shibbir what cache driver are you using? For example, the array cache driver lasts for the current request, so will not be there in subsequent requests (irrespective of the number of seconds!)
@Sinnbeck I use that putfunction when I get a product from the databse using a ajax call. Now I use the get function in another ajax call to see that output.
@tykus its default cache drive: 'default' => env('CACHE_DRIVER', 'file'),
@Shibbir So let me ask another way. Does it work if you do it the way I have shown in the same request?
@Shibbir Look for CACHE_DRIVER in your .env
@tykus So you mean I can not get the cache data in another request?
note: it's my vue + laravel application.
@Sinnbeck Opps! There is not CACHE_DRIVER key in my .env file. is this the reasone?
@Shibbir Yes you can. But I am trying to find out where it goes wrong. If you say it also does not work in the same request, we need to fix that, while if its only in different ajax calls it fails, we will focus on that.
@Shibbir No that just means it uses the default (file).
@Sinnbeck Now, I think it's working after I added the key to the .env file, OH !
@Shibbir that doesn't make sense really; what value did you give for the CACHE_DRIVER key?
@tykus it's this CACHE_DRIVER=file
@tykus I don't understand why it was working before !!
@tykus Just tested again after delete that key from the .env file. Now it's not working. I need to add the key to the.env file.
@Shibbir We cannot say. But changing it from file to file does nothing. So something else I would assume. If you remove it, does it start breaking again?
@Sinnbeck Yes, Its breaking again.
Are you sure 'default' => env('CACHE_DRIVER', 'file'), comes from /config/cache.php ?
@Sinnbeck Yes. I am sure. I see this.
@Sinnbeck Check this please: https://prnt.sc/b-5G-erqgsnu
@Shibbir Ok try removing it from .env and do dd(config('cache.default'));
What does this give you?
@Sinnbeck Its showing me null.
@Shibbir Ok I really dont understand what is going on with your project. Are you caching your config or something? php artisan config:clear
I cannot recreate it. Anyways: make sure to set it in .env and you are good I suppose
Just making sure.. Its not that you have this in your env when you "remove it"?
CACHE_DRIVER=
@Sinnbeck I cleared all the cache though.
Please or to participate in this conversation.