会话 VS 文件 VS Memcache 用于 PHP 中的缓存? [英] Session VS File VS Memcache for a Cache in PHP?

查看:30
本文介绍了会话 VS 文件 VS Memcache 用于 PHP 中的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个社交网络

  • 用户表大约有 60,000 行

  • The users table is around 60,000 rows

朋友表大约有100万行(用于确定谁是您的
朋友)

The friends table is around 1 million rows (used to determine who is your
friend)

我想做一个朋友提要、墙,不管你喜欢叫它什么,它会显示用户状态帖子(推特类型帖子)之类的东西,它会显示一些不同的项目,但一开始它只是好友状态,可能还有博文.

I am wanting to do a friend feed, wall, whatever you like to call it, it will show things like user status post (twitter type posts), it will show a few different items but for the start it will just be friend status and maybe blog post.

基本上,您只会看到使用您好友列表中的用户 ID 发布的内容.

Basicly you will only see content published with a user ID that is in your friend list.

我一直在努力想出最好的方法,但还没有走多远,但这是我的最新想法.

I have been trying to come up with the best way and haven't gotten very far but here is my latest idea.

目前要构建此提要,我必须

Currently to build this feed, I have to

  1. 获取好友ID列表大朋友桌

  1. Get the list of friend ID's on the large friend table

从好友处获取流数据来自上述结果的 id

Get the stream data from the friend ids from the above result

JOIN 用户表以获取发布商图片网址和用户名

JOIN the user table to get the publishers picture URL and username

然后JOIN评论表得到在提要项目上发布的评论

Then JOIN the comments table to get comments posted on the feed items

构建该提要是一项艰巨的任务

That is one big task to build that feed

到目前为止我有 3 个想法,这是您可以提供帮助的地方.

内存缓存选项:

  1. 使用 memcache 并缓存一个用户朋友列表作为一个数组,当用户登录网站,也当用户批准一个新的好友请求要添加到列表中的朋友,它将重建那里的缓存.
  2. 除了只是让朋友到那里,我还可以保存朋友的图片 URL 和用户名,这将通过在构建朋友提要时消除此查询来再次加快速度.

文件缓存选项:

  1. 和memcache选项一样确实但将此数据保存为数组到缓存文件而不是内存,然后将此缓存文件包含到页面.

  1. Do the same as the memcache option does but save this data as an array to a cache file instead of memory, then include this cache file into the page.

我不确定哪个是最好的我理解的表现方法memcache 将所有内容存储在内存中所以有20,000的朋友可以使用很多的朋友内存和文件缓存只会当用户把它放在内存中如果我是对的,需要它.另外如果我做了文件方法,当用户退出网站,我会删除有缓存文件所以缓存文件夹文件永远不会太大

I am not sure which is the best method for performance I understand memcache stores everything in memory so friends that have like 20,000 friends that could use a lot of memory and a file cache would only put it in memory when the users needs it if I am correct. Also if I did the file method, when a user logs out of the site, I would delete there cache file so the cache folder would never be too large of files

会话缓存选项:

  1. 与上面的文件缓存一样,我刚刚意识到会话数据被保存到一个文件中,所以它不能成为缓存吗?

请给我您的意见或您对此的任何建议或信息,因为我对缓存没有太多了解,我已经阅读了很多,但有时其他人的想法会很有帮助>

推荐答案

Memcache 是您最好的选择,原因有很多:

Memcache is your best bet for a lot of reasons:

  1. 它真的很快 - 一切都在内存中,并且针对像您这样的情况进行了高度优化(以及一般的缓存:)
  2. 它是分布式的 - 这意味着如果您有多个网络/应用服务器在运行,它们都可以访问相同的缓存
  3. 您可以将多台服务器用于内存缓存 - 如果您有几台相对未充分利用的服务器(或几台专用缓存服务器),您可以将它们全部集中到一个大缓存中
  4. 超级可扩展(出于前面提到的原因)
  5. 它有很好的 PHP 支持 - 内存缓存的 PECL 包最近更新了很多新的优点
  6. 您甚至可以将您的用户会话存储在 memcache 中 - 只需在您的 php.ini 文件中进行设置即可.这比将会话存储在数据库中要快得多,并且允许您的会话在多个 Web 主机上持续存在(如果您处于负载平衡的情况下)...这也将为您的站点提供一些性能提升,因为没有必要在每个请求中访问文件系统/数据库以获取会话信息.

... 还有更多 ;)

... and many more ;)

至于您对单个缓存项的内存占用量的一些担忧,您有几个选择.我最初的想法是试一试,看看这些缓存项到底有多大(你可以找到几个开源的东西来监控实际的缓存使用情况,比如 cacti).我认为它们会比你想象的要小.

As to some of your concerns about memory footprint of individual cached items you've got a few options. My initial thought is to just give it a whirl, see how big these cache items really get (you can find several open-source things to monitor the actual cache usage, such as cacti). I think they'll be smaller than you'd think.

如果不是,我建议重新考虑您的缓存策略,就您实际缓存的内容、缓存时间等而言.也许您可以从缓存中已有的几项内容(即缓存个人)构建提要用户数据,然后从缓存中的所有这些单个项目为一个人构建提要).在这方面有很多好文章,只需搜索它们:)

If they're not, I'd suggest re-thinking your cache strategy as far as what you actually cache, for how long, etc. Maybe you could build the feed from several things already in the cache (i.e. cache individual user data, and then build the feed for a person from all those individual items in cache). There are a lot of good articles out there on that front, just search 'em out :)

这篇关于会话 VS 文件 VS Memcache 用于 PHP 中的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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