0

Does PHP provide an option to keep a file open instead of re-opening whenever the script is started?

My script is called around 500 times per second and can be compared to a counter script, which reads 100 lines from the file status.json, does some ++ or -- or resets some values in the file and writes the result back to the same file.

With 500 read/writes per second, the hdd has no issue yet and is not blocking other parallel scripts that r/w the same file. However, this will happen as soon as the number of calls per second increase significantly.

That's why I'd like to keep this file open for r/w by any PHP instances without locks or delay by reopening or closing again and again within each script.

Any tip for this? Has this been solved well with sqlite? If yes, I can switch to sqlite.

ledy
  • 1,527
  • 6
  • 22
  • 33
  • Use a DB that's the way to go, unless you want to save the file in the ram and write on it. – HamZa May 01 '13 at 11:38
  • See answers on http://stackoverflow.com/questions/126917/php-object-caching re memcached and APC – Anigel May 01 '13 at 11:42
  • 1
    At 500 r/w per second I would think about using a ram disk or http://www.sqlite.org/inmemorydb.html you can then sync it back to a backup every x seconds / min / hr depending on the criticalness of the data. If you use PDO to connect to an sqlite db then you may find transactions useful. – Al. May 01 '13 at 11:43

1 Answers1

0

Does PHP provide an option to keep a file open instead of re-opening whenever the script is started?

no.

Has this been solved well with sqlite?

sqlite being the very same file that have to be opened closed as well

You may try some intelligent database that does it's operations mainly in RAM but also dumps it's content to disc periodically for safety. Mongo is an example

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • i went back one step, split the data into separate that are merged periodically with a little delay, once per XY seconds. – ledy May 02 '13 at 22:36