JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / persistent.php
1 <?php
2
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/
7
8 # a simple, persistent, key/value store optimized for many reads and small data
9 #
10 # call persistent_{get,set,clear}
11
12 # for internal use
13 # IF YOU CHANGE THIS: change the version in db_upgrade() too
14 function persistent_init() {
15         if (isset($GLOBALS['wfpl_persistent'])) {
16                 return;
17         }
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);
22         } unset($row);
23 }
24
25 function persistent_get($k) {
26         persistent_init();
27         if (isset($GLOBALS['wfpl_persistent'][$k])) {
28                 return $GLOBALS['wfpl_persistent'][$k];
29         }
30         return null;
31 }
32 function persistent_set($k, $v) {
33         persistent_init();
34         if (isset($GLOBALS['wfpl_persistent'][$k])) {
35                 if ($GLOBALS['wfpl_persistent'][$k] === $v) {
36                         return;
37                 }
38         }
39         db_replace('wfpl_persistent', 'k,v', $k, json_encode($v));
40         $GLOBALS['wfpl_persistent'][$k] = $v;
41 }
42 function persistent_clear($k) {
43         persistent_init();
44         if (isset($GLOBALS['wfpl_persistent'][$k])) {
45                 db_delete('persistent', 'where k=%"', $k);
46                 unset($GLOBALS['wfpl_persistent'][$k]);
47         }
48 }
49
50 function persistent_invalidate_cache() {
51         if (isset($GLOBALS['wfpl_persistent'])) {
52                 unset($GLOBALS['wfpl_persistent']);
53         }
54         return;
55 }
56