目录
Redis简介
Redis是一个key-value存储系统。和Memcached类似,它支持丰富的数据类型,最为常用的数据类型主要有五种,分别是:string(字符串)、 hash(哈希)、list(链表)、set(集合)和zset(有序集合)。Redis通常将数据存储于内存中,或被配置为使用虚拟内存。与memcached不同的是,redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,以此来达到数据持久化的目的,并且在此基础上实现了master-slave(主从)同步。当前 Redis的应用已经非常广泛,国内像新浪、淘宝,国外像 Flickr、Github等均在使用Redis的缓存服务。
环境要求
- Windows 64位操作系统
- php-7.2.2-Win32-VC15-x64
- Redis-x64-4.0.2.2
- php_redis-4.0.2-7.2-ts-vc15-x64
- php_igbinary-2.0.7-7.2-ts-vc15-x64(php_redis扩展依赖)
资源下载地址
php7.2、Redis、php-redis、php-igbinary
注意事项:windows环境下,https://github.com/MicrosoftArchive/redis/releases资源列表里面能找到的最新版本为3.2,但是却没有资源库能够提供对应版本的php-redis扩展,真TM扯淡。
安装Redis
将下载下来的redis压缩包Redis-x64-4.0.2.2.zip解压至某个目录,如图:
根据自己所采用的启动方式,修改对应的配置文件。另外,出于安全方面的考虑,通常需要为redis设置连接密码,找到配置文件中含有requirepass foobared字样的行,在其下面新增一行,如图:
设置Redis连接密码后,客户端在连接Redis时需要通过AUTH <password>命令提供密码。
启动Redis
此处有两种启动方式,通过命令行和通过系统服务的方式。
命令行方式启动:
切换到redis的根目录,比如根目录为 D:\myplatform\Redis,然后执行命令 redis-server redis.windows.conf 即可启动redis服务,如图:
注意:此窗口请勿关闭,否则redis服务就会停止了。
系统服务方式启动:
cmd窗口下,切换到redis根目录,执行如下命令:
redis-server --service-install redis.windows-service.conf --loglevel verbose --service-name "Redis Service 4.0" --port 10001
安装成功后,即可通过windows服务界面的方式启动redis了。
客户端测试
新开一个cmd窗口,切换到redis根目录,执行命令 redis-cli,如果能看到redis的服务器地址和端口,就说明客户端已成功连接。根据redis启动方式的不同,选择相应的命令。
命令行方式:redis-cli
系统服务方式:redis-cli -h 127.0.0.1 -p 10001
安装php-redis扩展
将php_redis-4.0.2-7.2-ts-vc15-x64.zip压缩包中的php_redis.dll文件,以及php_igbinary-2.0.7-7.2-ts-vc15-x64.zip压缩包中的php_igbinary.dll文件,拷贝到php安装目录的ext目录下,如图:
然后,修改php.ini配置文件,在里面加入如下扩展:
extension=php_igbinary.dll
extension=php_redis.dll
注意:php_igbinary扩展要放在php_redis扩展的前面。
最后,重启apache服务。
php客户端使用
封装redis工具类,代码如下:
class RedisUtil {
// redis实例对象
private $redis;
// redis服务器列表
private static $serverList = array();
public function __construct($config=array()) {
$this->initRedis($config);
}
/**
* 初始化redis连接实例,每个redis服务器只连接一次
* @param array $config 参数示例:array('host' => '127.0.0.1','port' => 10001,'timeOut' => 10,'password' => '123456')
* @return mixed
*/
private function initRedis($config=array()) {
if (array_key_exists($config['host'],self::$serverList)) {
return $this->redis;
}
$this->redis = new Redis();
// 连接redis服务端
$this->redis->connect($config['host'], $config['port'],$config['timeOut']);
// 密码认证
if (isset($config['password']) && $config['password']) {
$this->redis->auth($config['password']);
}
// 连接失败时,重置$redis实例为null
if (strcasecmp($this->redis->ping(),'+PONG') != 0) {
$this->redis = null;
}
if ($this->redis) {
self::$serverList[$config['host']] = true;
}
if ($this->redis == null) {
die('Redis 连接异常!');
}
}
/**
* 设置字符串缓存
* @param $key
* @param $value
*/
public function set($key,$value) {
$this->redis->set($key, $value);
}
/**
* 获取字符串缓存
* @param $key
* @return bool|string
*/
public function get($key) {
return $this->redis->get($key);
}
/**
* 设置队列缓存(数据左进右出)
* 应用场景:简单的消息系统、最新列表、秒杀/抢购等
* @param $key 队列key名称
* @param $items 队列数据,支持单个数据和一维数组格式
* @return mixed $items为单个数据时,返回被添加元素在队列的第几位(从1开始)
*/
public function setLeftList($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->lPush($key, $item);
}
} else {
return $this->redis->lPush($key, $items);
}
}
/**
* 设置队列缓存(数据右进左出)
* 应用场景:简单的消息系统、最新列表、秒杀/抢购等
* @param $key 队列key名称
* @param $items 队列数据,支持单个数据和一维数组格式
* @return mixed $items为单个数据时,返回被添加元素在队列的第几位(从1开始)
*/
public function setRightList($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->rPush($key, $item);
}
} else {
return $this->redis->rPush($key, $items);
}
}
/**
* 从队列左侧弹出一个
* @param $key
* @return mixed
*/
public function popLeft($key) {
return $this->redis->lPop($key);
}
/**
* 从队列右侧弹出一个
* @param $key
* @return mixed
*/
public function popRight($key) {
return $this->redis->rPop($key);
}
/**
* 获取队列缓存
* @param $key
* @param int $start 开始位置,默认为队列第一个元素
* @param int $end 结束位置,默认为队列最后一个元素
* @return mixed
*/
public function getList($key,$start=0,$end=-1) {
return $this->redis->lRange($key, $start, $end);
}
/**
* 获取队列中的元素个数
* @param $key
* @return mixed
*/
public function getListSize($key) {
return $this->redis->lLen($key);
}
/**
* 删除队列中指定个数的元素值
* @param $key
* @param $value 元素值
* @param int $count 删除的个数 0 全部 | 正数 从左侧删除指定个数 | 负数 从右侧删除指定个数
*/
public function removeList($key,$value,$count=0) {
$this->redis->lRem($key,$value,$count);
}
/**
* 设置hash表缓存
* 应用场景:对象存储
* @param $key hash表key名称
* @param $items hash表数据,一维数组,键值对格式,如:array('key1' => 'item1','key2' => 'item2')
*/
public function setHash($key,$items) {
$this->redis->hmset($key, $items);
}
/**
* 获取hash表缓存
* @param $key hash表key名称
* @param null $hashKey hash表中的key,省略该参数时,获取所有数据
* @return mixed
*/
public function getHash($key,$hashKey=null) {
if ($hashKey) {
return is_string($hashKey) ? $this->redis->hGet($key,$hashKey) : $this->redis->hmget($key, $hashKey);
}
return $this->redis->hGetAll($key);
}
/**
* 获取hash表中的所有key
* @param $key hash表key名称
* @return mixed
*/
public function getHashKeys($key) {
return $this->redis->hKeys($key);
}
/**
* 删除hash
* @param $key hash表key名称
* @param $hashKey hash表中的key,支持单个数据和一维数组
*/
public function removeHash($key,$hashKey) {
if (is_array($hashKey)) {
foreach ($hashKey as $item) {
$this->redis->hDel($key,$item);
}
} else {
$this->redis->hDel($key,$hashKey);
}
}
/**
* 检查指定的hash表中是否存在某个key
* @param $key hash表key名称
* @param $hashKey hash表中的key
* @return mixed true 存在 | false 不存在
*/
public function hashExists($key,$hashKey) {
return $this->redis->hExists($key,$hashKey);
}
/**
* 设置set无序集合
* @param $key 集合key名称
* @param $items 集合数据,支持单个数据和一维数组
*/
public function setCollection($key,$items) {
is_array($items) ? $this->redis->sAddArray($key,$items) : $this->redis->sAdd($key,$items);
}
/**
* 获取set无序集合
* @param $key
* @return mixed
*/
public function getCollection($key) {
return $this->redis->sMembers($key);
}
/**
* 删除set无序集合中的元素
* @param $key 集合key名称
* @param $items 待删除的元素,支持单个数据和一维数组
*/
public function removeCollection($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->sRem($key,$item);
}
} else {
$this->redis->sRem($key,$items);
}
}
/**
* 检查set无序集合中是否包含某个元素
* @param $key 集合key名称
* @param $item 元素
* @return mixed true 包含 | false 不包含
*/
public function collectionExists($key,$item) {
return $this->redis->sIsMember($key, $item);
}
/**
* 获取set无序集合中元素的个数
* @param $key 集合key名称
* @return mixed
*/
public function getCollectionCount($key) {
return $this->redis->sCard($key);
}
/**
* 获取两个set无序集合的交集
* @param $key1
* @param $key2
* @return mixed
*/
public function getCollectionInter($key1,$key2) {
return $this->redis->sInter($key1, $key2);
}
/**
* 获取两个set无序集合的并集
* @param $key1
* @param $key2
* @return mixed
*/
public function getCollectionUnion($key1,$key2) {
return $this->redis->sUnion($key1, $key2);
}
/**
* 获取两个set无序集合的差集
* @param $key1
* @param $key2
* @return mixed
*/
public function getCollectionDiff($key1,$key2) {
return $this->redis->sDiff($key1, $key2);
}
/**
* 设置set有序集合
* @param $key 集合key名称
* @param $items 集合数据,格式:array('score1' => 'item1','score2' => 'item2')
*/
public function setZCollection($key,$items) {
foreach ($items as $score => $item) {
$this->redis->zAdd($key, $score, $item);
}
}
/**
* 获取set有序集合
* 应用场景:排行榜
* @param $key 集合key名称
* @param string $sort 排序方式,asc|desc,默认为增序
* @param int $start 开始位置,默认为集合第一个元素
* @param int $end 结束位置,默认为集合最后一个元素
* @return mixed
*/
public function getZCollection($key,$sort='asc',$start=0,$end=-1) {
if ($sort == 'asc') {
// 成员按分数值递增排序,分数值相同的则按元素字典序来排序
return $this->redis->zRange($key, $start, $end);
}
// 成员按分数值递减排序,分数值相同的则按元素字典序的逆序来排序
return $this->redis->zRevRange($key, $start, $end);
}
/**
* 删除set有序集合中的元素
* @param $key 集合key名称
* @param $items 待删除的元素,支持单个数据和一维数组
*/
public function removeZCollection($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->zRem($key,$item);
}
} else {
$this->redis->zRem($key,$items);
}
}
/**
* 增加set有序集合中指定元素的score
* 应用场景:计数器、统计点击数等
* @param $key 集合key名称
* @param $item 元素
* @param int $num 增量,默认为1
*/
public function zCollectionIncr($key,$item,$num=1) {
$this->redis->zIncrBy($key, $num, $item);
}
/**
* 检查set有序集合中是否包含某个元素
* @param $key 集合key名称
* @param $item 元素
* @return bool true 包含 | false 不包含
*/
public function zCollectionExists($key,$item) {
if ($this->redis->zScore($key, $item) !== false) {
return true;
}
return false;
}
/**
* 获取set有序集合中元素的个数
* @param $key 集合key名称
* @return mixed
*/
public function getZCollectionCount($key) {
return $this->redis->zCard($key);
}
}
使用方式:
$config = array(
'host' => '127.0.0.1', // redis 服务器地址
'port' => 10001, // redis 服务器端口号
'timeOut' => 10, // redis 客户端连接超时时间
'password' => '123456' // redis 客户端连接密码
);
$redisUtil = new RedisUtil($config);
根据需要,调用相应的方法即可。
Redis相关完整软件包
下载地址:Redis相关软件包