summaryrefslogtreecommitdiff
path: root/devel-utf8/it_cache.class
diff options
context:
space:
mode:
authorNathan Gass2012-03-22 18:18:42 +0000
committerNathan Gass2012-03-22 18:18:42 +0000
commitd59a4921188753dbe4c0161081755a28112c3ef6 (patch)
tree81496414d988f37f1db9d92c9750d888ffa13746 /devel-utf8/it_cache.class
parentca11771e8fad5fef96615df4c44e04b8fb60ac31 (diff)
downloaditools-d59a4921188753dbe4c0161081755a28112c3ef6.tar.gz
itools-d59a4921188753dbe4c0161081755a28112c3ef6.tar.bz2
itools-d59a4921188753dbe4c0161081755a28112c3ef6.zip
Branch itools/devel-utf8 created
Diffstat (limited to 'devel-utf8/it_cache.class')
-rw-r--r--devel-utf8/it_cache.class107
1 files changed, 107 insertions, 0 deletions
diff --git a/devel-utf8/it_cache.class b/devel-utf8/it_cache.class
new file mode 100644
index 0000000..095becc
--- /dev/null
+++ b/devel-utf8/it_cache.class
@@ -0,0 +1,107 @@
+<?php
+/*
+** $Id$
+**
+** Copyright (C) 1995-2008 by the ITools Authors.
+** This file is part of ITools - the Internet Tools Library
+**
+** ITools is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 3 of the License, or
+** (at your option) any later version.
+**
+** ITools is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+**
+** it_cache.class - Caching functions
+*/
+
+class it_cache
+{
+
+static function _defaults($p)
+{
+ $p += array(
+ 'ttl' => 2,
+ 'distributed' => false,
+ 'hostsfile' => '/opt/ultra/etc/memcached.hosts',
+ );
+
+ if (!it::is_live())
+ $p['distributed'] = false; # Always local cache on non-live systems
+
+ return $p;
+}
+
+/**
+ * Get value for specific key from cache. Can be mixed value but no objects.
+ * @param $key Key to get value for
+ * @param $p['distributed'] Use distributed memcache [false]
+ * @return Value for given key or null
+ */
+static function get($key, $p = array())
+{
+ $p = it_cache::_defaults($p);
+
+ if ($result = $GLOBALS['it_cache_local'][$key])
+ ; # Use local copy
+ else if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p)))
+ $result = @$memcache->get($key);
+ else
+ $result = function_exists("apc_fetch") ? apc_fetch($key) : (function_exists("eaccelerator_get") ? eaccelerator_get($key) : null);
+
+ return $result;
+}
+
+/**
+ * Put value for specific key into cache. Can be mixed value but no objects.
+ * @param $key Key to put value with
+ * @param $value Value to put (mixed but no objects allowed)
+ * @param $p['distributed'] Use distributed memcache [false]
+ * @param $p['ttl'] Time to live for this key/value-pair in seconds
+ * @return Boolean if value was put into cache
+ */
+static function put($key, $value, $p = array())
+{
+ $p = it_cache::_defaults($p);
+
+ if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p)))
+ {
+ $result = @$memcache->set($key, $value, MEMCACHE_COMPRESSED, $p['ttl']);
+ }
+ else
+ {
+ function_exists("eaccelerator_gc") && eaccelerator_gc();
+ $result = function_exists("apc_store") ? apc_store($key, $value, $p['ttl']) : (function_exists("eaccelerator_put") ? eaccelerator_put($key, $value, $p['ttl']) : null);
+ }
+
+ $GLOBALS['it_cache_local'][$key] = $value; # Also store local copy
+
+ return $result;
+}
+
+static function _get_memcache($p)
+{
+ $memcache_id = "it_cache_memcache_" . $p['hostsfile'];
+
+ if (!isset($GLOBALS[$memcache_id]) && class_exists("Memcache", false))
+ {
+ $memcache = new Memcache;
+
+ foreach (array_filter(it::replace(array('[#\s].*' => ""), file($p['hostsfile']))) as $host)
+ $reachable += intval(@$memcache->addServer($host));
+
+ $GLOBALS[$memcache_id] = $reachable ? $memcache : false;
+ }
+
+ return $GLOBALS[$memcache_id];
+}
+
+}
+
+?>