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
10 # call persistent_{get,set,clear}
13 function persistent_init() {
14 if (isset($GLOBALS['wfpl_persistent'])) {
17 $GLOBALS['wfpl_persistent'] = array();
18 $rows = db_get_rows('persistent', 'k,v');
19 foreach ($rows as &$row) {
20 $GLOBALS['wfpl_persistent'][$row[0]] = json_decode($row[1], true);
24 function persistent_get($k) {
26 if (isset($GLOBALS['wfpl_persistent'][$k])) {
27 return $GLOBALS['wfpl_persistent'][$k];
31 function persistent_set($k, $v) {
33 if (isset($GLOBALS['wfpl_persistent'][$k])) {
34 if ($GLOBALS['wfpl_persistent'][$k] === $v) {
37 db_update('persistent', 'v', json_encode($v), 'where k=%"', $k);
39 db_insert('persistent', 'k,v', $k, json_encode($v));
41 $GLOBALS['wfpl_persistent'][$k] = $v;
43 function persistent_clear($k) {
45 if (isset($GLOBALS['wfpl_persistent'][$k])) {
46 db_delete('persistent', 'where k=%"', $k);
47 unset($GLOBALS['wfpl_persistent'][$k]);