For my site/application I would like to have something like "Application Preferences" that are during run-time stored in memory.
These would be basically generic (rather long-term settings) that are used by multiple scripts on multiple places.
Example: Maximum size of file that can be uploaded for the site.
Several solutions came to my mind, but none of these really completely fulfill the needs:
Having the app prefs only in DB -> the disadvantage is that the DB would need to be queried for the same data basically all the time (e.g. several times within one script).
Normal variables/arrays -> as I understand these are instantiated for each script, for each user so this would take a lot of memory
sessions -> again, this is created for each and every user thus again consuming the server resources more that is trully needed (since the data is same for all users)
The bottom line is I would like to have the preferences in memory (for fast access), but don't want them to be instantiated multiple times (to optimize the memory consumption).
If anyone was addressing this before, I'd be grateful for any advice or suggestions.
One of the best storage settings mechanism is use php array. use default settings as you wish. merge it with user saved array in another place and php will handle fast access using APC opcache code.
<?php
$defautSetting = array(
'foo' => 1,
'baz' => false,
);
$usedSetting = array_merge(
$defautSetting,
require '/path/to/user/saved/settings.php',
);
var_dump($usedSetting['baz']); // true
And user settings file.
<?php
// /settings.php
return array(
'baz' => true,
);
Follow fast and good engineered framework like zendframe work. They will give you better view of many mechanism like this.
I wrote a simple CMS once, with a table called settings, with two fields, key, and value.
I query on the first time I need that setting, via the key, and then store the key and value in an array.
You only use one query per setting, but you could do one function at the top of all pages, with one query and a loop.