diff options
author | Urban Müller | 2008-11-27 16:13:42 +0000 |
---|---|---|
committer | Urban Müller | 2008-11-27 16:13:42 +0000 |
commit | be30e42c7430ad6ee54c0577ef688fe83ed78a4a (patch) | |
tree | 8f8a95e1f25f8b27f54de52343c78629277fd6da | |
parent | 681f5a60ed975fef44f77a3ab1d96660562a6542 (diff) | |
download | itools-be30e42c7430ad6ee54c0577ef688fe83ed78a4a.tar.gz itools-be30e42c7430ad6ee54c0577ef688fe83ed78a4a.tar.bz2 itools-be30e42c7430ad6ee54c0577ef688fe83ed78a4a.zip |
memory cache
-rw-r--r-- | it_cache.class | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/it_cache.class b/it_cache.class new file mode 100644 index 0000000..551d103 --- /dev/null +++ b/it_cache.class @@ -0,0 +1,98 @@ +<?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 +{ + +function _defaults($p) +{ + $p += array( + 'ttl' => 2, + 'distributed' => false, + 'hostsfile' => '/opt/ultra/etc/memcached.hosts', + ); + + return $p; +} + +/** + * Get value for specific key from cache. Can be mixed value but no objects. + * @param $key Key to get value for + * @return Value for given key or null + */ +function get($key, $p = array()) +{ + $p = it_cache::_defaults($p); + + if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p))) + $result = @$memcache->get($key); + else + $result = 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['ttl'] Time to live for this key/value-pair + * @return Boolean if value was put into cache + */ +function put($key, $value, $p = array()) +{ + $p = it_cache::_defaults($p); + + if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p))) + { + @$memcache->set($key, $value, 0, $p['ttl']); + } + else + { + function_exists("eaccelerator_gc") && eaccelerator_gc(); + $result = function_exists("eaccelerator_put") ? eaccelerator_put($key, $value, $p['ttl']) : null; + } + + return $result; +} + +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, 11211, true, 1, 1, -1)); + + $GLOBALS[$memcache_id] = $reachable ? $memcache : false; + } + + return $GLOBALS[$memcache_id]; +} + +} + +?> |