开发者

Can I load lots of data only once and use it on each request?

Is there a way to load a big data object into the memory, which usually has to be loaded at each request, only once?

In Java you can instantiate an object in a servlet when this servlet is loaded, but once it's there you can use it in any request. Example is below. Can this be done in PHP?

public class SampleServlet extends HttpServlet {  
  private static HugeGraphObject hgo;

  public void init() {
    hgo = HugeGraphObjectFactory.get();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletEx开发者_StackOverflow社区ception, IOException {
    String param = request.getParameter("q");
    response.getWriter().write(hgo.getSomeThing(param));
  }
}


You would cache it in APC. Something like this,

$hgo = apc_fetch("hgo_v1");

if (!$hgo) {
   $hgo = HugeGraphObjectFactory();
   apc_store("hgo_v1", $hgo);
}

This way, it only needs to load the object once per server instance.


You are essentially talking about caching. In order to do this, you'll need to utilize one of the PHP caching solutions - APC, XCache, memcached. There are a lot of great articles that compare these solutions in terms of benchmarking. Memcached can be used across servers, which is why it is so popular.

I have personally used memcached and XCache. Storing objects and variables can reduce overhead by more than half, if used properly. Take a look at Zend_Cache (http://framework.zend.com/manual/en/zend.cache.html), which is a class that handles caching across platforms (APC, XCache, etc.). When using Zend_Cache, switching from APC to XCache, or file-based to memcached is as simple as changing a single string.


Use shared memory.


As others have said, the memory allocated for PHP is blown away and re-initialized for every request - so any data which you want to persist beyond a single request needs to be stored eslewhere. That also means that you have a significant overhead loading it back into PHP variable(s) to service requests.

If you're trying to write Java programs using PHP then you're not going to get very far. Its not a failing of PHP - if you try to write C programs in Java or Lisp programs in Forth then you'll have the same problem. I think it's highly probable that you could solve the problem by refactoring / pushing the data out to a relevant storage medium and querying it effectively.

There are some very unusual cases where that is not the case (don't assume this is in any way an endorsement of your methodology). Here the way to solve the problem is to develop a daemon providing a service to client programs - but if you're writing using OO then do be careful about the memory management (you might consider using the circular reference checking garbage collector).

C.


Just with PHP I believe not, its server side, so the browser will read and process every time... (if I'm not mistaken) ;)


In PHP each trip to the server is unique.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜