JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix db_get_value after mysql->mysqli upgrade
[wfpl.git] / persistent.php
index cc12028..c471322 100644 (file)
@@ -1,18 +1,24 @@
 <?php
 
-# a simple, persistent, key/value store optimized for many reads
+# This program is in the public domain within the United States. Additionally,
+# we waive copyright and related rights in the work worldwide through the CC0
+# 1.0 Universal public domain dedication, which can be found at
+# http://creativecommons.org/publicdomain/zero/1.0/
+
+# a simple, persistent, key/value store optimized for many reads and small data
 #
 # call persistent_{get,set,clear}
 
 # for internal use
+# IF YOU CHANGE THIS: change the version in db_upgrade() too
 function persistent_init() {
        if (isset($GLOBALS['wfpl_persistent'])) {
                return;
        }
        $GLOBALS['wfpl_persistent'] = array();
-       $rows = db_get_rows('persistent', 'k,v');
+       $rows = db_get_assocs('wfpl_persistent', 'k,v');
        foreach ($rows as &$row) {
-               $GLOBALS['wfpl_persistent'][$row[0]] = json_decode($row[1], true);
+               $GLOBALS['wfpl_persistent'][$row['k']] = json_decode($row['v'], true);
        } unset($row);
 }
 
@@ -29,10 +35,8 @@ function persistent_set($k, $v) {
                if ($GLOBALS['wfpl_persistent'][$k] === $v) {
                        return;
                }
-               db_update('persistent', 'v', json_encode($v), 'where k=%"', $k);
-       } else {
-               db_insert('persistent', 'k,v', $k, json_encode($v));
        }
+       db_replace('wfpl_persistent', 'k,v', $k, json_encode($v));
        $GLOBALS['wfpl_persistent'][$k] = $v;
 }
 function persistent_clear($k) {
@@ -42,3 +46,11 @@ function persistent_clear($k) {
                unset($GLOBALS['wfpl_persistent'][$k]);
        }
 }
+
+function persistent_invalidate_cache() {
+       if (isset($GLOBALS['wfpl_persistent'])) {
+               unset($GLOBALS['wfpl_persistent']);
+       }
+       return;
+}
+