2

Possible Duplicate:
Cache Object in PHP without using serialize

So I have built a rather large data structure which cannot easily be turned into a relational database format. Using this data structure the requests I make are very fast but it takes about 4-5 seconds to load into memory. What I want is to load it into memory once, then have it sit there and be able to quickly answer individual requests which of course is not the normal flow with the php scripts I have written normally. Is there any good way to do this in php? (again no using a database, it has to use this specialized precomputed structure which takes a long time to load into memory.)

EDIT: This tutorial kind of gives what I want but it is pretty complicated and I was hoping someone would have a more elegant solution. As he says in the tutorial the whole problem is that naturally php is stateless.

6
  • Create a webserver in PHP (exists already), load the structure when the server starts, then answer requests. The data is already available. Commented Dec 15, 2011 at 2:28
  • Sorry that I am new to this side of PHP... what do you mean by create a webserver in PHP? The setup now is a user goes to a page and apache runs the php script for that page... Commented Dec 15, 2011 at 2:39
  • 1
    @hackartist nanoweb.si.kz this... it sits on port 80 (or whatever you set it up to) INSTEAD of apache. So you IN PHP get the request and process it. The one problem I see with this is that PHP doesn't have threads... so for concurrency it has to fork itself (which is fine, as long as you know what you are doing..) Commented Dec 15, 2011 at 2:59
  • 3
    What about storing the data in APC (php.net/manual/en/apc.configuration.php)? Then you are essentially just working in memory. Commented Dec 15, 2011 at 3:12
  • Thanks for the hint about APC.. this looks promising for what I want Commented Dec 15, 2011 at 3:25

1 Answer 1

1

You absolutely must do something like what your linked tutorial proposes.

No PHP state persist between requests. This is by design.

Thus you will need some kind of separate long-running process and thus some kind of IPC method, or else you need a better data structure you can load piecemeal.

If you really can't put this into a relational database (such as sqlite--it doesn't have to be a process database), explore using some other kind of database, such as a file-based key-value store.

Note that it is extremely unlikely that any long-running process you write, in any language, will be faster, easier, or better than getting this data structure of yours into a real database, relational or otherwise! Get your data structure into a database! It's the easiest among your possible paths.

Another thing you can do is just make loading your data structure as quick as possible. You can serialize it to a file and then deserialize the file; if that is not fast enough you can try igbinary, which is a much-faster-than-standard-php serializer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggestion and I will try to refactor my data structure if I can't find another solution. My data structure is a large (problem specific) index kind of like a suffix tree and it would be much faster to have it in memory rather than try to be following paths in a database even if I could come up with a good representation for it in the relational model. However, the more I think about it, I don't really need the service just the data structure, to be persistent. I could still have the php request model do its thing if the data structure was already quick to access in memory.
You're not understanding--there's no way to get that data structure into the PHP process space persistently! The process space is cleared after every request. You need some serialization step, either IPC or memory-mapped file or database or whatever. You can't load the whole thing into native PHP objects and just keep it around forever.
APC is a good suggestion, BTW, but you still need a serialization-deserialization step. You are essentially using APC as an in-memory database.
OK fair enough. Thank you for helping me understand this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.