JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
upload.php cleanup: really don't make dot files
[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
9 #
10 # call persistent_{get,set,clear}
11
12 # for internal use
13 function persistent_init() {
14         if (isset($GLOBALS['wfpl_persistent'])) {
15                 return;
16         }
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);
21         } unset($row);
22 }
23
24 function persistent_get($k) {
25         persistent_init();
26         if (isset($GLOBALS['wfpl_persistent'][$k])) {
27                 return $GLOBALS['wfpl_persistent'][$k];
28         }
29         return null;
30 }
31 function persistent_set($k, $v) {
32         persistent_init();
33         if (isset($GLOBALS['wfpl_persistent'][$k])) {
34                 if ($GLOBALS['wfpl_persistent'][$k] === $v) {
35                         return;
36                 }
37                 db_update('persistent', 'v', json_encode($v), 'where k=%"', $k);
38         } else {
39                 db_insert('persistent', 'k,v', $k, json_encode($v));
40         }
41         $GLOBALS['wfpl_persistent'][$k] = $v;
42 }
43 function persistent_clear($k) {
44         persistent_init();
45         if (isset($GLOBALS['wfpl_persistent'][$k])) {
46                 db_delete('persistent', 'where k=%"', $k);
47                 unset($GLOBALS['wfpl_persistent'][$k]);
48         }
49 }