]> git.mjollnir.org Git - moodle.git/commitdiff
moodlelib: Introducing the cache_flags functions
authormartinlanghoff <martinlanghoff>
Tue, 2 Oct 2007 08:39:44 +0000 (08:39 +0000)
committermartinlanghoff <martinlanghoff>
Tue, 2 Oct 2007 08:39:44 +0000 (08:39 +0000)
These are functions for handling the volatile entries in the
cache_flags table.

 get_cache_flags() - get array of current flags
 set_cache_flag()  - set a single flag
 unset_cache_flag() - unset a single flag
 gc_cache_flags() - garbage-collect expired flags

lib/moodlelib.php

index 5a65aa27b6ae35c0a05a7319a872870ed574e7f2..8edfe63e6948e1afc927db9d4c06a8dc4bcf8e12 100644 (file)
@@ -720,6 +720,97 @@ function unset_config($name, $plugin=NULL) {
     }
 }
 
+/**
+ * Get volatile flags
+ *
+ * @param string $type
+ * @param int    $changedsince
+ * @return records array
+ *
+ */
+function get_cache_flags($type, $changedsince=NULL) {
+
+    $type = addslashes($type);
+
+    $sqlwhere = 'flagtype=\'' . $type . '\' AND expiry >= ' . time();
+    if ($changedsince !== NULL) {
+        $changedsince = (int)$changedsince;
+        $sqlwhere .= ' AND timemodified > ' . $changedsince;
+    }
+    $cf = array();
+    if ($flags=get_records_select('cache_flags', $sqlwhere, '', 'name,value')) {
+        foreach ($flags as $flag) {
+            $cf[$flag->name] = $flag->value;
+        }
+    }
+    return $cf;
+}
+
+
+/**
+ * Set a volatile flag
+ *
+ * @param string $type the "type" namespace for the key
+ * @param string $name the key to set
+ * @param string $value the value to set (without magic quotes) - NULL will remove the flag
+ * @param int $expiry (optional) epoch indicating expiry - defaults to now()+ 24hs
+ * @return bool
+ */
+function set_cache_flag($type, $name, $value, $expiry=NULL) {
+
+
+    $timemodified = time();
+    if ($expiry===NULL || $expiry < $timemodified) {
+        $expiry = $timemodified + 24 * 60 * 60;
+    } else {
+        $expiry = (int)$expiry;
+    }
+
+    if ($value === NULL) {
+        return unset_cache_flag($type,$name);
+    }
+
+    $type = addslashes($type);
+    $name = addslashes($name);
+    $value = addslashes($value);
+    if ($f = get_record('cache_flags', 'name', $name, 'flagtype', $type)) {
+        $f->value        = $value;
+        $f->expiry       = $expiry;
+        $f->timemodified = $timemodified;
+        return update_record('cache_flags', $f);
+    } else {
+        $f = new StdClass;
+        $f->flagtype     = $type;
+        $f->name         = $name;
+        $f->value        = $value;
+        $f->expiry       = $expiry;
+        $f->timemodified = $timemodified;
+        return insert_record('cache_flags', $f);
+    }
+}
+
+/**
+ * Removes a single volatile flag
+ *
+ * @param string $type the "type" namespace for the key
+ * @param string $name the key to set
+ * @uses $CFG
+ * @return bool
+ */
+function unset_cache_flag($type, $name) {
+
+    return delete_records('cache_flags',
+                          'name', addslashes($name),
+                          'flagtype', addslashes($type));
+}
+
+/**
+ * Garbage-collect volatile flags
+ *
+ */
+function gc_cache_flags() {
+    return delete_records_select('cache_flags', 'expiry < ' . time());
+}
 
 /**
  * Refresh current $USER session global variable with all their current preferences.