dongwang3066 2013-08-09 14:59
浏览 36
已采纳

如何读取大XML数据并在PHP中缓存它

I am doing a PHP app and I need to read several xml files containing text and I want show it to the user. This is working without any problems.

The information stored in those files could be cached for a long time as it does not change too often. However, those files tend to be kind of heavy so it could be a bad idea to load them at every request. i'm trying to find a way to load the data in a lazy way maybe by using a singleton and store them somewhere. Si, I still have the following questions:

  • where to store de data? as session parameters?
  • how to do the lazy loading? using singleton? is that even a good idea in php?

Thanks a lot

  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2013-08-09 15:13
    关注

    Do not use session parameters. These are typically stored in a text file which is parsed every time you call session_start(). You should avoid storing anything more than a few bytes there.

    Use an external cache system instead. The APC extension offers a simple in-memory user cache, or otherwise you can use Memcached for a cache system that can be shared between many php servers.

    Lazy loading is not difficult:

    ...
    private static $my_cached_object = null;
    public static function GetMyObject() {
       if (null === self::$my_cached_object) {
    
          self::$my_cached_object = load_my_object();
    
       }
       return self::$my_cached_object;
    }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗