PHP 缓存而不影响文件权限(另外,如何在 PHP 中模拟 ASP 应用程序变量)

发布于 2024-07-13 01:09:39 字数 2189 浏览 6 评论 0原文

我正在尝试在 PHP 脚本中实现某种缓存,该脚本将发送到许多不同的客户端/站点,这些客户端/站点将由相当非技术的用户使用各种 Web 主机进行部署。 由于用户的非技术性质,我想避免要求他们调整文件权限。 缓存需要跨会话,因此使用会话变量已经过时了。 如果我在 ASP 中进行编码,我会使用应用程序变量,但它们在 PHP 中不存在(据我所知)

有人对如何完成此任务有任何建议吗?

以下是我考虑过的一些可能性,对这些的任何评论都会有用:

  • 通过系统临时文件夹中的文件进行缓存 - 我可以使用 sys_get_temp_dir() (或 PHP4 上的一些自制类似函数)来帮助找到这样的文件夹。 这里的缺点是它可能无法在使用 openbase_dir 限制的主机上工作
  • 我查看过的一些网站提到欺骗 PHP 使所有用户会话共享相同的会话状态,从而强制会话变量像会话变量一样工作。 当然,我现在找不到那个帖子了……无论如何,这看起来很可怕。 (一旦我再次找到它,我将用这篇文章的链接更新这个问题)
  • 使用第三方数据存储,例如亚马逊的简单存储服务 - 似乎有点矫枉过正
  • 将数据缓存在我控制的服务器上,并让客户端下载新数据从那里开始每次点击。

再次强调,如果对这些想法或任何新想法有任何评论,我们将不胜感激。

更新:我尝试使用 session_id() 来使用共享会话状态,但它不起作用。 每个会话都维护自己的“GlobalCache”,有什么想法吗?:

// This code doesn't work, but the similar code in my answer does!

function test() {
    if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
    $_SESSION['session_count']++;


    $count = get_cache( 'count' );

    print "pre application count: $count<br>";

    if ( !$count )
        $count = 0;

    $count++;

    print "session_id: " . session_id() . "<br>";
    print "post application count: $count<br>";
    print "session_count: " . $_SESSION['session_count'] . "<br>";

    set_cache('count', $count);
}


function set_cache( $name, $value ) {
    $old_session = session_id();
    print "old_session (set): $old_session<br>";

    session_id("GlobalCache");
    print "new_session (set): " . session_id() .  "<br>";

    $_SESSION[$name] = $value;
    session_id( $old_session );
}

function get_cache( $name ) {
    $old_session = session_id();
    print "old_session (get): $old_session<br>";

    session_id("GlobalCache");
    print "new_session (get): " . session_id() .  "<br>";

    $value = $_SESSION[$name];
    session_id( $old_session );
    return $value;
}

session_start();
test();

更新:一些人建议使用memcached,这实际上对某些人来说是一个很好的解决方案,但由于我无法控制终端服务器环境这不是一个选项。 我们的想法是拥有一个脚本,人们可以通过 FTP 传输到开箱即用的共享托管帐户。

更新:有人建议使用脚本创建我自己的缓存文件夹,但是我不需要在已经具有写入权限的文件夹内创建这样的文件夹吗?

更新,找到解决方案:我最终找出了全局会话脚本中的问题,并发布了我自己的答案。 感谢大家的帮助。

I'm trying to implement some kind of caching in a PHP script that will go out to many different clients/sites, which will be deployed by fairly non-technical users, using a variety of web hosts. Due to the non-technical nature of the users, I'd like to avoid asking them to tweak file permissions. The caching needs to be across sessions, so using session variables is out. If I was coding this in ASP I'd use application variables, but they don't exist in PHP (to my knowledge)

Does anyone have any suggestions on how to accomplish this?

Here are some possibilities I've considered, any comments on these would be useful:

  • Caching via files in system temp folders - I could use sys_get_temp_dir() (or some home rolled similar function on PHP4) to help find such a folder. The disadvantage here, is it probably wouldn't work on hosts using the openbase_dir restriction
  • Some website I looked at mentioned tricking PHP into making all user sessions share the same session state thereby forcing session variables to act like session variables. Of course I can't find that post now... This seems scary anyway. (I'll update this question with the link to this post once I find it again)
  • Use a third party data store like Amazon's Simple Storage Service - seems like overkill
  • Cache the data on a server I control, and have the client download new data from there on each hit.

Once again, any comments on these ideas or any new ones would be appreciated.

UPDATE: I tried using session_id() to use a shared session state, but it doesn't work. Each session is maintaining its own "GlobalCache", any ideas why?:

// This code doesn't work, but the similar code in my answer does!

function test() {
    if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
    $_SESSION['session_count']++;


    $count = get_cache( 'count' );

    print "pre application count: $count<br>";

    if ( !$count )
        $count = 0;

    $count++;

    print "session_id: " . session_id() . "<br>";
    print "post application count: $count<br>";
    print "session_count: " . $_SESSION['session_count'] . "<br>";

    set_cache('count', $count);
}


function set_cache( $name, $value ) {
    $old_session = session_id();
    print "old_session (set): $old_session<br>";

    session_id("GlobalCache");
    print "new_session (set): " . session_id() .  "<br>";

    $_SESSION[$name] = $value;
    session_id( $old_session );
}

function get_cache( $name ) {
    $old_session = session_id();
    print "old_session (get): $old_session<br>";

    session_id("GlobalCache");
    print "new_session (get): " . session_id() .  "<br>";

    $value = $_SESSION[$name];
    session_id( $old_session );
    return $value;
}

session_start();
test();

UPDATE: some have suggested using memcached, which is in fact a great solution for some, but since I don't have control over the end server environment it isn't an option. The idea is to have a script that people can just FTP up to a shared hosting account that just works out of the box.

UPDATE: someone suggested creating my own cache folder with the script, but wouldn't I need to create such a folder inside a folder that already had write permissions?

UPDATE, SOLUTION FOUND: I ended up figuring out the problems in my global session script and have posted my own answer to that effect. Thanks for the help everyone.

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

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

发布评论

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

评论(6

冰葑 2024-07-20 01:09:39

好的,我想出了如何通过使用共享/全局会话状态模拟 ASP 样式应用程序变量来做到这一点。 相关更新中我的非工作代码有两个关键更改:

  1. 要切换会话状态,我们必须结束当前会话,切换到新会话,然后启动它。 我将这个过程封装在 switch_session() 中

  2. 由于我们要切换会话 ID,因此我们必须使用 ob_start()/ob_end_flush() 缓冲页面的输出,以便会话 cookie 不会发送得太快。

完整的工作代码如下(也已清理!)。 这可以通过在 IE 和 Firefox 窗口中加载页面并每次重新加载几次来观察计数器的上升来轻松测试:

<?php
function test() {

    // Do a regular session count
    print "session_id: " . session_id() . "<br>";
    if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
    $_SESSION['session_count']++;
    print "session count: " . $_SESSION['session_count'] . "<br>";


    // Do an application count
    $count = get_cache( 'count' );
    if ( !$count ) $count = 0;
    $count++;
    print "application count: $count<br>";
    set_cache('count', $count);
}


function set_cache( $name, $value ) {
    $old_session = switch_session("GlobalCache");
    $_SESSION[$name] = $value;
    switch_session( $old_session );
}

function get_cache( $name ) {
    $old_session = switch_session("GlobalCache");
    $value = $_SESSION[$name];
    switch_session( $old_session );
    return $value;
}

function switch_session( $session_id ) {

    // switch the session and return the original
    $old_id = session_id();

    session_write_close();
    session_id($session_id);
    session_start();
    return $old_id;
}

ob_start();
session_start();
test();
ob_end_flush();
?>

Okay, I figured out how to do this by emulating ASP-style application variables using a shared/global session state. Two key changes from my non-working code in the relevant update:

  1. To switch session state, we must end the current session, switch to the new one, then start it. I've encapsulated this process in switch_session()

  2. Since we are switching session ids around we have to buffer the page's output using ob_start()/ob_end_flush() so that the session cookie isn't sent too soon.

The full working code follows (cleaned up too!). This can be easily tested by loading the page in an IE and a Firefox window, and reloading each several times to watch the counters rise:

<?php
function test() {

    // Do a regular session count
    print "session_id: " . session_id() . "<br>";
    if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
    $_SESSION['session_count']++;
    print "session count: " . $_SESSION['session_count'] . "<br>";


    // Do an application count
    $count = get_cache( 'count' );
    if ( !$count ) $count = 0;
    $count++;
    print "application count: $count<br>";
    set_cache('count', $count);
}


function set_cache( $name, $value ) {
    $old_session = switch_session("GlobalCache");
    $_SESSION[$name] = $value;
    switch_session( $old_session );
}

function get_cache( $name ) {
    $old_session = switch_session("GlobalCache");
    $value = $_SESSION[$name];
    switch_session( $old_session );
    return $value;
}

function switch_session( $session_id ) {

    // switch the session and return the original
    $old_id = session_id();

    session_write_close();
    session_id($session_id);
    session_start();
    return $old_id;
}

ob_start();
session_start();
test();
ob_end_flush();
?>

冰魂雪魄 2024-07-20 01:09:39

您可以使用具有固定会话密钥的会话。

http://de.php.net/manual/en/function。 session-id.php

session_id([id]) 用于获取或设置当前会话的会话id
如果指定了 id,它将替换当前会话 id。 为此,需要在 session_start() 之前调用 session_id()。

You could use sessions with a fixed session key.

http://de.php.net/manual/en/function.session-id.php:

session_id([id]) is used to get or set the session id for the current session.
If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose.

桃扇骨 2024-07-20 01:09:39

在应用程序的目录中使用您自己的缓存目录。 这样,您就不必为各种服务器和/或 PHP 设置而烦恼,并获得最佳的可移植性。

Use your own cache dir in your application’s dir. Therewith you don’t have to struggle with various server and/or PHP settings and get the best portability.

他不在意 2024-07-20 01:09:39

我投票支持“将数据缓存在我控制的服务器上,并让客户端在每次点击时从那里下载新数据”。

所有其他道路都会导致某些疯狂。

I vote for "Cache the data on a server I control, and have the client download new data from there on each hit."

All other paths lead to certain madness.

亽野灬性zι浪 2024-07-20 01:09:39

查看 php 的 memcached - 它工作得非常好。

http://www.php.net/memcache

Check out memcached for php- it works really well.

http://www.php.net/memcache

成熟的代价 2024-07-20 01:09:39

由于我期望高负载,因此我使用作者更新的函数作为缓存简单数据库查询结果的方法。 我还存储了一个时间戳,以便我可以定义页面更新数据库结果的频率,而不仅仅是获取缓存的值。

我可以告诉你,这个缓存函数的性能比每次请求时直接访问数据库的性能要差得多。 我真的杀了服务器。 一旦我切换回简单地查询数据库,服务器就会全速运转,甚至没有注意到相当大的负载。

I used the updated function by the author as a way to cache a simple database query result since I was expecting high load. I also stored a timestamp so that I could define how often the page would renew the database result rather than just taking the cached value.

I can tell you that this cache function had much much much worse performance than going straight for the database at each request. I actually killed the server. Once I switched back to simply querying the database the server got up to full speed and the pretty big load was not even noticed.

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