summaryrefslogtreecommitdiff
path: root/lib/Kolab/Utils
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2013-02-20 20:21:48 (GMT)
committerThomas Bruederli <bruederli@kolabsys.com>2013-02-20 20:21:48 (GMT)
commit8d4b2846f83412c6125b623a9b2ff98a6787a188 (patch)
tree00d16f0ea84dac526f0b8904487b87ca4deeb191 /lib/Kolab/Utils
downloadiRony-8d4b2846f83412c6125b623a9b2ff98a6787a188.tar.gz
Initial import
Diffstat (limited to 'lib/Kolab/Utils')
-rw-r--r--lib/Kolab/Utils/CacheAPC.php86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/Kolab/Utils/CacheAPC.php b/lib/Kolab/Utils/CacheAPC.php
new file mode 100644
index 0000000..e075876
--- /dev/null
+++ b/lib/Kolab/Utils/CacheAPC.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Kolab\Utils;
+
+/**
+ * Utility class that provides a simple API to local APC cache
+ */
+class CacheAPC
+{
+ private static $instances = array();
+
+ private $prefix = 'kolabdav:';
+ private $ttl = 600; // Default Time To Live
+ private $enabled = false; // APC enabled?
+ private $local = array(); // local in-memory cache
+
+ /**
+ * Singleton getter
+ *
+ * @param string Cache domain used to prefix cache entries
+ * @return object CacheAPC instance for the given domain
+ */
+ public static function get_instance($domain = '')
+ {
+ if (!self::$instances[$domain]) {
+ self::$instances[$domain] = new CacheAPC($domain);
+ }
+
+ return self::$instances[$domain];
+ }
+
+ /**
+ * Private constructor
+ */
+ private function CacheAPC($domain)
+ {
+ if (!empty($domain))
+ $this->prefix = $domain . ':';
+
+ $this->enabled = extension_loaded('apc');
+ }
+
+ /**
+ * Get data from cache
+ */
+ function get($key)
+ {
+ if (isset($this->local[$key])) {
+ return $this->local[$key];
+ }
+
+ if ($this->enabled) {
+ $success = false;
+ $data = apc_fetch($this->prefix . $key, $success);
+ return $success ? $data : null;
+ }
+ }
+
+ /**
+ * Save data to cache
+ */
+ function set($key, $data, $ttl = 0)
+ {
+ $this->local[$key] = $data;
+ if ($this->enabled) {
+ return apc_store($this->prefix . $key, $data, $ttl ?: $this->ttl);
+ }
+
+ return true;
+ }
+
+ /**
+ * Deelete a cache entry
+ */
+ function del($key)
+ {
+ unset($this->local[$key]);
+
+ if ($this->enabled) {
+ apc_delete($this->prefix . $key);
+ }
+ }
+
+}
+
+