将 PHP 会话和 cookie 视为缓存选项是否不好?

发布于 2024-10-28 02:02:36 字数 1445 浏览 0 评论 0原文

下面是 PHP 中基于驱动程序的缓存类的简单开始。另一个用户提交了它......

<?php
class Cache
{
    protected $Driver;
    public function __construct($driver)
    {
        require_once 'drivers/' . $drivers . '.driver.php';
        $driver = "Cache_Driver_" . $driver;

        $this->Driver = new $driver;
    }


    public function create($key,$value)
    {
        return $this->Driver->create($key,$value);
    }

    public function read($key,$value)
    {
        return $this->Driver->read($key,$value);
    }

    public function update($key,$value)
    {
         return $this->Driver->update($key,$value);
    }

    public function remove($key)
    {
        return $this->Driver->remove($key);
    }
}

?>

我的计划是为我的应用程序中的每种缓存方法拥有单独的类“文件/驱动程序”。到目前为止,我计划使用,

- 文件
- 装甲运兵车
- Memcache 或 Memcached
-Redis
- 会议
- Cookies

所以我的主要问题(#1)在这里,我意识到会话和cookie通常不被认为是缓存的一部分,但我认为它们适合这里,而不是真正的“缓存”大量数据,但更多的是作为获取/设置会话和 cookie 的一种方式,而不是具有特定的会话和 cookie 类,他们将利用此类中的 get/set/unset 方法。

请告诉我你对这个想法有何看法?好,坏,有什么建议吗?谢谢你!

问题(#2) n对于我的会话类,我在想,在会话驱动程序类的构造函数中,我可以在那里调用

session_start()

。可能的问题,这是否要求我立即使用会话驱动程序启动缓存,就像在调用其他任何内容之前在应用程序的顶部一样?或者更糟糕的是,必须在类本身之外、在我的应用程序顶部调用 session_start() ?再次寻找建议,我觉得我忽略了一些东西,再次感谢您的帮助。

<?php  
$cache = new Cache('Session');
?>

Below is a simple start for a driver based cache class in PHP. Another user submitted it...

<?php
class Cache
{
    protected $Driver;
    public function __construct($driver)
    {
        require_once 'drivers/' . $drivers . '.driver.php';
        $driver = "Cache_Driver_" . $driver;

        $this->Driver = new $driver;
    }


    public function create($key,$value)
    {
        return $this->Driver->create($key,$value);
    }

    public function read($key,$value)
    {
        return $this->Driver->read($key,$value);
    }

    public function update($key,$value)
    {
         return $this->Driver->update($key,$value);
    }

    public function remove($key)
    {
        return $this->Driver->remove($key);
    }
}

?>

My plan is to have separate class "files/drivers" for each method of caching I will have in my app. So far I plan on using,

- File
- APC
- Memcache or Memcached
- Redis
- Sessions
- Cookies

So my main question(#1) here, I realize that sessions and cookies are not usually considered part of the caching but I think that they would fit in here, not to really "cache" a lot of data but more as a way to get/set my sessions and cookies, instead of having specific sessions and cookie classes, they would utilize my get/set/unset methods from this class.

Please tell me what you think about this idea? Good, bad, any suggetions? Thank you!

question(#2) nFor my class that will be for sessions, I was thinking, in my constructor for the session Driver class, I could call

session_start()

there. Possible problem, wouldn't this require that I initiate the cache with session driver right away, like at the top of my application before anything else is called? Or even worse, would have to call session_start() outside of the class itself, at the top of my application? Again looking for suggetions, I feel I am overlooking something, thanks again for any help.

<?php  
$cache = new Cache('Session');
?>

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

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

发布评论

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

评论(3

丶情人眼里出诗心の 2024-11-04 02:02:36

cookie

cookie 中的数据存储存在许多限制,这使得它们作为缓存解决方案不太理想,并且可能不值得付出努力:

  • 每个域的 cookie 数量和大小都受到限制。例如在 IE 中,每个 cookie 的大小限制为 4kb,并且最多可以有 20 个 cookie每个域。
  • 每个请求都会将 Cookie 发送到服务器。如果您在 cookie 中存储 80kb 的数据,这将使每个请求(包括针对同一主机上的非动态资源的请求)变得相当大。
  • Cookie 是针对每个用户的(见下文)

$_SESSION

将数据缓存在 $_SESSION 中更为合理,因为仅增加了 back 和-forth 流量是单个小会话 ID cookie 标头。在 $_SESSION 数组中进行缓存的一大缺点是每个客户端的会话数据是独立的。通过其他缓存后端(APC、Memcache 等),数据可以共享并可用于每个客户端的请求。这引发的问题是,对于每个客户端会话缓存,没有简单的方法可以清除或更新其他客户端会话数据中的值,从而导致缓存过时。

结论

虽然每客户端缓存通常是合理的事情,但它们与您提到的其他共享存储缓存后端(APC、memcache 等)之间的操作模型之间的差异意味着许多应用程序当提供每个客户端缓存时,期望共享存储缓存可能会中断。两种类型的缓存都有自己的位置,但是混淆正在使用的类型可能会导致问题。

就个人而言,我会跳过会话数据和 cookie 作为缓存后端,除非您专门构建每个客户端的缓存系统。在这种情况下,我会跳过 cookie,只实现会话存储。

cookies

There are a number of limitations to data storage in cookies that are going to make them less than ideal as a caching solution and probably not worth the effort:

  • Cookies are limited in number and size per domain. For example in IE, each cookie is limited to 4kb and a there can be a maximum of 20 cookies per domain.
  • Cookies get sent to the server with every request. If you are storing 80kb of data in cookies that will make every request (including requests that are for non-dynamic resources on the same host) quite large.
  • Cookies are per-user (see below)

$_SESSION

Caching data in the $_SESSION is much more reasonable since the only increase in back-and-forth traffic is the single small session-id cookie header. The big downside with caching in the $_SESSION array is that the session-data is separate for each client. With your other caching back-ends (APC, Memcache, etc), data is shared and available for requests from every client. The issue this raises is that with per-client session caches, there is no easy way to clear or update a value from other clients' session-data, resulting in stale caches.

Conclusion

While per-client caches are reasonable things generally, the difference in operation model between them and the other shared-storage cache back-ends you mention (APC, memcache, etc) mean that many applications that are expecting a shared-storage cache will likely break when presented with a per-client cache. Both types of cache have their place, but confusing which type is being used can be a recipe for problems.

Personally, I would skip session data and cookies as caching backends unless you specifically are building a per-client caching system. In that case I would skip on cookies and just implement session storage.

握住我的手 2024-11-04 02:02:36

我意识到会话和cookie通常不被视为缓存的一部分,但我认为它们适合放在这里,不是真正“缓存”大量数据,而是更多地作为获取/设置会话和cookie的一种方式,他们不会使用特定的会话和 cookie 类,而是利用此类中的 get/set/unset 方法。

PHP 有完全足够的方法来处理 cookie 和会话。它们称为 $_COOKIE/setcookie$_SESSION。即使接口表面上足够,您也不需要为其他目的而设计的包装器。例如,cookie 有一些额外的事情需要您担心,例如域名和过期时间,而会话则不会。也许你的缓存数据会过期,但是你将如何在会话中管理过期信息呢?

如果您想包装它们,请包装它们,但不要将它们塞入为其他目的而设计的界面中。

这是否需要我立即使用会话驱动程序启动缓存,就像在调用其他任何内容之前在应用程序的顶部一样?

未必。 session_id() 返回一个当会话尚未启动时(至少在交互式提示符下),为空字符串。您可以使用它来检测是否需要启动会话。

I realize that sessions and cookies are not usually considered part of the caching but I think that they would fit in here, not to really "cache" a lot of data but more as a way to get/set my sessions and cookies, instead of having specific sessions and cookie classes, they would utilize my get/set/unset methods from this class.

PHP has perfectly adequate ways to deal with cookies and sessions. They're called $_COOKIE/setcookie and $_SESSION. You don't need a wrapper for these that is designed for another purpose, even if the interface is adequate on the surface. For example, cookies have extra things that you have to worry about, like domain names and expiration times, while sessions won't. Maybe your cached data will expire, but then how will you manage that expiration information in the session?

If you want to wrap them, wrap them, but don't wedge them into an interface designed for another purpose.

wouldn't this require that I initiate the cache with session driver right away, like at the top of my application before anything else is called?

Not necessarily. session_id() returns an empty string when the session has not yet been started, at least at the interactive prompt. You can use this to detect if you need to start a session.

素染倾城色 2024-11-04 02:02:36

PHP 已经支持不同的后端来管理会话。不要创建自己的包装器,PHP 会话已经是一个包装器。您可以将 PHP 配置为使用文件(默认)、mysql、memcache 或许多其他方法。

PHP already supports different backends for managing sessions. Don't create your own wrappers, the PHP session is already a wrapper. You can configure PHP to use files (default), mysql, memcache or a number of other methods.

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