We’ve all seen many websites that were so slow, making us crazy not being fast enough, and we ended up leaving them and not coming back to them anymore.
我们都已经看到许多网站是如此之慢,以至于我们疯狂得不够快,我们最终离开了他们,不再回到他们身边。
Imagine having a website with a lot of requests to your database sooner or later you will have to dedicate a lot of computing power for responding DB requests, and you will have to pay tons of money for it.
想象一下,有一个网站迟早会向您的数据库发送大量请求,您将不得不投入大量的计算能力来响应数据库请求,并且您将为此付出大量的金钱。
那么,有什么方法可以减少具有相同数量请求的数据库查询? (So is there any way to lessen the DB queries while having the same number of requests?)
Yes, caching can help with this issue.
是的, 缓存可以帮助解决此问题。
If you’re using the Laravel framework as your backend stack for your website/web application, this is quite easy to configure. Laravel has some drivers ready out of the box, some of them are:
如果您将Laravel框架用作网站/ Web应用程序的后端堆栈,则配置起来非常容易。 Laravel开箱即用地准备了一些驱动程序,其中一些是:
File
文件
Redis
雷迪斯
Memcached
记忆快取
File driver is a good choice, but if you want to do something on a bigger scale, and much faster, you should probably use Redis or Memcached.
文件驱动程序是一个不错的选择,但是如果您想以更大的速度更快地执行某项操作,则可能应该使用Redis或Memcached。
在本文中,我们将通过两个简单的步骤了解缓存如何与Redis一起工作: (In this article, we will see how does caching work with Redis in 2 simple steps:)
I assume you have both Laravel and Redis up and running on your local environment.
我假设您已经在本地环境中同时运行了Laravel和Redis。
- Add Redis configurations, and set the cache driver to use Redis (in the .env file): 添加Redis配置,并将缓存驱动程序设置为使用Redis(在.env文件中):
![Image for post](https://miro.medium.com/max/9999/1*etP5UpNkqXp-cqVT0RwmLw.png)
As you know, Redis is an in-memory key-value database. This is how you can add a key and value pair to it:
如您所知,Redis是一个内存中键值数据库。 这是您可以向其添加键和值对的方法:
Cache::put($key, $value, $ttl);
Cache::put('foo', 'bar', 600);
This will save ‘foo’ with the value ‘bar’ that will remain for 10 minutes (600 seconds). To get the value of the cache, we can use:
这会将'foo'保存为值'bar',该值将保留10分钟(600秒)。 要获取缓存的值,我们可以使用:
$foo = Cache::get('foo');
This is the way we retrieve posts from the database.
这是我们从数据库检索帖子的方式。
![Image for post](https://miro.medium.com/max/9999/1*662Nac24BWat2nNw3ucSmg.png)
2. We can cache these posts after the first request then we won’t query the database anymore, because the data will come from Redis:
2.我们可以在第一个请求之后缓存这些帖子,然后我们将不再查询数据库,因为数据将来自Redis:
![Image for post](https://miro.medium.com/max/9999/1*LZuJp3TT4_cVzf4k_t0SRw.png)
This will check if the ‘posts’ key exists in the cache, if so, it will use cache otherwise, it will query the database and then saves the data to cache for the next requests for 33600 seconds.
这将检查高速缓存中是否存在“ posts”键,如果存在,则将使用高速缓存,否则将查询数据库,然后将数据保存到高速缓存中以等待下一个请求33600秒。
Keep in mind that when you create, update, delete a post, you have to delete (forget) the cache with that key in this case ‘posts’ otherwise, it will show you the old data, which is cached until the TTL time ends.
请记住,在创建,更新,删除帖子时,您必须使用该键删除(忘记)缓存,在这种情况下为“帖子”,否则,它将向您显示旧数据,该数据一直缓存到TTL时间结束 。
翻译自: https://medium.com/dev-genius/caching-eloquent-queries-with-redis-in-laravel-b8872d8eeb18