public function test() { $redis = new Redis(); // $redis->connect('127.0.0.1',6379); $redis->set('test', 'hello world!'); echo $redis->get("test"); }
public function goodsClickTimesRedis2Db() { //获取redis中商品id以及访问量次数 $redis = new \Redis(); $redis->connect('127.0.0.1'); $arrkeys = $redis->keys('goods_click_times:*'); $arrvalue = $redis->mget($arrkeys); foreach ($arrkeys as $i => $key) { $key = ltrim($key, 'goods_click_times:'); $m = D('GoodsTimes'); $count = $m->where(array('goods_id' => $key))->count(); if ($count > 0) { $m->where(array('goods_id' => $key))->setInc('times', $arrvalue[$i]); } else { $m->add(array('goods_id' => $key, 'times' => $arrvalue[$i])); } } $redis->del($arrkeys); }
/** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options = array()) { ini_set('default_socket_timeout', -1); //不超时 if (empty($options)) { $options = array('host' => C('KVSTORE_HOST'), 'port' => C('KVSTORE_PORT'), 'timeout' => false, 'persistent' => false, 'prefix' => C('DATA_CACHE_PREFIX'), 'user' => C('DATA_CACHE_USER'), 'pwd' => C('DATA_CACHE_PW'), 'expire' => 12000); } else { $options['host'] = isset($options['host']) ?: C('KVSTORE_HOST'); $options['port'] = isset($options['port']) ?: C('KVSTORE_PORT'); $options['timeout'] = isset($options['timeout']) ?: FALSE; $options['persistent'] = isset($options['persistent']) ?: FALSE; $options['prefix'] = isset($options['prefix']) ?: C('DATA_CACHE_PREFIX'); $options['user'] = isset($options['user']) ?: C('DATA_CACHE_USER'); $options['pwd'] = isset($options['pwd']) ?: C('DATA_CACHE_PW'); $options['expire'] = isset($options['expire']) ?: 12000; } $this->options = $options; $redis = new \Redis(); for ($i = 0; $i < 2; $i++) { if ($redis->pconnect($options['host'], $options['port']) == false) { try { $error = $redis->getLastError(); } catch (\RedisException $ex) { $error = $ex->getMessage(); DILog($error); if ($error != 'Redis server went away') { break; //跳出重试循环 } else { sleep(0.5); //暂停0.5s以后重试连接 DILog('Kvstore: Try Reconnected'); } } // E($redis->getLastError()); } } $redis->setOption(\Redis::OPT_PREFIX, $options['prefix']); /* user:password 拼接成AUTH的密码 */ if ($redis->auth($options['user'] . ':' . $options['pwd']) == false) { $error = $redis->getLastError(); DILog($error); // E($redis->getLastError()); } $this->handler = $redis; }
/** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options = array()) { ini_set('default_socket_timeout', -1); //不超时 if (empty($options)) { $options = array('host' => C('KVSTORE_HOST'), 'port' => C('KVSTORE_PORT'), 'timeout' => false, 'persistent' => false, 'prefix' => C('DATA_CACHE_PREFIX'), 'user' => C('DATA_CACHE_USER'), 'pwd' => C('DATA_CACHE_PW'), 'expire' => 12000); } $this->options = $options; $redis = new \Redis(); if ($redis->pconnect($options['host'], $options['port']) == false) { E($redis->getLastError()); } $redis->setOption(\Redis::OPT_PREFIX, $options['prefix']); /* user:password 拼接成AUTH的密码 */ if ($redis->auth($options['host'] . ':' . $options['pwd']) == false) { E($redis->getLastError()); } $this->handler = $redis; }
/** * 获取Redis实例 * @return Redis */ public static function getInstance() { return Redis::getInstance(self::CACHE_TYPE); }
public function redis($eid = 1) { $redis_server = new Redis(); $data = $redis_server->get("twitter:sg:event:python:test3:" . $eid); $this->ajaxReturn($data); }