Page 1 of 1
storing a hashmap permanently
Posted: Thu Aug 23, 2007 4:50 am
by raghavan20
hello, i am looking to store something like an hashmap(set of key:value pairs) but i do not really want to save it in mysql db. i want to keep this as close as possible to php code in file system. it needs to be stored permanently so that it can be retrieved in subsequent requests. the value wont be big but the hashmap set may grow. there is a very good chance of too many retrievals and deletions happening on it. so, what is the best way to do it; i do not really want to do manual file related operations (retrieving, finding, deleting, saving, etc) as they would be slow. any readymade solutions available? or should i use sqlite?
Thanks for your suggestions.
EDIT: is there is anything available from zend framework???
Posted: Thu Aug 23, 2007 6:35 am
by Kieran Huggins
This sounds like a job for..... MySQL! Seriously, that's pretty much a clear-cut case of when to use a DB. Performance wise it's way more expensive to do just about anything else. DB engines are optimized to all hell.
If you're looking to keep the app portable, have you considered SQLite?
If you're looking to avoid a bunch of SQL, you could always whip up a simple DAO for your hash (and have it handle the SQL).
Or you could do both and be too cool for school.

Posted: Thu Aug 23, 2007 7:28 am
by Chris Corbyn
To all intent and purpose, arrays are equivalent to HashMaps in Java. You can serialize an array easily enough:
Code: Select all
$data = array();
$data["key1"] = "value1";
$data["key2"] = "value2";
file_put_contents("data.sore", serialize($data));
And to get it back:
Code: Select all
$data = unserialize(file_get_contents("data.store"));
I could work with that... K.I.S.S

Posted: Thu Aug 23, 2007 7:43 am
by raghavan20
d11wtq wrote:To all intent and purpose, arrays are equivalent to HashMaps in Java. You can serialize an array easily enough:
Code: Select all
$data = array();
$data["key1"] = "value1";
$data["key2"] = "value2";
file_put_contents("data.sore", serialize($data));
And to get it back:
Code: Select all
$data = unserialize(file_get_contents("data.store"));
I could work with that... K.I.S.S

i hope this would be as fast as i do expect. thanks both of you guys.
Posted: Thu Aug 23, 2007 9:11 am
by feyd
I'd use
file_put_contents() with
var_export().. making it possible to include.

Posted: Thu Aug 23, 2007 9:17 am
by Kieran Huggins
I know the php manual user comments used to be stored in a separate set of files as base64-encoded strings in an "ini file" structure. Not that I advocate this method, but it served them well for years.
You could also store the hash as a serialized object, though I guess an array would be simpler in that case.
Posted: Fri Aug 24, 2007 1:32 pm
by Weirdan
d11wtq wrote:
I could work with that... K.I.S.S

you should add some locking to prevent reads when a write is being done (and vice versa) though
Posted: Fri Aug 24, 2007 4:03 pm
by Chris Corbyn
Weirdan wrote:d11wtq wrote:
I could work with that... K.I.S.S

you should add some locking to prevent reads when a write is being done (and vice versa) though
True

flock() for anyone who's wondering. (Or just use a database with row-level locking)