Redis常见7种使用场景

1、缓存数据

最常用,对经常需要查询且变动不是很频繁的数据 常称作热点数据。

<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$strCacheKey = 'Test_bihu';

//字符串应用
$arrCacheData = [
    'name' => 'job',
    'sex' => '男',
    'age' => '30'
];
$redis->set($strCacheKey, json_encode($arrCacheData));
$redis->expire($strCacheKey, 30); # 设置30秒后过期
$json_data = $redis->get($strCacheKey);
$data = json_decode($json_data);
print_r($data->age); //输出数据

//哈希类型应用
$arrWebSite = [
    'google' => [
        'google.com',
        'google.com.hk'
    ],
];
$redis->hSet($strCacheKey, 'google', json_encode($arrWebSite['google']));
$json_data = $redis->hGet($strCacheKey, 'google');
$data = json_decode($json_data);
print_r($data); //输出数据

 简单队列实战

<?php
$redis->connect('127.0.0.1', 6379);
$strQueueName = 'Test_bihu_queue';

//进队列
$redis->rpush($strQueueName, json_encode(['uid' => 1, 'name' => 'Job']));
$redis->rpush($strQueueName, json_encode(['uid' => 2, 'name' => 'Tom']));
$redis->rpush($strQueueName, json_encode(['uid' => 3, 'name' => 'John']));
echo "---- 进队列成功 ---- <br /><br />";

//查看队列
$strCount = $redis->lrange($strQueueName, 0, -1);
echo "当前队列数据为: <br />";
print_r($strCount);

//出队列
$redis->lpop($strQueueName);
echo "<br /><br /> ---- 出队列成功 ---- <br /><br />";

//查看队列
$strCount = $redis->lrange($strQueueName, 0, -1);
echo "当前队列数据为: <br />";
print_r($strCount);


$key = 'bills:'.$month;
if ($redis->exists($key)) {
    $json = $redis->lrange($key, 0, -1);
    $result = array_map(function ($v) {
        return json_decode($v, true);
    }, $json);
} else {
    $sql = "select * from aa_bills where month='$month'";
    $result = $this->conn->fetchAll($sql);
    foreach ($result as $info) {
        $redis->rpush($key, json_encode($info));
    }
    $redis->expire($key, 86400);
}

2、消息队列

相当于消息订阅系统,比如ActiveMQ、RocketMQ。如果对数据有较高一致性要求时,还是建议使用MQ

简单发布订阅实战

<?php
//以下是 pub.php 文件的内容 cli下运行
ini_set('default_socket_timeout', -1);
$redis->connect('127.0.0.1', 6379);
$strChannel = 'Test_bihu_channel';

//发布
$redis->publish($strChannel, "来自{$strChannel}频道的推送");
echo "---- {$strChannel} ---- 频道消息推送成功~ <br/>";
$redis->close();

 php.exe -f sub.php

<?php
//以下是 sub.php 文件内容 cli下运行
ini_set('default_socket_timeout', -1);
$redis->pconnect('127.0.0.1', 6379); //优化性能
$redis->setOption(Redis::OPT_READ_TIMEOUT, -1);//subscribe操作出现timeout的问题
$strChannel = 'Test_bihu_channel';

//订阅
echo "---- 订阅{$strChannel}这个频道,等待消息推送...----  <br/><br/>";
$redis->subscribe([$strChannel], 'callBackFun');
function callBackFun($redis, $channel, $msg) {
    print_r([
        'redis' => $redis,
        'channel' => $channel,
        'msg' => $msg
    ]);
}

3、计数器

比如统计点击率、点赞率,Redis具有原子性,可以避免并发问题。

<?php
$redis->connect('127.0.0.1', 6379);
$strKey = 'Test_bihu_comments';

//设置初始值
$redis->set($strKey, 0);

$redis->INCR($strKey); //+1
$redis->DECR($strKey); //-1
$redis->INCR($strKey); //+1

$strNowCount = $redis->get($strKey);

echo "---- 当前数量为{$strNowCount}。 ---- ";

 4、排行榜实战(有序集当类型

<?php
$redis->connect('127.0.0.1', 6379);
$strKey = 'Test_bihu_score';

//存储数据
$redis->zadd($strKey, '50', json_encode(['name' => 'Tom']));
$redis->zadd($strKey, '70', json_encode(['name' => 'John']));
$redis->zadd($strKey, '90', json_encode(['name' => 'Jerry']));
$redis->zadd($strKey, '30', json_encode(['name' => 'Job']));
$redis->zadd($strKey, '100', json_encode(['name' => 'LiMing']));

$dataOne = $redis->ZREVRANGE($strKey, 0, -1, true);
echo "---- {$strKey}由大到小的排序 ---- <br /><br />";
print_r($dataOne);

$dataTwo = $redis->ZRANGE($strKey, 0, -1, true);
echo "<br /><br />---- {$strKey}由小到大的排序 ---- <br /><br />";
print_r($dataTwo);

5、

每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁。

<?php
/**
 * 获取锁
 * @param  String $key 锁标识
 * @param  Int $expire 锁过期时间
 * @return Boolean
 */
public function lock($key = '', $expire = 5) {
    $is_lock = $this->_redis->setnx($key, time() + $expire);
    //不能获取锁
    if (!$is_lock) {
        //判断锁是否过期
        $lock_time = $this->_redis->get($key);
        //锁已过期,删除锁,重新获取
        if (time() > $lock_time) {
            unlock($key);
            $is_lock = $this->_redis->setnx($key, time() + $expire);
        }
    }

    return $is_lock ? true : false;
}

/**
 * 释放锁
 * @param  String $key 锁标识
 * @return Boolean
 */
public function unlock($key = '') {
    return $this->_redis->del($key);
}

// 定义锁标识
$key = 'Test_bihu_lock';

// 获取锁
$is_lock = lock($key, 10);
if ($is_lock) {
    echo 'get lock success<br>';
    echo 'do sth..<br>';
    sleep(5);
    echo 'success<br>';
    unlock($key);
} else { //获取锁失败
    echo 'request too frequently<br>';
}

简单事务的乐观锁实战

每次去拿数据的时候都认为别人不会修改,所以不会上锁。watch命令会监视给定的key,当exec时候如果监视的key从调用watch后发生过变化,则整个事务会失败。也可以调用watch多次监视多个key。这样就可以对指定的key加乐观锁了。注意watch的key是对整个连接有效的,事务也一样。

<?php
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('127.0.0.1', 7379);
$redis->watch("mywatchlist"); //监听 
$len = $redis->hlen("mywatchlist");
$rob_total = 100; //抢购数量
if ($len < $rob_total) {
    $redis->multi();
    $redis->hSet("mywatchlist", "user_id_" . mt_rand(1, 999999), time());
    $rob_result = $redis->exec();
    //file_put_contents("log.txt", $len . PHP_EOL, FILE_APPEND);
    if ($rob_result) {
        $mywatchlist = $redis->hGetAll("mywatchlist");
        echo '抢购成功' . PHP_EOL;
        echo '剩余数量:' . ($rob_total - $len - 1) . PHP_EOL;
        echo '用户列表:' . PHP_EOL;
        print_r($mywatchlist);
    } else {
        exit('手气不好,再抢购!');
    }
} else {
    exit('已卖光');
}

6、电商网站信息

大型电商平台初始化页面数据的缓存。比如去哪儿网购买机票的时候首页的价格和你点进去的价格会有差异。

一、页面缓存

对于通用页面,一般使用页面缓存,直接将需要返回给用户的页面存储到redis中,然后当有人再次访问时直接出redis中取出直接返回给用户,不在进行其他操作,节省系统资源。

  /* 做页面缓存
     * */
    @GetMapping(value = "/product_list",produces = "text/html")
    @ResponseBody
    public String product_list(Model model, User user,
                               HttpServletResponse response,
                               HttpServletRequest request,
                               @RequestParam(value = "pageNo",defaultValue = "1")int pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "6")int pageSize){
                               
        model.addAttribute("user",user);
        PageInfo<ProductVo> page = productService.listProcuctVo(pageNo,pageSize);
        model.addAttribute("pageInfo",page);
        
        //取缓存
        String html = (String) redisService.get("product_list");
        System.out.println(html);
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        
        //手动渲染
        IWebContext ctx = new WebContext(request,response,request.getServletContext(),
                request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("product_list",ctx);
        if(!StringUtils.isEmpty(html)){
            redisService.set_Time("product_list",html,60);
        }
        return html;
    }

二、url缓存

url缓存是一种特殊的页面缓存,原因在于他有特殊的参数信息,对应不同的页面,比如一个商品的详情页,就需要url缓存,因为不同的商品有不同的信息,使用页面缓存并不能实现这一功能。

/* 商品详情页
 * 做url缓存
 * */
    @RequestMapping(value = "/product_detail/{productId}",produces = "text/html")
    @ResponseBody
    public String product_detail(Model model, User user,
                                 @PathVariable("productId") long productId,
                                 HttpServletRequest request,
                                 HttpServletResponse response){
       
        //取缓存
        String html = (String) redisService.get("product_detail"+productId);
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user",user);
        ProductVo productVo = productService.selectProductById(productId);
        model.addAttribute("product",productVo);
        long startTime = productVo.getStart_date().getTime();
        long endTime = productVo.getEnd_date().getTime();
        long nowTime = System.currentTimeMillis();

        int miaoshaStatus = 0;
        int remainSeconds = 0;
        if( nowTime < startTime){//秒杀未开始
            miaoshaStatus = 0;
            remainSeconds = (int)((startTime-nowTime)/1000);
        }else if(nowTime > endTime){//秒杀结束
            miaoshaStatus = 2;
            remainSeconds = -1;
        }else {//正在进行
            miaoshaStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("miaoshaStatus",miaoshaStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        
        //手动渲染
        IWebContext ctx = new WebContext(request,response,request.getServletContext(),
                request.getLocale(),model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("product_detail",ctx);
        if(!StringUtils.isEmpty(html)){
            redisService.set_Time("product_detail"+productId,html,60);
        }
        return html;
    }

7、热点数据

比如新闻网站实时热点、微博热搜等,需要频繁更新。总数据量比较大的时候直接从数据库查询会影响性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值