且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

会话VS文件VS Memcache是​​否用于PHP中的缓存?

更新时间:2023-11-28 11:33:22

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 :)