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

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

问题描述

我有一个社交网络

  • users表大约有60,000行

  • The users table is around 60,000 rows

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

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

我想做一个朋友供稿,墙,无论您喜欢叫什么,它都会显示诸如用户状态帖子(Twitter类型的帖子)之类的内容,它将显示一些不同的项目,但是一开始它只是朋友状态,甚至是博客文章.

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

然后加入评论表以获取 提要项上发布的评论

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

这是构建该供稿的一项重要任务

That is one big task to build that feed

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

Memcache选项:

  1. 使用内存缓存并缓存用户 当用户将好友列表作为数组时 登录到站点,也当用户登录 批准新的朋友请求 要添加到该列表的朋友,它 会在那里重建缓存.
  2. 除了可以结交朋友外,我还可以在其中保存朋友的图片URL和用户名,这将通过在构建朋友供稿时消除此查询来再次加快工作速度.
  1. Use memcache and cache a users friendlist as an array when the user logs into the site, also when the user approves a new friend request for a friend to be added to there list, it would rebuild there cache.
  2. In addition to just getting there friends I could save there friends picture URL and username, this would speed up things again by eliminating this query when building the friend feed.

文件缓存选项:

  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. 它是分布式的-这意味着如果您有多个正在运行的Web/应用服务器,它们都可以访问相同的缓存
  3. 您可以将多个服务器池化以用于内存缓存-如果您有一些利用率相对较低的服务器(或几个专用的缓存服务器),则可以将它们全部合并到一个大缓存中.
  4. 它是超级可伸缩的(出于前面提到的原因)
  5. 它具有强大的PHP支持-用于memcache的PECL软件包最近进行了更新,带来了许多新的优点
  6. 您甚至可以将用户会话存储在内存缓存中-只需在php.ini文件中进行设置即可.这比将会话存储在数据库中要快得多,并且可以使您的会话在多个Web主机之间保持持久(如果您处于负载平衡的情况下)...这也将使您的网站在性能上有所提升,因为不需要可以在文件系统/数据库中获取每个请求的会话信息.
  1. It's REALLY fast - Everything's in memory, and it's highly optimized for situations just like yours (and caching in general :)
  2. It's distributed - This means that if you have multiple web / app servers running, they can all access the same cache
  3. You can pool multiple servers for memcache - If you've got a few servers that are relatively underutilized (or several dedicated cache servers), you can pool them all together into one big cache
  4. It's super-scalable (for the reasons mentioned prior)
  5. It's got great PHP support - The PECL package for memcache was recently updated with a lot of new goodness
  6. You can even store your user sessions in memcache - just set it up in your php.ini file. This is much faster than storing sessions in databases, and allows your sessions to persist across multiple web hosts (if you're in a load balanced situation)... this will also give your site a bit of a performance boost as there's no need to hit the filesystem / database for session info on every request.

...还有更多;)

关于您对单个缓存项目的内存占用量的一些担忧,您可以选择几种方法.我最初的想法是旋转一下,看看这些缓存项真正得到了多大(您可以找到一些开源的东西来监视实际的缓存使用情况,例如仙人掌).我认为它们会比您想象的要小.

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天全站免登陆