了解Laravel缓存:缓存外观和Redis [英] Understanding Laravel caching: Cache facade and Redis

查看:68
本文介绍了了解Laravel缓存:缓存外观和Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手.我已经完成了有关该主题的研究,但似乎找不到找到可以解决问题的答案.

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.

我知道Laravel的默认缓存驱动器设置为 file ,可以更改.它还有一些手工缓存命令,例如:

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)即使Laravel具有一些自动处理某些缓存的内置命令和功能(无法确切了解哪些部分),我仍然必须在查询结果上手动使用Cache Facade ,对吧?

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?

它不会自动执行,如果要更改某些内容,我只需要使用Cache门面,对吗?

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)当使用Redis(带有 predis 包)作为缓存驱动程序时,我是否需要在以下位置使用Cache外观或Redis外观,或两者都使用?某些情况下?还是可以简单地在Laravel和服务器(Forge上的Digital Ocean Droplet)中启用Redis,而不做其他事情?

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?

我已经在Laravel文档中使用Cache外观看到了类似的东西:

I've seen something like this in the Laravel docs, using the Cache facade:

Cache::store('redis')->put('bar', 'baz', 600);

我也看过使用Redis外观的教程:

I've also seen a tutorial using the Redis facade:

use Illuminate\Support\Facades\Redis; 

Route::get('/', function () { 
     $visits = Redis::incr('visits'); 
     return $visits; 
});

我不知道该怎么做.

推荐答案

公用:

它们是所有缓存.

它们都用来降低时间成本.

它属于应用.我们称之为应用程序缓存.

php artisan config:cache
php artisan route:cache

这两个命令是缓存路由和配置.

These two commands is caching routes and configurations.

它们始终存储在bootstrap/cache/

运行

php artisan config:clear
php artisan route:clear

仅清除bootstrap/cache/中的目录和文件.

Only clear the directories and files in bootstrap/cache/.

它们是静态.因此,只有在更改它们时,它们才会被更改.

They are static. So they are only changed when you changing them.

如果更改它们,则需要手动clearcache.

If you change them, you need to clear and cache them manually.

在您配置了这些路由和配置之后.

After you cahce these routes and configurations.

Laravel 无需再次从文件读取配置和路由,这花费了IO时间.

Laravel doesn't need to read the configurations and routes from file again which took IO time cost.

文件系统缓存和Redis缓存也都是缓存.

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.

文件系统路径:如果您正在使用文件系统驱动程序.它们存储在storage/framework/cache/

Reids PATH:数据通过键值存储在redis中.

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.

您可以使用缓存来存储它们,然后在下次对该API发出另一个请求时使用. 它只是从缓存中获取数据. 如下所示:

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();});

第一个请求从数据库中获取帖子的数据,然后将这些数据存储在30秒后过期的缓存(Reids或Filesystem)中.

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.

还有一个:

use Illuminate\Support\Facades\Redis; 

Route::get('/', function () { 
     $visits = Redis::incr('visits'); 
     return $visits; 
});

表示当人们请求localhost:8000/时,redis中的用户访问次数增加了(不需要存储在数据库中,这会花费更多时间), 下次,当请求搜索访问计数时,可以很快在redis中找到它.

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:这里使用Redis Facade,然后将数据存储在redis中.

如果您将redis用作缓存驱动程序,则Cache::remember()也会在redis中存储数据.

If you are using redis as cache driver, Cache::remember() will store datas in redis too.

但是,使用Redis Facade,您可以使用许多Redis方法.

However, using Redis Facade, you can use many redis methods.

哪个更好?

我认为redisfilesystem好.

  1. 因为redis将数据存储在内存中,而文件系统存储在磁盘中. 从内存中读取数据比从磁盘中读取数据更快.

  1. Because redis store datas in memory, and filesystem store in disk. Read data from memory is faster than disk.

操作数据比文件系统更容易.例如,Redis支持清除特定标签的所有缓存,但是文件系统无法[因为文件系统通过加密密钥的名称存储缓存数据].

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.

老实说,还可以选择其他驱动程序,例如mongodb.

Honestly, there are other drivers can be choosed, like mongodb.

顺便说一句,我的英语不是很好,希望你能理解.

By the way, my English is not very good, hope u understand.

这篇关于了解Laravel缓存:缓存外观和Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆