Cache Object in PHP without using serialize

后端 未结 9 992
眼角桃花
眼角桃花 2020-12-14 12:26

I have a complex object that I create in a PHP script. I am looking for a way to store this object such that subsequent requests do not have to recreate it, or spend time un

相关标签:
9条回答
  • 2020-12-14 12:53

    You can rewrite your app to ReactPHP what create webserver in one long-running PHP process (just like Node.js or Web.py). Then you can build your big object once (on server start) as a global variable and access it from request event handlers.

    0 讨论(0)
  • 2020-12-14 12:54

    As far as I'm aware, it's not possible to cache objects in PHP without serializing. In general, however, caching mechanisms (APC, Memcache, etc) are really trying to remove the db connection(s) more than improve performance (and thereby decrease the overall DB strain). This is definitely how memcache, et al are employed with regards to Drupal. In other words, the caching mechanisms should allow you to scale, though they may not particularly improve performance.
    Implementing a caching mechanism should allow you to more easily scale outward, even if the performance per machine is no better than before for a single connection. At a certain threshold, DB performance will degrade sharply, and the caching mechanisms should help alleviate that issue.

    0 讨论(0)
  • 2020-12-14 12:54

    Maybe the solution is to not build a single, massive, expensive object.

    Given that a PHP application pretty much starts from a clean slate on every page load, a solution that depends on a single, giant object is a poor fit to the language. Since you don't go into much detail about what your object is & what it does, I can't be certain, but I'd suspect you don't really need everything the object does on every page load. If that's the case, you might seriously consider splitting it into a number of smaller, simpler classes that you can instantiate as needed.

    0 讨论(0)
提交回复
热议问题