问题
I'm new to Laravel. I've done research on the topic, but I can't seem to find an answer that clears things up for me.
I know Laravel's default cache driver is set to file, which I can change. It also has some artisan cache commands, such as:
php artisan config:cache
php artisan route:cache
1) Even if Laravel has some built-in commands and features that automatically handle some cache (didn't understand exactly what parts), I still have to manually use the Cache facade on query results, right?
It doesn't do it automatically, and I only have to use the Cache facade if I want to change things or something, right?
Here's a random example from a tutorial:
$posts = Cache::remember('index.posts', 30, function()
{return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();});
2) When using Redis (with the predis package) as the cache driver, do I need to use the Cache facade, or the Redis facade, or both in some cases? Or can I simply enable Redis in Laravel and server (Digital Ocean droplet on Forge) and don't do anything else?
I've seen something like this in the Laravel docs, using the Cache facade:
Cache::store('redis')->put('bar', 'baz', 600);
I've also seen a tutorial using the Redis facade:
use Illuminate\Support\Facades\Redis;
Route::get('/', function () {
$visits = Redis::incr('visits');
return $visits;
});
I can't figure out what's the proper thing to do.
回答1:
Common:
They are all cache.
They are all used to reduce time cost.
Differenet:
Configurations And Routes:
It belongs to application. We call it Application Cache.
php artisan config:cache
php artisan route:cache
These two commands is caching routes and configurations.
PATH:
They are always stored in
bootstrap/cache/
Running
php artisan config:clear
php artisan route:clear
Only clear the directories and files in bootstrap/cache/
.
They are static. So they are only changed when you changing them.
Benifit
If you change them, you need to clear
and cache
them manually.
After you cahce these routes and configurations.
Laravel doesn't need to read the configurations and routes from file again which took IO time cost.
Filesystem Cache And Redis Cache:
Filesystem cache and Redis cache are both cache too.
However, they use different driver to store the datas, means where you store the caching datas.
Filesystem PATH: If you are using filesystem driver. they are stored in
storage/framework/cache/
Reids PATH: Datas store in
redis
by key-value.
When do you use them?
When you found that there are many request to this code for getting datas. And these datas is not changed so fast.
You can use cache to store them, and then, next time another request to this api. it just take datas from the cache. like below:
$posts = Cache::remember('index.posts', 30, function()
{return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();});
The first request get the posts' datas from database, and then store the these datas in cache(Reids or Filesystem) expired after 30 sec.
Next request get posts' datas only through cache. They don't need to search in databases again.
And this one:
use Illuminate\Support\Facades\Redis;
Route::get('/', function () {
$visits = Redis::incr('visits');
return $visits;
});
Means when people request localhost:8000/
, users' visit count increasing in redis
(don't need to store in database, it cost more time),
next time, when request to search visit count, it can be found in redis
very quickly.
PS: Here use Redis Facade, then the datas are stored in
redis
.If you are using redis as cache driver,
Cache::remember()
will store datas in redis too.However, using Redis Facade, you can use many redis methods.
Which is better?
I think redis
is better than filesystem
.
Because
redis
store datas in memory, and filesystem store in disk. Read data from memory is faster than disk.Operate datas in Redis is easier than Filesystem. For example Redis support clear all cache for a specific tag, but filesystem cannot[Because filesystem store cache datas by encrypted key's name].
For distributed server, filesystem cache is a bad idea. Lower cache hit ratio.
Honestly, there are other drivers can be choosed, like mongodb
.
By the way, my English is not very good, hope u understand.
来源:https://stackoverflow.com/questions/58890788/understanding-laravel-caching-cache-facade-and-redis