Methods for caching PHP objects to file?

后端 未结 8 988
时光取名叫无心
时光取名叫无心 2020-12-24 12:42

In ASPNET, I grew to love the Application and Cache stores. They\'re awesome. For the uninitiated, you can just throw your data-logic objects into them, and hey-presto, you

8条回答
  •  感动是毒
    2020-12-24 13:17

    What I always do if I have to be able to write is to ensure I'm not writing anywhere I have PHP code. Typically my directory structure looks something like this (it's varied between projects, but this is the general idea):

    project/
      app/
      html/
        index.php
        data/
      cache/
    

    app is not writable by the web server (neither is index.php, preferably). cache is writable and used for caching things such as parsed templates and objects. data is possibly writable, depending on need. That is, if the users upload data, it goes into data.

    The web server gets pointed to project/html and whatever method is convenient is used to set up index.php as the script to run for every page in the project. You can use mod_rewrite in Apache, or content negotiation (my preference but often not possible), or whatever other method you like.

    All your real code lives in app, which is not directly accessible by the web server, but should be added to the PHP path.

    This has worked quite well for me for several projects. I've even been able to get, for instance, Wikimedia to work with a modified version of this structure.

    Oh... and I'd use serialize()/unserialize() to do the caching, although generating PHP code has a certain appeal. All the templating engines I know of generate PHP code to execute, making post-parse very fast.

提交回复
热议问题