PHP操作使用Redis

连接

//实例化redis
$redis = new Redis();
//连接
$redis->connect('127.0.0.1', 6379);
//检测是否连接成功
echo "Server is running: " . $redis->ping();
// 输出结果 Server is running: +PONG

string

// 设置一个字符串的值
$redis->set('cat', 111);
//获取一个字符串的值
echo $redis->get('cat'); // 111
// 重复set
$redis->set('cat', 222);
echo $redis->get('cat'); // 222

hash

<?php
  //实例化redis
  $redis = new Redis();
  //连接
  $redis->connect('127.0.0.1', 6379);
  //字典
  //批量设置多个key的值
  $arr = [1=>1, 2=>2, 3=>3, 4=>4, 5=>5];
  $redis->hmset('hash', $arr);
  print_r($redis->hgetall('hash'));echo '<br>';
  // 批量获得额多个key的值
  $arr = [1, 2, 3, 5];
  $hash = $redis->hmget('hash', $arr);
  print_r($hash);echo '<br>';
  //检测hash中某个key知否存在
  echo $redis->hexists('hash', '1');echo '<br>';
  var_dump($redis->hexists('hash', 'cat'));echo '<br>';
  print_r($redis->hgetall('hash'));echo '<br>';
  //给hash表中key增加一个整数值
  $redis->hincrby('hash', '1', 1);
  print_r($redis->hgetall('hash'));echo '<br>';
  //给hash中的某个key增加一个浮点值
  $redis->hincrbyfloat('hash', 2, 1.3);
  print_r($redis->hgetall('hash'));echo '<br>';
  //结果
  // Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
  // Array ( [1] => 1 [2] => 2 [3] => 3 [5] => 5 )
  // 1
  // bool(false)
  // Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
  // Array ( [1] => 2 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
  // Array ( [1] => 2 [2] => 3.3 [3] => 3 [4] => 4 [5] => 5 )

list

//列表
//存储数据到列表中
$redis->lpush('list', 'html');
$redis->lpush('list', 'css');
$redis->lpush('list', 'php');
//获取列表中所有的值
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>'; 
//从右侧加入一个
$redis->rpush('list', 'mysql');
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>';
//从左侧弹出一个
$redis->lpop('list');
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>';
//从右侧弹出一个
$redis->rpop('list');
$list = $redis->lrange('list', 0, -1);
print_r($list);echo '<br>';
// 结果
// Array ( [0] => php [1] => css [2] => html )
// Array ( [0] => php [1] => css [2] => html [3] => mysql )
// Array ( [0] => css [1] => html [2] => mysql )
// Array ( [0] => css [1] => html )

set

<?php
  //实例化redis
  $redis = new Redis();
  //连接
  $redis->connect('127.0.0.1', 6379);
  //集合
  $redis->sadd('set', 'horse');
  $redis->sadd('set', 'cat');
  $redis->sadd('set', 'dog');
  $redis->sadd('set', 'bird');
  $redis->sadd('set2', 'fish');
  $redis->sadd('set2', 'dog');
  $redis->sadd('set2', 'bird');
  print_r($redis->smembers('set'));echo '<br>';
  print_r($redis->smembers('set2'));echo '<br>';
  //返回集合的交集
  print_r($redis->sinter('set', 'set2'));echo '<br>';
  //执行交集操作 并结果放到一个集合中
  $redis->sinterstore('output', 'set', 'set2');
  print_r($redis->smembers('output'));echo '<br>';
  //返回集合的并集
  print_r($redis->sunion('set', 'set2'));echo '<br>';
  //执行并集操作 并结果放到一个集合中
  $redis->sunionstore('output', 'set', 'set2');
  print_r($redis->smembers('output'));echo '<br>';
  //返回集合的差集
  print_r($redis->sdiff('set', 'set2'));echo '<br>';
  //执行差集操作 并结果放到一个集合中
  $redis->sdiffstore('output', 'set', 'set2');
  print_r($redis->smembers('output'));echo '<br>';
  // 结果
  // Array ( [0] => cat [1] => dog [2] => bird [3] => horse )
  // Array ( [0] => bird [1] => dog [2] => fish )
  // Array ( [0] => bird [1] => dog )
  // Array ( [0] => dog [1] => bird )
  // Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )
  // Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )
  // Array ( [0] => horse [1] => cat )
  // Array ( [0] => horse [1] => cat )

zset

<?php
  //实例化redis
  $redis = new Redis();
  //连接
  $redis->connect('127.0.0.1', 6379);
  //有序集合
  //添加元素
  echo $redis->zadd('set', 1, 'cat');echo '<br>';
  echo $redis->zadd('set', 2, 'dog');echo '<br>';
  echo $redis->zadd('set', 3, 'fish');echo '<br>';
  echo $redis->zadd('set', 4, 'dog');echo '<br>';
  echo $redis->zadd('set', 4, 'bird');echo '<br>';
  //返回集合中的所有元素
  print_r($redis->zrange('set', 0, -1));echo '<br>';
  print_r($redis->zrange('set', 0, -1, true));echo '<br>';
  //返回元素的score值
  echo $redis->zscore('set', 'dog');echo '<br>';
  //返回存储的个数
  echo $redis->zcard('set');echo '<br>';
  //删除指定成员
  $redis->zrem('set', 'cat');
  print_r($redis->zrange('set', 0, -1));echo '<br>';
  //返回集合中介于min和max之间的值的个数
  print_r($redis->zcount('set', 3, 5));echo '<br>';
  //返回有序集合中score介于min和max之间的值
  print_r($redis->zrangebyscore('set', 3, 5));echo '<br>';
  print_r($redis->zrangebyscore('set', 3, 5, ['withscores'=>true]));echo '<br>';
  //返回集合中指定区间内所有的值
  print_r($redis->zrevrange('set', 1, 2));echo '<br>';
  print_r($redis->zrevrange('set', 1, 2, true));echo '<br>';
  //有序集合中指定值的socre增加
  echo $redis->zscore('set', 'dog');echo '<br>';
  $redis->zincrby('set', 2, 'dog');
  echo $redis->zscore('set', 'dog');echo '<br>';
  //移除score值介于min和max之间的元素
  print_r($redis->zrange('set', 0, -1, true));echo '<br>';
  print_r($redis->zremrangebyscore('set', 3, 4));echo '<br>';
  print_r($redis->zrange('set', 0, -1, true));echo '<br>';
  //结果
  // 1
  // 0
  // 0
  // 0
  // 0
  // Array ( [0] => cat [1] => fish [2] => bird [3] => dog )
  // Array ( [cat] => 1 [fish] => 3 [bird] => 4 [dog] => 4 )
  // 4
  // 4
  // Array ( [0] => fish [1] => bird [2] => dog )
  // 3
  // Array ( [0] => fish [1] => bird [2] => dog )
  // Array ( [fish] => 3 [bird] => 4 [dog] => 4 )
  // Array ( [0] => bird [1] => fish )
  // Array ( [bird] => 4 [fish] => 3 )
  // 4
  // 6
  // Array ( [fish] => 3 [bird] => 4 [dog] => 6 )
  // 2
  // Array ( [dog] => 6 )

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值