Home > Article > Backend Development > Some summary of Redis commands in PHP
# 实例化$redis = new Redis(); # 连接redisredis->connect('127.0.0.1',6379);
# 设置$redis->set('abc',123); # 提取$redis->get('abc');
# 左边加入数据 $redis->lpush('language','PHP'); $redis->lpush('language','Java'); $redis->lpush('language','Go'); $redis->lpush('language','Python); # 循环获取值 $languages = $redis->lrange('language',0,-1); print_r($languages); echo '<hr/>'; # 右侧加入一条数据 $redis->rpush('language','C'); $languages = $redis->lrange('language',0,-1); print_r($languages); echo "<hr/>"; # 左边弹出数据 $redis->lpop('language'); $languages = $redis->lrange('language',0,-1); print_r($languages); echo "<hr/>"; # 右边弹出 $redis->rpop('language'); $languages = $redis->lrange('language',0,-1); print_r($languages); echo "<hr/>"; # 获取list长度 $length = $redis->lsize('language'); echo $length; echo '<hr/>'; # 获取列表中key位置的值 echo $redis->lget('language',2); echo $redis->lindex('language',2); # 设置列表的key位置的值 $redis->lset('language',2); # 返回从start到end的值 $redis->lrange('language',0,2); $redis->lgetrange('language',0,2); # 截取链表中start到end的元素,保留截取的元素,其余的删除 $res = $redis-> ltrim('language',0,1); print_r($res);echo '<br/>'; print_r($redis->lrange('language',0,-1)); # 删除方式: 从左向右删 $redis->lrem('language','PHP',2); # 删除方式: 从右向左删 $redis->lrem('language','Go',2); # 删除所有 $redis->lrem('language','PHP',0);
# 给Hash的key设置value,成功返回1 失败返回0 echo $redis->hset('person','name','Miki'); echo '<br/>'; echo $redis->hset('person','age','18'); echo '<br/>'; echo $redis->hset('person','gender','female'); echo '<br/>'; # 获取hash中某个key的值 echo $redis->hget('person','name'); # 获取所有的keys $arr = $redis->hkeys('person'); print_r($arr);echo '<br/>'; # 获取hash中的所有值,注意:顺序随机!$arr = $redis->hvals('person'); print_r($arr);echo '<br/>';
The above is the detailed content of Some summary of Redis commands in PHP. For more information, please follow other related articles on the PHP Chinese website!