I'm writing a small program that does just one task, stores phone numbers that need to be returned, And my goal is to use a single Json file as the database. Now here is my problem, since I want all employees to have access to this program I can't use PHP's json_decode and _encode functions and then use fwrite() because then one employee will be overriding another. So my question is if their is any workaround for this or if someone can suggest a library that would handle this. Any help is appreciated.
8
-
Do you know about mongodb ? This should perfectly fit your needshek2mgl– hek2mgl2013-02-06 22:33:03 +00:00Commented Feb 6, 2013 at 22:33
-
@hek2mgl yes but I'm doing this for fun too, so I'm playing around a littleuser2048702– user20487022013-02-06 22:34:17 +00:00Commented Feb 6, 2013 at 22:34
-
2If you must store everything in a single file, you'll have to use some locking mechanism to ensure two people are not editing / writing concurrently.Ayush– Ayush2013-02-06 22:36:05 +00:00Commented Feb 6, 2013 at 22:36
-
1Why not use sqlite? It's a database in a filePatashu– Patashu2013-02-06 22:37:11 +00:00Commented Feb 6, 2013 at 22:37
-
2this is not software engineering..this is just a bad way to use tools in the way they are not born/made for. So as people yet suggested to you, use sqlite or mongodb or even better just share a document with this numbers (like google docs..)Gianpaolo Di Nino– Gianpaolo Di Nino2013-02-06 22:40:09 +00:00Commented Feb 6, 2013 at 22:40
|
Show 3 more comments
1 Answer
You can handle this by creating a mutex. If you're on Windows, I'd suggest flock, but if you're on *nix, I'd suggest a sys5 semaphore.
As an aside, if you want a much hackier/simpler setup, you can pump the output from json_encode through error_log, as it implements its own mutex.
Another option which is probably easier would be to use SQLite.