Laravel - 使用哪个缓存驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45677563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Which cache driver to use?
提问by Sensoray
This is my first time dealing with caching, and even though I looked through the laravel docs and other various sites for instructions of how to set it up, I'm still at a bit of a loss as which one to use and what the different cache drivers do.
这是我第一次处理缓存,尽管我查看了 laravel 文档和其他各种站点以获取有关如何设置它的说明,但我仍然对使用哪个以及有什么不同感到有些茫然缓存驱动做。
My current scenario is that I have a scheduling system where you can create pdfs of the current week of classes. They can also choose a date in the future and make a pdf of that week as well. This is a frontend feature, so anyone who visits the site would be able to use it. There are many classes and variations of patterns that the classes can have, so the query would have a lot of records to look through. Which driver would be best out of the supported cache drivers?? (apc, array, database, file, memcached & redis)
我目前的情况是我有一个日程安排系统,您可以在其中创建当前一周课程的 pdf。他们还可以选择未来的日期并制作该周的 pdf。这是一项前端功能,因此任何访问该站点的人都可以使用它。有许多类和类可以具有的模式变体,因此查询将有大量记录要查看。在支持的缓存驱动程序中哪个驱动程序最好?(apc、数组、数据库、文件、memcached 和 redis)
Brownie Points
印象分
I'd like to get an understanding of which to use and why so I can make the best decisions for future projects. So what does each do/when would it be best to use them?? -- Doesn't need to be answered to get accepted answer, but I'd really like to know.
我想了解使用哪个以及为什么使用,以便我可以为未来的项目做出最佳决策。那么每个人做什么/什么时候最好使用它们?- 不需要回答即可获得接受的答案,但我真的很想知道。
Thanks!
谢谢!
回答by HubertNNN
When it comes to using cache in Laravel you have 3 possible "families" that you should concider:
在 Laravel 中使用缓存时,您应该考虑 3 个可能的“家庭”:
Temporary/Debug
- array
Always available
- file
- database
- apc (I would not trust this one since PHP7)
Dedicated
- redis
- memcached
临时/调试
- 大批
始终可用
- 文件
- 数据库
- apc(自 PHP7 起我就不相信这个了)
投入的
- Redis
- 内存缓存
Since you can easily replace the cache drivers you dont need to pick one based on your use case, but more based on your server needs/load and possibilities.
由于您可以轻松替换缓存驱动程序,因此您不需要根据您的用例选择一个,而是更多地基于您的服务器需求/负载和可能性。
For example on your development machine I suggest using file, since this way you wont need any extra software clogging your PC plus you gain the ability to quickily clear the cache even if you do something really bad like breaking the artisan command. All you need to do is delete the storage/framework folder and you have a fresh instance again (make sure to regenerate the .gitignore files from your repository after that)
例如,在您的开发机器上,我建议使用文件,因为这样您就不需要任何额外的软件堵塞您的 PC,而且即使您做了一些非常糟糕的事情,例如破坏了 artisan 命令,您也可以获得快速清除缓存的能力。您需要做的就是删除 storage/framework 文件夹,然后您再次拥有一个新实例(确保在此之后从您的存储库中重新生成 .gitignore 文件)
For your main server you have to think about your possibilities. If you have one of those free hosting websites you almost certainly won't be able to install any new software, so you can consider using file or database. Even though database will probably be faster than file, it is in most cases the weakest point of your website, and trying to push even more data into that bottleneck is not a good idea, that is why I would suggest against using it, and instead stick to files.
对于您的主服务器,您必须考虑您的可能性。如果您拥有这些免费托管网站之一,您几乎肯定无法安装任何新软件,因此您可以考虑使用文件或数据库。尽管数据库可能比文件快,但在大多数情况下,它是您网站的最弱点,并且尝试将更多数据推入该瓶颈并不是一个好主意,这就是为什么我建议不要使用它,而是坚持文件。
If you have a dedicated server, than you should definately pick memcached or redis. Which one of the two? It depends on many factors, and you can find a lot of comparations online, just look for one. I personally prefer redis becouse of its ability to persist data, but either one is a good solution.
如果您有专用服务器,那么您肯定应该选择 memcached 或 redis。两者中的哪一个?这取决于很多因素,你可以在网上找到很多比较,找一个就行了。我个人更喜欢 redis,因为它具有持久化数据的能力,但任何一个都是很好的解决方案。
回答by Paras
Typically you would use cache for frequent queries (when you need to perform a particular read op frequently but write not as frequently). If this isn't the case, you would generally fallback to DB.
通常,您将缓存用于频繁的查询(当您需要频繁地执行特定的读取操作但不那么频繁地写入时)。如果不是这种情况,您通常会回退到 DB。
Looking at your use case, it sounds like it's a batch job that would run once a week. So, it's an infrequent task and the data would be fresh every week. So what exactly do you hope to achieve by caching?
看看您的用例,这听起来像是一个每周运行一次的批处理作业。因此,这是一项不常见的任务,数据每周都会更新。那么你到底希望通过缓存实现什么呢?