3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
8 # a simple, persistent, key/value store optimized for many reads and small data
10 # call persistent_{get,set,clear}
13 # IF YOU CHANGE THIS: change the version in db_upgrade() too
14 function persistent_init() {
15 if (isset($GLOBALS['wfpl_persistent'])) {
18 $GLOBALS['wfpl_persistent'] = array();
19 $rows = db_get_assocs('wfpl_persistent', 'k,v');
20 foreach ($rows as &$row) {
21 $GLOBALS['wfpl_persistent'][$row['k']] = json_decode($row['v'], true);
25 function persistent_get($k) {
27 if (isset($GLOBALS['wfpl_persistent'][$k])) {
28 return $GLOBALS['wfpl_persistent'][$k];
32 function persistent_set($k, $v) {
34 if (isset($GLOBALS['wfpl_persistent'][$k])) {
35 if ($GLOBALS['wfpl_persistent'][$k] === $v) {
39 db_replace('wfpl_persistent', 'k,v', $k, json_encode($v));
40 $GLOBALS['wfpl_persistent'][$k] = $v;
42 function persistent_clear($k) {
44 if (isset($GLOBALS['wfpl_persistent'][$k])) {
45 db_delete('persistent', 'where k=%"', $k);
46 unset($GLOBALS['wfpl_persistent'][$k]);
50 function persistent_invalidate_cache() {
51 if (isset($GLOBALS['wfpl_persistent'])) {
52 unset($GLOBALS['wfpl_persistent']);