在Laravel中使用Redis缓存雄辩的查询

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开箱即用地准备了一些驱动程序,其中一些是:

  1. File

    文件

  2. Redis

    雷迪斯

  3. 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。

  1. Add Redis configurations, and set the cache driver to use Redis (in the .env file):

    添加Redis配置,并将缓存驱动程序设置为使用Redis(在.env文件中):
Image for post
.env file
.env文件

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
app/Http/PostController.php file
app / Http / PostController.php文件

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
app/Http/PostController.php file
app / Http / PostController.php文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值