是否建议将 PHP Session 存储在 MemCache 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13946033/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:19:52  来源:igfitidea点击:

Is it recommended to store PHP Sessions in MemCache?

phpmemcached

提问by Adam

I'm working with a couple of Web Servers behind a Load Balancer and I can enable Sticky Sessions to hold a user to the one specific Web Servers - this will work.

我正在使用负载均衡器后面的几个 Web 服务器,我可以启用粘性会话以将用户保留到一个特定的 Web 服务器 - 这将起作用。

I have been reading about PHP Sessions & MemCache. I must say what I've read is a touch confusing as some pages say its a good idea and others the opposite.

我一直在阅读有关 PHP Sessions 和 MemCache 的文章。我必须说我读到的内容有点令人困惑,因为有些页面说这是一个好主意,而其他页面则相反。

Questions:

问题:

  1. is it possible to keep php sessions in memcache?
  2. is it better to use sticky sessions over memcache?
  3. what are the problems with php sessions in memcache - note: I can get enough cache (amazon so its expandable).
  1. 是否可以在内存缓存中保留 php 会话?
  2. 在内存缓存上使用粘性会话更好吗?
  3. memcache 中的 php 会话有什么问题 - 注意:我可以获得足够的缓存(亚马逊因此可扩展)。

回答by FredTheWebGuy

1: YES. And I strongly recommend storing PHP sessions in Memcached. Here's why:

1:是的。我强烈建议将 PHP 会话存储在 Memcached 中。原因如下:

Memcached is great for storing small chunks of data that are frequently accessed by the database and filesystem.

Memcached 非常适合存储数据库和文件系统经常访问的小块数据。

Memcached was designed specifically for sessions. It was originally the brainchild of the lead developer of livejournal.com, and later used to also cache the content of users' posts. The benefit was immediate: most of the action was taking place in memory. Page load times greatly improved.

Memcached 专为会话而设计。它最初是 livejournal.com 的首席开发人员的创意,后来还用于缓存用户帖子的内容。好处是立竿见影的:大部分动作都发生在内存中。页面加载时间大大改善。

Thankfully, PHP and Apache have an easy implementation to handle sessions with Memcached. Simply install with a few shell commands

幸运的是,PHP 和 Apache 有一个简单的实现来处理与 Memcached 的会话。只需使用几个 shell 命令安装

example for debian:

以 debian 为例:

sudo apt-get -t stable install php5-memcached

and

change your php.ini settings to something similar to:

将您的 php.ini 设置更改为类似于:

(taken from http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/)

(取自http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/

 session.save_handler = memcache
 ; change server:port to fit your needs...
 session.save_path="tcp://server:port?persistent=1&weight=1&
timeout=1&retry_interval=15"

The key is the session.save_path

关键是 session.save_path

It will no longer point to a relative file path on your server. APC was mentioned- APC for the caching of .php files used by the program. APC and Memcached will reduce IO signicantly and leave Apache free to serve resources,such as images, faster.

它将不再指向您服务器上的相对文件路径。提到了 APC - APC 用于缓存程序使用的 .php 文件。APC 和 Memcached 将显着减少 IO,让 Apache 可以更快地免费提供资源,例如图像。

2:No

2:没有

3:The fundamental disadvantage of using Memcached is data volatility

3:使用Memcached的根本缺点是数据易变

Session data is not persistent in Memcached. So if and when the server crashes, all data in memory is lost. Everyone will have to log in again.

会话数据在 Memcached 中不是持久的。因此,如果服务器崩溃,内存中的所有数据都会丢失。每个人都必须重新登录。

And then you have memory consumption...

然后你有内存消耗......

Remember: the sessions are stored in the memory. If your website handles a large amount of concurrent users, you may have to shell out a little extra money for a larger memory allocation.

请记住:会话存储在内存中。如果您的网站处理大量并发用户,您可能需要为更大的内存分配多花一点钱。

回答by Brenton Alker

1. Yes, it is possible to keep PHP sessions in memcached.

1. 是的,可以在 memcached 中保留 PHP 会话。

The memcache extension even comes with a session handler that takes very little configuration to get up and running. http://php.net/manual/en/memcached.sessions.php

memcache 扩展甚至带有一个会话处理程序,只需很少的配置即可启动和运行。http://php.net/manual/en/memcached.sessions.php

2. Memcache/Sticky Sessions

2. Memcache/Sticky Sessions

I don't really know which is "better". I feel this is going to be one of those "it depends" answers. It likely depends on your reasons for load balancing. If a small number of users cause lots of load each, or if it's a large number causing a small load each.

我真的不知道哪个“更好”。我觉得这将是“视情况而定”的答案之一。这可能取决于您进行负载平衡的原因。如果少数用户导致每个负载大量,或者如果用户数量众多导致每个负载很小。

3. Cons of Memcache

3. Memcache 的缺点

There are probably 2 main cons to using memcache for sessions storage.

使用 memcache 进行会话存储可能有两个主要缺点。

Firstly, it is volatile. This means, if one of your memcached instances is restarted/crashes etc. any sessions stored in that instance are lost. While if they were using traditional file based sessions, they will be still there when the server returns.

首先,它是易变的。这意味着,如果您的 memcached 实例之一重新启动/崩溃等,则存储在该实例中的任何会话都将丢失。而如果他们使用传统的基于文件的会话,当服务器返回时他们仍然存在。

Secondly and probably more relevant, memcached doesn't guarantee persistance, it is only meant to be a cache. Data can be purged from memcached at any time, for any reason. While, in reality, the only reasons data should be purged is if the cache is nearing its size limits. The least recently accessed data will be expelled. Again, this might not be an issue, as the user is probably gone if their session is stale, but it depends on your needs.

其次,可能更相关的是,memcached 不保证持久性,它只是一个缓存。可以随时出于任何原因从 memcached 中清除数据。而实际上,应该清除数据的唯一原因是缓存是否接近其大小限制。最近最少访问的数据将被驱逐。同样,这可能不是问题,因为如果他们的会话陈旧,用户可能会离开,但这取决于您的需求。

回答by ribluc

If you want to use "memcacheD" extension not "memcache" (there are two different extensions) for session control, you should pay attention to modify php.ini.

如果要使用“memcacheD”扩展名而不是“memcache”(有两种不同的扩展名)进行会话控制,则​​应注意修改php.ini

Most web resources from Google is based on memcache because it's earlier version than memcacheD. They will say as following:

大多数来自 Google 的网络资源都基于 memcache,因为它的版本比 memcacheD 早。他们会说如下:

session.save_handler = memcache session.save_path = "tcp://localhost:11211"

session.save_handler = memcache session.save_path = "tcp://localhost:11211"

But it's not valid when it comes to memcacheD.

但是当涉及到 memcacheD 时它是无效的。

You should modify php.ini like that:

你应该像这样修改 php.ini :

session.save_handler = memcached session.save_path = "localhost:11211"

session.save_handler = memcached session.save_path = "localhost:11211"

There is no protocol indentifier.

没有协议标识符。

From: http://php.net/manual/en/memcached.sessions.php#99646

来自:http: //php.net/manual/en/memcached.sessions.php#99646

回答by indika

As my point of view its not recommended storing sessions in Memcached.If a session disappears, often the user is logged out,If a portion of a cache disappears or either due to a hardware crash it should not cause your users noticable pain.According to the memcached site, “memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.” So while developing your application, remember that you must have a fall-back mechanism to retrieve the data once it is not found in the Memcached server.

根据我的观点,不建议在 Memcached 中存储会话。如果会话消失,通常用户会被注销,如果缓存的一部分消失或由于硬件崩溃,它不应该给您的用户带来明显的痛苦。根据memcached 站点,“memcached 是一种高性能、分布式内存对象缓存系统,本质上是通用的,但旨在通过减轻数据库负载来加速动态 Web 应用程序。” 因此,在开发应用程序时,请记住,一旦在 Memcached 服务器中找不到数据,您就必须具有回退机制来检索数据。