关于在会话中存储用户对象的php性能问题
我正在组装一个基于用户的网络应用程序。系统在登录时创建一个用户对象,然后将其存储在会话数组中。在每个页面加载时,系统都会根据数据库中存储的会话密钥验证会话数据,并检查与用户相关的任何数据是否已更改(用户表中的布尔标志)。如果数据已更改,则重新创建用户对象,否则我将使用存储在会话中的用户对象。它看起来像:
session_start();
if (isset($_SESSION['name']))
{
$flags = get_session_flags($_SESSION['session_key']);
if (!$flags['valid_session_key'])
{
logout();
redirect_to_home();
}
if ($flags['user_data_changed'])
{
$user = recreate_user_object();
}
else
{
$user = $_SESSION['user'];
}
}
我担心的是 php/mysql 服务器性能。在页面加载之间存储用户对象可能会遇到哪些问题?我应该总是从数据库中提取新鲜数据吗?它是大量信息,包括存储在用户对象内的多个基于活动的对象。
感谢您的帮助。
I have a user based web app that I am putting together. The system creates a user object at login and then stores it in the session array. On each page load the system validates the session data against a session key stored in the database and checks to see if any data related to the user has been changed (bool flag in the user table). If data has been changed the user object is recreated, otherwise I use the one stored in sessions. It looks something like:
session_start();
if (isset($_SESSION['name']))
{
$flags = get_session_flags($_SESSION['session_key']);
if (!$flags['valid_session_key'])
{
logout();
redirect_to_home();
}
if ($flags['user_data_changed'])
{
$user = recreate_user_object();
}
else
{
$user = $_SESSION['user'];
}
}
My concerns are about php/mysql server performance. What possible issues might I run into storing the user object between page loads? Should I just always pull the data fresh from the db? It is a significant amount of information including several activity based objects stored inside the user object.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
User 对象由什么构成?您将使用多少 User 对象?您是否经常需要它提供的所有数据?
用户数据多久更新一次?如果它只在几个页面上更新,那么检查每个页面加载似乎有点多。上一个问题仍然存在,即什么构成了重建用户对象。
What constitutes a User object? How much of the User object will you utilize? Do you constantly need all the data it offers?
How often is the user data updated? If it's only updated on a few pages, then doing the check every page load seems a bit much. The previous question still holds though as what constitutes rebuilding a User object.