php

redis场景应用

2022-06-28  本文已影响0人  ysp123

redis场景应用

redis存储类型:string(字符串)、hash(哈希)、list(列表)、set(无序集合)、sort-set(有序集合) 及 publish(发布) subscribe(订阅)

基本链接

require './redis.php';
$config = [
    'host'=>'127.0.0.1',
    'port'=>6379  
];
$redis = redissql::getInstance($config);

应用:
1、简单的字符存储

$redis->set('name','23423'));
var_dump($redis->get('name'));
var_dump($redis->del('name'));

var_dump($redis->setnx('name','11111111'));

var_dump($redis->mset(['name'=>'11111111','sex'=>'1']));
var_dump($redis->mget(['name','sex']));

2、计数器

var_dump($redis->incr('p'));

3、哈希字符

$redis->hset('book','name','php学习');
$redis->hmset('book',['name'=>'php学习','type'=>'编程']);

$redis->hgetall('book');

4、列表

$redis->lpush('test', 11);
 $redis->lpush('test', 14);
 $redis->lpush('test', 23);
 $redis->lpush('test', 45);
 $redis->lpush('test', 56);

 $result = $redis->sort('test',['sort'=>'desc']);
 var_dump($result);

5、无序集合

6、有序集合

$resilt = $redis->zadd('fenshu', 67, 'user9');
$resilt = $redis->zadd('fenshu', 23, 'user1');
$resilt = $redis->zadd('fenshu', 33, 'user2');
$resilt = $redis->zadd('fenshu', 56, 'user3');

$result = $redis->zrange('fenshu',0,-1);
var_dump($result);

$result = $redis->zrevrange('fenshu',0,-1);
var_dump($result);

7、redis事务

$redis->multi();
$redis->set('g','444444444');
$redis->incr('pro');
var_dump($redis->exec());

8、hash保存用户信息

$user_id = $redis->incr('user_id');
$name="ysp";
$pwd="1234556";
$redis->set('username:'.$name,'user_'.$user_id);
$redis->hmset('user_'.$user_id,['name'=>$name,'pwd'=>$pwd]);

9、hash保存文章信息

//文章保存
$book_id = $redis->incr('book_id');
 $result = $redis->hmset('book_'.$book_id,['id'=>$book_id,'content'=>json_encode(['name'=>'node学习书','author'=>'ddd','type'=>'编程','user'=>'user_1']),'buy_nums'=>5]);
var_dump($result);

//文章读取
$result = $redis->sort('books',['by'=>'book_*->buy_nums','GET'=>['book_*->content','book_*->buy_nums'],'sort'=>'desc']);
 //var_dump($result);
 $temp = [];
 array_walk($result, function($value, $key) use(&$temp){
         //$value = json_decode($value, true);
            $temp[] = json_decode($value, true);
 });
var_dump($temp);

10、模型商品队列购买

//模拟商品写入
for($i=0;$i<50;$i++){
    try{
        $redis->lpush('sale_list', 'pro'.$i);
    }catch(Exception $e){
        echo $e->getMessage();
    }
}

// 固定商品抢购
$res = $redis->lpop('sale_list');
if(!empty($res)){
    $log = "成功!{$res}".PHP_EOL;
    file_put_contents("./test.log",$log,FILE_APPEND);
}else{
    $log = "失败!".PHP_EOL;
    file_put_contents("./test.log",$log,FILE_APPEND);
}

11、计数器抢购商品

  $pro_num = 10;
  $pro_id = 1243;
 $buy_num = $redis->incr($pro_id);
 if((int)$buy_num > $pro_num){
     $log = "失败!".PHP_EOL;
     file_put_contents("./test.log",$log,FILE_APPEND);
 }else{
     $log = "成功下单,请在10秒内支付!".PHP_EOL;
     //order_id  + user_id
     $redis->setex($buy_num."::".$pro_id, 10, "订单延时支付!");
     file_put_contents("./test.log",$log,FILE_APPEND);
 }

12、事务控制并发

$redis->watch('list');
//$len = $redis->hlen('list');
$len = $redis->get('num');
$total = 10;

if($len < $total){
    $redis->multi();
    $redis->hset("list", "user_id_" . mt_rand(1, 999999), time());
    $redis->set('num', $len+1);
    $result = $redis->exec();

    if($result){
        $mywatchlist = $redis->hgetall("list");
        file_put_contents("./test.log",'抢购成功,剩余数量:'.($total - $len - 1),FILE_APPEND);
        print_r($mywatchlist);
        exit;
    }else{
        exit('手气不好,再抢购!');
    }
   
}else {
    exit('已卖光');
}

13、订阅延时支付

$redis->setex('2134141234', 3, '订单延时支付');

$redis->setOption();
$redis->psubscribe(['__keyevent@0__:expired'], 'keyCallback');
function keyCallback($redis, $pattern, $chan, $msg){
    echo "Pattern: $pattern\n";
    echo "Channel: $chan\n";
    echo "Payload: $msg\n\n";
}
上一篇下一篇

猜你喜欢

热点阅读