Home  >  Article  >  Backend Development  >  Some summary of Redis commands in PHP

Some summary of Redis commands in PHP

零到壹度
零到壹度Original
2018-04-11 16:23:352189browse


The content of this article is a partial summary of the Redis commands in PHP. It has certain reference value. Friends in need can refer to it

Connect Redis

# 实例化$redis = new Redis();
# 连接redisredis->connect('127.0.0.1',6379);

String

# 设置$redis->set('abc',123);
# 提取$redis->get('abc');

List(list):

Some summary of Redis commands in PHP

# 左边加入数据
$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 &#39;<hr/>&#39;;

# 右侧加入一条数据
$redis->rpush(&#39;language&#39;,&#39;C&#39;);
$languages = $redis->lrange(&#39;language&#39;,0,-1);
print_r($languages);
echo "<hr/>";

# 左边弹出数据
$redis->lpop(&#39;language&#39;);
$languages = $redis->lrange(&#39;language&#39;,0,-1);
print_r($languages);
echo "<hr/>";

# 右边弹出
$redis->rpop(&#39;language&#39;);
$languages = $redis->lrange(&#39;language&#39;,0,-1);
print_r($languages);
echo "<hr/>";

# 获取list长度
$length = $redis->lsize(&#39;language&#39;);
echo $length; echo &#39;<hr/>&#39;;

# 获取列表中key位置的值
echo $redis->lget(&#39;language&#39;,2);
echo $redis->lindex(&#39;language&#39;,2);

# 设置列表的key位置的值
$redis->lset(&#39;language&#39;,2);

# 返回从start到end的值
$redis->lrange(&#39;language&#39;,0,2);
$redis->lgetrange(&#39;language&#39;,0,2);

# 截取链表中start到end的元素,保留截取的元素,其余的删除
$res = $redis-> ltrim(&#39;language&#39;,0,1);
print_r($res);echo &#39;<br/>&#39;;
print_r($redis->lrange(&#39;language&#39;,0,-1));

# 删除方式: 从左向右删
$redis->lrem(&#39;language&#39;,&#39;PHP&#39;,2);

# 删除方式: 从右向左删
$redis->lrem(&#39;language&#39;,&#39;Go&#39;,2);

# 删除所有
$redis->lrem(&#39;language&#39;,&#39;PHP&#39;,0);

Hash(Hash)

Some summary of Redis commands in PHP

# 给Hash的key设置value,成功返回1 失败返回0
echo $redis->hset(&#39;person&#39;,&#39;name&#39;,&#39;Miki&#39;);
echo &#39;<br/>&#39;;
echo $redis->hset(&#39;person&#39;,&#39;age&#39;,&#39;18&#39;);
echo &#39;<br/>&#39;;
echo $redis->hset(&#39;person&#39;,&#39;gender&#39;,&#39;female&#39;);
echo &#39;<br/>&#39;;

# 获取hash中某个key的值
echo $redis->hget(&#39;person&#39;,&#39;name&#39;);

# 获取所有的keys
$arr = $redis->hkeys(&#39;person&#39;);
print_r($arr);echo &#39;<br/>&#39;;

# 获取hash中的所有值,注意:顺序随机!$arr = $redis->hvals(&#39;person&#39;);
print_r($arr);echo &#39;<br/>&#39;;

Set

Some summary of Redis commands in PHP

Sort-Set

Some summary of Redis commands in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn