tags:

views:

45

answers:

3

Hi,

I am creating a site that allows users to search a static text file.

The file is large static data txt file (10mb) that needed to be access/read every time a user searches (which is hopefully often).

Is there a way/technique that loads the content of the file and stored it in memory (and assigned a variable to it) permanently so that it would speed up the process?

A: 

You can use file_get_contents to get the entire file contents into a in-memory string variable.

codaddict
Thanks, that is what I am doing right now. I just want know how to not do that every time the script runs.
Jamex
+1  A: 

Well, nothing could be permanently in RAM ;)

But if you've already APC installed, you can store it in there, or use memcached or Redis.

Using APC is the preferable way, cause you'll get opcode cache as well, which give you a performance boost, and it's just a PHP module. No need to setup another daemon.

racetrack
Thanks, I just don't know how apc works. Assume that the server has apc installed, how does it work? Right now, every time the script runs, I use file_get_contents to load the file into the variable. How do I use apc to "store" the content of the file, what are the php codes to access the stored apc content?
Jamex
`APC` provides two functions, `apc_store()`, and `apc_fetch()`. Something like `apc_store("content", file_get_contents($filename))`, and later you can retrieve it via `apc_fetch("content")`.
racetrack
There's also `apc_exists()` which let you know if desired key is already stored in cache or not.
racetrack
A: 

Have you tried the php shared memory functions? Another possibility might to to use a RAM Disk.

Robin