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 ServletException, IOException {
String param = request.getParameter("q");
response.getWriter().write(hgo.getSomeThing(param));
}
}
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.