storing a hashmap permanently

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

storing a hashmap permanently

Post 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???
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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. 8)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use file_put_contents() with var_export().. making it possible to include. :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
Post Reply