简体   繁体   English

如何读取大型XML数据并将其缓存在PHP中

[英]how to read big XML data and cache it in 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. 我正在做一个PHP应用程序,我需要阅读几个包含文本的xml文件,并想向用户展示。 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? 这在php中是个好主意吗?

Thanks a lot 非常感谢

Do not use session parameters. 不要使用会话参数。 These are typically stored in a text file which is parsed every time you call session_start() . 这些通常存储在一个文本文件中,每次调用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. APC扩展提供了简单的内存中用户缓存,否则,您可以将Memcached用于可在许多php服务器之间共享的缓存系统。

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;
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM