redis计数器常见的使用场景
<?php
require_once '../../CacheRedis.php';
$server = '127.0.0.1:6379';
$redis = CacheRedis::getInstance($server);
// 简单计数
// 视频播放量
$keyPrefix = 'video:play:count:';
$videoId = 12;
$key = $keyPrefix . $videoId;
//$total = $redis->incr($key);
//var_dump($total);
// 页面访问量
$key2 = 'request:total';
//$total2 = $redis->incr($key2);
//var_dump($total2);
// 投票量,点赞量等等
// 按月/天/小时计数
$key3 = 'request:total:' . date("Ymd");
//var_dump($key3);
//$total3 = $redis->incr($key3);
//$res = $redis->expire($key3, 3600 * 24 * 2);
//var_dump($total3);
// 存db
//$total = $redis->get($key);
//var_dump($total);
// 写db
// 补充
$key4 = 'ps';
$total = $redis->incrBy($key4, 2);
var_dump($total);
//$total = $redis->decr($key4);
//var_dump($total);
//$total = $redis->decrBy($key4, 2);
//var_dump($total);
推荐相关精品课程: redis基础&实战教程

