JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: merge ckeditor settings from cms
[wfpl.git] / persistent.php
1 <?php
2
3 # a simple, persistent, key/value store optimized for many reads
4 #
5 # call persistent_{get,set,clear}
6
7 # for internal use
8 function persistent_init() {
9         if (isset($GLOBALS['wfpl_persistent'])) {
10                 return;
11         }
12         $GLOBALS['wfpl_persistent'] = array();
13         $rows = db_get_rows('persistent', 'k,v');
14         foreach ($rows as &$row) {
15                 $GLOBALS['wfpl_persistent'][$row[0]] = json_decode($row[1], true);
16         } unset($row);
17 }
18
19 function persistent_get($k) {
20         persistent_init();
21         if (isset($GLOBALS['wfpl_persistent'][$k])) {
22                 return $GLOBALS['wfpl_persistent'][$k];
23         }
24         return null;
25 }
26 function persistent_set($k, $v) {
27         persistent_init();
28         if (isset($GLOBALS['wfpl_persistent'][$k])) {
29                 if ($GLOBALS['wfpl_persistent'][$k] === $v) {
30                         return;
31                 }
32                 db_update('persistent', 'v', json_encode($v), 'where k=%"', $k);
33         } else {
34                 db_insert('persistent', 'k,v', $k, json_encode($v));
35         }
36         $GLOBALS['wfpl_persistent'][$k] = $v;
37 }
38 function persistent_clear($k) {
39         persistent_init();
40         if (isset($GLOBALS['wfpl_persistent'][$k])) {
41                 db_delete('persistent', 'where k=%"', $k);
42                 unset($GLOBALS['wfpl_persistent'][$k]);
43         }
44 }