function cache_replace($key, $data, $expires) { $connection = cache_connect(); $res = memcache_replace($connection, (string) $key, $data, false, (int) $expires); memcache_close($connection); return $res; }
function cache_set($key, $value) { if (!cache_connect()) { return false; } $key = $GLOBALS['config']['service']['server'] . '/' . $key; $op = memcache_replace(object('memcache'), $key, $value, MEMCACHE_COMPRESSED, 60 * 60); if ($op == false) { memcache_add(object('memcache'), $key, $value, MEMCACHE_COMPRESSED, 60 * 60); } }
function SaeMemcacheReplace($key, $value) { //如果长度超出,则取末尾的234位 if (strlen($key) > 234) { $key = substr($key, -234); } $mmc = memcache_init(); if ($mmc == false) { return false; } else { return memcache_replace($mmc, $key, $value); } }
static function do_put($key, $value, $expire_time = 0) { if (self::connect()) { $result = memcache_replace(CGlobal::$memcache_connect_id, md5(AZMemcache::$identifier . $key), $value, MEMCACHE_COMPRESSED, (int) $expire_time); if ($result == false) { if (DEBUG) { self::$set++; CGlobal::$query_debug .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE' align='center'>\r\n\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t <td style='font-size:14px' bgcolor='#EFEFEF'><span style='color:blue'><b>Memcache</b></span></td>\r\n\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t <td style='font-family:courier, monaco, arial;font-size:14px;color:brown'>Set \"{$key}\" (Expire: " . ($expire_time ? "{$expire_time} sec" : " forever") . ") by memcache_set(" . md5(AZMemcache::$identifier . $key) . ");</td>\r\n\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t<td style='font-size:14px' bgcolor='#EFEFEF'></td>\r\n\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t </table><br />\n\n"; } $result = memcache_set(CGlobal::$memcache_connect_id, md5(AZMemcache::$identifier . $key), $value, MEMCACHE_COMPRESSED, (int) $expire_time); } elseif (DEBUG) { self::$replace++; CGlobal::$query_debug .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE' align='center'>\r\n\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t <td style='font-size:14px' bgcolor='#EFEFEF'><span style='color:blue'><b>Memcache</b></span></td>\r\n\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t <td style='font-family:courier, monaco, arial;font-size:14px;color:brown'>Replace \"{$key}\" (Expire: " . ($expire_time ? "{$expire_time} sec" : " forever") . ") by memcache_set(" . md5(AZMemcache::$identifier . $key) . ");</td>\r\n\t\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t\t<td style='font-size:14px' bgcolor='#EFEFEF'></td>\r\n\t\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t\t </table><br />\n\n"; } return $result; } return false; }
public function testReplaceNotThere() { $memcache = new Memcache(); $request = new MemcacheSetRequest(); $item = $request->addItem(); $item->setKey("float"); $item->setValue("2"); $item->setFlags(6); // float $item->setSetPolicy(SetPolicy::REPLACE); $item->setExpirationTime(30); $response = new MemcacheSetResponse(); $response->addSetStatus(SetStatusCode::NOT_STORED); $this->apiProxyMock->expectCall('memcache', 'Set', $request, $response); $this->assertFalse(memcache_replace($memcache, "float", 2.0, null, 30)); $this->apiProxyMock->verify(); }
<?php define('MEMCACHED_HOST', '127.0.0.1'); define('MEMCACHED_PORT', '11211'); define('MEMCACHED_TIMEOUT', '10'); if (!extension_loaded('memcache')) { die('Extension not loaded'); } if ($cr = memcache_connect(MEMCACHED_HOST, MEMCACHED_PORT, MEMCACHED_TIMEOUT)) { $fileContents = 'SSI block added at ' . date('Y/m/d H:i:s', time()); $flag = false; $finalKey = md5(time() + rand(0, 100)) . '.html'; $fileFinalContents = file_get_contents('./lipsum.txt'); //the file does not exists if (!memcache_replace($cr, $finalKey, $fileFinalContents, $flag, 0)) { if (!memcache_add($cr, $finalKey, $fileFinalContents, $flag, 0)) { print 'Unable to add file'; } } echo '<!--#include memcached="' . $finalKey . '" -->'; } else { print 'unable to connect to memcached'; }
/** * 替换对应key的value * * @param mixed $key * @param mixed $var * @param mixed $flag * @param mixed $expire */ public function replace($key, $var, $flag = 0, $expire = 1) { $f = memcache_replace($this->memcache, $key, $var, $flag, $expire); return $f; }
/** * 设置缓存周期 * * @param string $name 缓存名 * @param int $expires 缓存周期 * @return boolean 成功返回true,失败返回false */ public function setExpires($name, $expires) { $val = memcache_get($name); if (!$val) { return false; } return memcache_replace($name, $val, $expires); }
public static function replace($key, $value, $expire = 0) { return \memcache_replace(self::$connection, $key, $value, self::$flags, $expire); }