PHP - 会话变量的替代品是什么

发布于 2024-12-09 20:22:22 字数 158 浏览 0 评论 0原文

我在会话中存储了很多变量,这造成了性能问题。 因此,我被要求将其存储在其他地方,我可以将其存储在数据库中,但这又会很慢。

有没有更好的替代方法来存储会话变量? 全局变量针对每个文件/请求。而cookie会向用户开放变量,并且不会将其保留在服务器端。

预先感谢您的回答!

I am storing a lot of variables in session, which is creating a performance problem.
So, I have been asked to store it somewhere else, I can store it in the database, but that would again be slow.

Is there any better alternative to store session variables?
Global variable are per file/request. While cookies will open the variables to users and will not keep it server side.

Thanks in advance for your answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-12-16 20:22:22

对于半持久数据,请考虑使用 memcached像这样。将缓存键存储在 $_SESSION 中,然后使用它来获取缓存数据。

由于 memcached 将所有内容缓存在内存中(并且严格来说是键值存储),因此它比数据库更快。对于会话之类的事情来说,这有点理想,因为如果您碰巧丢失了缓存的数据,则不会丢失任何严重的内容(用户只是意外注销)。

事实上,PHP Memcache 实现提供了一个会话处理程序(请参阅示例#2),它可以透明地为您处理会话,而无需您真正需要对代码进行任何修改。

Consider memcached for semi-persistent data like this. Store the cache key in $_SESSION and then use it to grab your cached data.

Since memcached caches everything in memory (and is strictly a key-value store), it's faster than a database. It's somewhat ideal for things like sessions, because if you happen to lose the cached data, nothing serious is lost (the user just gets unexpectedly logged out).

In fact, the PHP Memcache implementation provides a session handler (see Example #2) which could transparently handle your sessions for you without you really needing to make any modifications to your code.

维持三分热 2024-12-16 20:22:22

php 会话可以配置为以多种方式工作。如果服务器上有足够的可用内存,您可以从内存缓存运行它。这将是高性能,

您也可以使用数据库来存储会话信息,但正如您所说,这可能会很慢。

为什么它目前会造成性能问题?
是否正在创建大量会话或者是否在会话中存储大量数据?

php sessions can be configured to work in many ways. you can run it from memcache if you have enough free memory on the server. that would be high performance

you can also use database to store sessions information but as you say this can be slow.

why is it currently creating performance problems?
is there a large number of sessions being created or do you store large amounts of data in the session?

岁月静好 2024-12-16 20:22:22

这取决于“很多”变量有多少? Rails 的默认会话存储是使用 cookie,这通常对我来说就足够了。如果您担心暴露变量,则 cookie 会被加密。你必须有服务器端吗?使用 html5,您可以选择本地存储。

It depends on how many is "a lot" of variables? The default session storage for rails is using cookies, which is usually enough for me. The cookie is encrypted if you're worried about exposing the variables. Do you have to have it server side? With html5 you have localStorage options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文