PHP - 将“存活"的服务器端变量永远? [英] PHP - Server side variables that will "live" for ever?

查看:41
本文介绍了PHP - 将“存活"的服务器端变量永远?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在服务器内存中存储计数器的简单方法,以允许页面加载选择,有点(伪代码):

I'm looking for a simple way to store a counter in the server memory to allow page load selection, kind of (pseudocode):

if counter is odd then load page-x 
else (even counter)  load page-y
increment counter by 1
store counter in server's memory

会话变量在多个用户之间无济于事.

Session variables would not help, among multiple users.

我知道这可以通过将字段存储到数据库中来实现,但这似乎是一种麻烦的方法.想知道更快的东西.

I understand this could be achieved storing a field into the database, but this seems a cloggy approach. Was wondering for something faster.

这就是为什么我想到了一些跨会话保存在内存中的服务器端变量......

That's why I thought about some server side variables that are kept in memory across sessions....

推荐答案

如果您已经打开了一个数据库连接,我会考虑使用它.我无法想象在任何类型的负载下查询一行都会给您带来性能问题.

If you have a database connection already open, I would consider using that. I can't imagine that querying one row can give you performance problems under any kind of load.

如果您不想这样做,一个简单快速的奇数/偶数"检查方法可能是使用临时文件.

If you don't want to do that, a simple and fast approach for your "odd/even" check could be using a temporary file.

  • 如果文件存在,则条件为奇数"

  • If the file exists, the condition is "odd"

如果不存在,则条件为偶数"

If it doesn't exist, the condition is "even"

确保针对尝试创建或创建的两个实例的竞争条件进行检查同时删除同一个文件 - 在这种情况下,您必须确保脚本不会崩溃.

make sure you build in a check against the race condition of two instances trying to create or remove the same file at the exact same time - you have to make sure the script doesn't crash in that case.

这个解决方案,显然,即使服务器重启也能幸免于难.

This solution, obviously, survives even a server restart.

这篇关于PHP - 将“存活"的服务器端变量永远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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