如何在PHP中存储每个用户会话的缓存?

如何在PHP中存储每个用户会话的缓存?,php,session,caching,Php,Session,Caching,我想缓存我的PHP输出。我只是对简单的PHP解决方案感兴趣。目前没有Memcache/Redis/APC。现在我是这样做的, <?php $timeout = 3600; // One Hour $file = md5($_SERVER['REQUEST_URI']); if (file_exists($file) && (filemtime($file) + $timeout) > time()) { // Output the existing file

我想缓存我的PHP输出。我只是对简单的PHP解决方案感兴趣。目前没有Memcache/Redis/APC。现在我是这样做的,

<?php

$timeout = 3600; // One Hour
$file = md5($_SERVER['REQUEST_URI']);
if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
    // Output the existing file to the user
    readfile($file);
    exit();
} else {
    // Setup saving and let the page execute
    ob_start();

    // HTML Output

    $content = ob_get_flush();
    file_put_contents($file, $content);
}

?>

它工作得很好

但是,如果我们在这个页面上登录了用户信息呢。任何进一步的请求都将从此文件获取数据&它可能包含登录用户的信息。如何应对


我们是否需要为每个会话/用户创建缓存文件?

< p>如果你有特定的理由缓存每个用户的页面输出,考虑缓存这个页面所基于的数据。也就是说,创建应用程序层缓存,而不是在web服务器或输出阶段进行。否则,别担心。。。设置适当的标题,让浏览器担心缓存问题。当你有一个只为一个用户生成的页面时,你试图创造新的问题。

< P>如果你有特定的理由缓存每个用户的页面的输出,考虑缓存这个页面所基于的数据。也就是说,创建应用程序层缓存,而不是在web服务器或输出阶段进行。否则,别担心。。。设置适当的标题,让浏览器担心缓存问题。当你有一个只为一个用户生成的页面时,你试图创造新的问题。

< P>如果你有特定的理由缓存每个用户的页面的输出,考虑缓存这个页面所基于的数据。也就是说,创建应用程序层缓存,而不是在web服务器或输出阶段进行。否则,别担心。。。设置适当的标题,让浏览器担心缓存问题。当你有一个只为一个用户生成的页面时,你试图创造新的问题。

< P>如果你有特定的理由缓存每个用户的页面的输出,考虑缓存这个页面所基于的数据。也就是说,创建应用程序层缓存,而不是在web服务器或输出阶段进行。否则,别担心。。。设置适当的标题,让浏览器担心缓存问题。当您有一个只为单个用户生成的页面时,尝试在此处重新发明控制盘会产生更多的问题。

尝试此代码-每个用户会话缓存。
$timeout = 3600; // One Hour

$_uuid = isset($_SESSION['uuid']) ? $_SESSION['uuid'] : uniqid() . "_" .time();
$_SESSION['uuid'] = $_uuid;

$file = $_uuid . "_" . md5($_SERVER['REQUEST_URI']);

if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
    // Output the existing file to the user
    readfile($file);
    exit();
} else {
    // Setup saving and let the page execute
    ob_start();

    // HTML Output

    $content = ob_get_flush();
    file_put_contents($file, $content);
}

?>

请尝试此代码-按用户会话缓存。
$timeout = 3600; // One Hour

$_uuid = isset($_SESSION['uuid']) ? $_SESSION['uuid'] : uniqid() . "_" .time();
$_SESSION['uuid'] = $_uuid;

$file = $_uuid . "_" . md5($_SERVER['REQUEST_URI']);

if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
    // Output the existing file to the user
    readfile($file);
    exit();
} else {
    // Setup saving and let the page execute
    ob_start();

    // HTML Output

    $content = ob_get_flush();
    file_put_contents($file, $content);
}

?>

请尝试此代码-按用户会话缓存。
$timeout = 3600; // One Hour

$_uuid = isset($_SESSION['uuid']) ? $_SESSION['uuid'] : uniqid() . "_" .time();
$_SESSION['uuid'] = $_uuid;

$file = $_uuid . "_" . md5($_SERVER['REQUEST_URI']);

if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
    // Output the existing file to the user
    readfile($file);
    exit();
} else {
    // Setup saving and let the page execute
    ob_start();

    // HTML Output

    $content = ob_get_flush();
    file_put_contents($file, $content);
}

?>

请尝试此代码-按用户会话缓存。
$timeout = 3600; // One Hour

$_uuid = isset($_SESSION['uuid']) ? $_SESSION['uuid'] : uniqid() . "_" .time();
$_SESSION['uuid'] = $_uuid;

$file = $_uuid . "_" . md5($_SERVER['REQUEST_URI']);

if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
    // Output the existing file to the user
    readfile($file);
    exit();
} else {
    // Setup saving and let the page execute
    ob_start();

    // HTML Output

    $content = ob_get_flush();
    file_put_contents($file, $content);
}

?>

正是出于这个原因,您通常不会像这样缓存动态个性化资源。在登录区域内,最好在适当的地方使用选择性缓存(比如大列表或需要很长时间的查询)。您应该真正解释为什么不想使用行业标准解决方案来解决此类问题。如果这是一个学习项目,那么就这么说。是的,不建议对用户数据所在的页面使用缓存,我们可以将其用于大型静态内容,如js、图像等。正是出于这个原因,您通常不会缓存动态个性化资源。在登录区域内,最好在适当的地方使用选择性缓存(比如大列表或需要很长时间的查询)。您应该真正解释为什么不想使用行业标准解决方案来解决此类问题。如果这是一个学习项目,那么就这么说。是的,不建议对用户数据所在的页面使用缓存,我们可以将其用于大型静态内容,如js、图像等。正是出于这个原因,您通常不会缓存动态个性化资源。在登录区域内,最好在适当的地方使用选择性缓存(比如大列表或需要很长时间的查询)。您应该真正解释为什么不想使用行业标准解决方案来解决此类问题。如果这是一个学习项目,那么就这么说。是的,不建议对用户数据所在的页面使用缓存,我们可以将其用于大型静态内容,如js、图像等。正是出于这个原因,您通常不会缓存动态个性化资源。在登录区域内,最好在适当的地方使用选择性缓存(比如大列表或需要很长时间的查询)。您应该真正解释为什么不想使用行业标准解决方案来解决此类问题。如果这是一个学习项目,那么就这么说吧。是的,不建议对用户数据所在的页面使用缓存,我们可以将其用于大型静态内容,如js、图像等