/** * 增加计数 * @param $key * @param int $expire * @param int $incrby * @return bool */ function addCount($key, $expire = 86400, $incrby = 1) { $key = self::PREFIX . $key; //增加计数 if ($this->redis->exists($key)) { return $this->redis->incr($key, $incrby); } else { return $this->redis->set($key, $incrby, $expire); } }
<?php header("content-type:text/html;charset=utf-8"); $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); $mywatchkey = $redis->get("mywatchkey"); $rob_total = 10; //抢购数量 if ($mywatchkey < $rob_total) { $redis->watch("mywatchkey"); $redis->multi(); //设置延迟,方便测试效果。 sleep(5); //插入抢购数据 $redis->hSet("mywatchlist", "user_id_" . mt_rand(1, 9999), time()); $redis->set("mywatchkey", $mywatchkey + 1); $rob_result = $redis->exec(); if ($rob_result) { $mywatchlist = $redis->hGetAll("mywatchlist"); echo "抢购成功!<br/>"; echo "剩余数量:" . ($rob_total - $mywatchkey - 1) . "<br/>"; echo "用户列表:<pre>"; } else { echo "手气不好,再抢购!"; exit; } } ?>
function login($client_id, $info) { $this->redis->set(self::$prefix . 'client_' . $client_id, serialize($info)); $this->redis->sAdd(self::$prefix . 'online', $client_id); }
public function redis() { var_dump(redis::set('name', 'enychen')); var_dump(redis::get('name')); }
{ } function get($key) { } } class myredis { function __construct($redis) { $this->_redis = $redis; } public function __call($name, $args) { return call_user_func_array(array($this->_redis, $name), $args); } } $redis = new redis(); $redis->connect('127.0.0.1'); $redis->set('key1', rand()); $redis->get('key1'); $myredis = new myredis($redis); $myredis->set('key2', rand()); $myredis->get('key2'); $redis2 = new redis2(); $redis2->connect('127.0.0.1'); $redis2->set('key3', rand()); $redis2->get('key3'); $myredis2 = new myredis($redis2); $myredis2->set('key4', rand()); $myredis2->get('key4');
public function update($key, $data, $ttl = 0) { return $this->_redis->set($key, $data, $ttl); }
#!/usr/bin/php <?php $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); if (!$result) { die("Redis连接失败;\n"); } $set_result = $redis->set('test1', 'test1-info-value'); var_dump($set_result); if (!$set_result) { die("Redis set 操作失败;\n"); } else { die("Redis set 操作成功;\n"); }
<?php $redis = new redis(); $redis->connect('127.0.0.1', 6379); $result = $redis->set('name', "abc1"); var_dump($result); //结果:bool(true) //echo xx; ?>
<?php $con = mysql_connect('localhost', 'root', '123'); mysql_select_db('qxy_istrone'); $sql = 'select ID, post_title, post_content from wp_posts where post_status= "publish" and post_title != "" order by post_date desc limit 30'; $query = mysql_query($sql); mysql_close($con); $redis = new redis(); $redis->pconnect('127.0.0.1'); $real_data = array(); $i = 0; while ($data = mysql_fetch_assoc($query)) { $real_data['{{link_' . $i . '}}'] = 'http://7magic.istrone.com/f/' . $data['ID']; $real_data['{{text_' . $i . '}}'] = $data['post_title']; $redis->set($data['ID'], json_encode(array('{{title}}' => $data['post_title'], '{{content}}' => $data['post_content'], '{{src_url}}' => 'http://istrone.com/?p=' . $data['ID']))); $i++; } $redis->set('home', json_encode($real_data)); $redis_get_binary = $redis->get('home'); echo $redis_get_binary . "\n"; $redis->close();
#!/usr/bin/php <?php // 10s redis set 测试 $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); if (!$result) { die("Redis连接失败;\n"); } $time_limit = 10; $time_start = time(); $time_length = 0; $i = 0; $error_num = 0; while ($time_length <= $time_limit) { $i++; $set_result = $redis->set('test:' . $i, time() . ' - ' . rand(1000, 10000)); if (!$set_result) { $error_num++; } $time_length = time() - $time_start; } echo "Total time : " . $time_limit . " s .\n"; echo "All : " . $i . "\n"; echo "Error : " . $error_num . "\n";
if ($Redis->pconnect($host, $port) == false) { die($Redis->getLastError()); } if ($Redis->auth($user . ":" . $pwd) == false) { die($Redis->getLastError()); } $file = fopen("redis.json", "r"); $arr = array(); while (!feof($file)) { //echo fgets($file). "<br />"; $arr = json_decode(fgets($file), true); $Redis->delete($arr['key']); switch ($arr['type']) { case $Redis::REDIS_STRING: echo "string\n"; $Redis->set($arr['key'], $arr['val']); setTimeout($Redis, $arr['key'], $arr['expire']); break; case $Redis::REDIS_HASH: echo "hash\n"; $Redis->hMset($arr['key'], $arr['val']); setTimeout($Redis, $arr['key'], $arr['expire']); break; case $Redis::REDIS_LIST: echo "list\n"; foreach ($arr['val'] as $v) { $Redis->rPush($arr['key'], $v); } setTimeout($Redis, $arr['key'], $arr['expire']); break; case $Redis::REDIS_SET: