]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/cache.php
typo fix in en_GB localization (also updated @ translatewiki)
[quix0rs-gnu-social.git] / lib / cache.php
index df6fc364931fb2f7b0734523e76724b8c8b675d5..c09a1dd9f27c75676eb24f634f6c0378547c798d 100644 (file)
@@ -159,6 +159,32 @@ class Cache
         return $success;
     }
 
+    /**
+     * Atomically increment an existing numeric value.
+     * Existing expiration time should remain unchanged, if any.
+     *
+     * @param string  $key    The key to use for lookups
+     * @param int     $step   Amount to increment (default 1)
+     *
+     * @return mixed incremented value, or false if not set.
+     */
+    function increment($key, $step=1)
+    {
+        $value = false;
+        if (Event::handle('StartCacheIncrement', array(&$key, &$step, &$value))) {
+            // Fallback is not guaranteed to be atomic,
+            // and may original expiry value.
+            $value = $this->get($key);
+            if ($value !== false) {
+                $value += $step;
+                $ok = $this->set($key, $value);
+                $got = $this->get($key);
+            }
+            Event::handle('EndCacheIncrement', array($key, $step, $value));
+        }
+        return $value;
+    }
+
     /**
      * Delete the value associated with a key
      *