1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | <?php // // Open Web Analytics - An Open Source Web Analytics Framework // // Copyright 2006 Peter Adams. All rights reserved. // // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // $Id$ // require_once(OWA_BASE_CLASS_DIR.'cache.php'); if ( ! class_exists( 'memcached' ) ) { require_once( OWA_INCLUDE_DIR . 'memcached-client.php' ); } /** * Memcached Based Cache * * @author Peter Adams <peter@openwebanalytics.com> * @copyright Copyright © 2006 - 2011 Peter Adams <peter@openwebanalytics.com> * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.4.0 */ class owa_memcachedCache extends owa_cache { var $mc; /** * Constructor * * Takes cache directory as param * * @param $cache_dir string */ function __construct() { $servers = owa_coreAPI::getSetting( 'base', 'memcachedServers' ); if ( ! $servers ) { owa_coreAPI::notice('No memcached servers found in configuration settings.'); return; } $persistant = owa_coreAPI::getSetting( 'base', 'memcachedPersisantConnections' ); $error_mode = owa_coreAPI::getSetting( 'base', 'error_handler' ); if ( $error_mode === 'development' ) { $debug = true; } else { $debug = false; } $this->mc = new owa_memcachedClient(array( 'servers' => $servers, 'debug' => $debug, 'compress_threshold' => 10240, 'persistant' => $persistant )); return parent::__construct(); } function makeKey($values) { $key = 'owa-'; $key .= $this->cache_id . '-'; $key .= implode('-', $values); return $key; } function getItemFromCacheStore($collection, $id) { $key = $this->makeKey( array( $collection, $id ) ); $item = $this->mc->get( $key ); if ($item) { $this->debug("$key retrieved from memcache."); return $item; } else { $this->debug("$key was not found in memcache."); } } function putItemToCacheStore($collection, $id) { $key = $this->makeKey( array( $collection, $id ) ); $item = $this->cache[$collection][$id]; $expiration = $this->getCollectionExpirationPeriod( $collection ); $ret = $this->mc->replace( $key, $item, $expiration ); if ( $ret ) { $this->debug( "$key successfully replaced in memcache." ); return true; } else { $ret = $this->mc->add( $key, $item ); if ( $ret ) { $this->debug( "$key successfully added to memcache." ); return true; } else { $this->debug( "$key not added/replaced in memcache." ); return false; } } } function removeItemFromCacheStore($collection, $id) { $key = $this->makeKey( array( $collection, $id ) ); $item = $this->cache[$collection][$id]; $ret = $this->mc->delete($key); if ($ret) { $this->debug( "$key successfully deleted from memcache." ); } else { $this->debug( "$key not deleted from memcache."); } } function flush() { owa_coreAPI::notice("Cannot flush Memcache from client."); return true; } } class owa_memcachedClient extends memcached { function _debugprint( $text ) { owa_coreAPI::debug( "memcached: $text" ); } } ?> |