[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Copyright 2008 Google Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 require_once "Google/Cache/Abstract.php"; 19 require_once "Google/Cache/Exception.php"; 20 21 /** 22 * A persistent storage class based on the memcache, which is not 23 * really very persistent, as soon as you restart your memcache daemon 24 * the storage will be wiped. 25 * 26 * Will use either the memcache or memcached extensions, preferring 27 * memcached. 28 * 29 * @author Chris Chabot <[email protected]> 30 */ 31 class Google_Cache_Memcache extends Google_Cache_Abstract 32 { 33 private $connection = false; 34 private $mc = false; 35 private $host; 36 private $port; 37 38 public function __construct(Google_Client $client) 39 { 40 if (!function_exists('memcache_connect') && !class_exists("Memcached")) { 41 throw new Google_Cache_Exception("Memcache functions not available"); 42 } 43 if ($client->isAppEngine()) { 44 // No credentials needed for GAE. 45 $this->mc = new Memcached(); 46 $this->connection = true; 47 } else { 48 $this->host = $client->getClassConfig($this, 'host'); 49 $this->port = $client->getClassConfig($this, 'port'); 50 if (empty($this->host) || empty($this->port)) { 51 throw new Google_Cache_Exception("You need to supply a valid memcache host and port"); 52 } 53 } 54 } 55 56 /** 57 * @inheritDoc 58 */ 59 public function get($key, $expiration = false) 60 { 61 $this->connect(); 62 $ret = false; 63 if ($this->mc) { 64 $ret = $this->mc->get($key); 65 } else { 66 $ret = memcache_get($this->connection, $key); 67 } 68 if ($ret === false) { 69 return false; 70 } 71 if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) { 72 $this->delete($key); 73 return false; 74 } 75 return $ret['data']; 76 } 77 78 /** 79 * @inheritDoc 80 * @param string $key 81 * @param string $value 82 * @throws Google_Cache_Exception 83 */ 84 public function set($key, $value) 85 { 86 $this->connect(); 87 // we store it with the cache_time default expiration so objects will at 88 // least get cleaned eventually. 89 $data = array('time' => time(), 'data' => $value); 90 $rc = false; 91 if ($this->mc) { 92 $rc = $this->mc->set($key, $data); 93 } else { 94 $rc = memcache_set($this->connection, $key, $data, false); 95 } 96 if ($rc == false) { 97 throw new Google_Cache_Exception("Couldn't store data in cache"); 98 } 99 } 100 101 /** 102 * @inheritDoc 103 * @param String $key 104 */ 105 public function delete($key) 106 { 107 $this->connect(); 108 if ($this->mc) { 109 $this->mc->delete($key, 0); 110 } else { 111 memcache_delete($this->connection, $key, 0); 112 } 113 } 114 115 /** 116 * Lazy initialiser for memcache connection. Uses pconnect for to take 117 * advantage of the persistence pool where possible. 118 */ 119 private function connect() 120 { 121 if ($this->connection) { 122 return; 123 } 124 125 if (class_exists("Memcached")) { 126 $this->mc = new Memcached(); 127 $this->mc->addServer($this->host, $this->port); 128 $this->connection = true; 129 } else { 130 $this->connection = memcache_pconnect($this->host, $this->port); 131 } 132 133 if (! $this->connection) { 134 throw new Google_Cache_Exception("Couldn't connect to memcache server"); 135 } 136 } 137 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |