Redis的数据类型及操作(二):hash类型和list类型

一  hash类型


      Redis hash是一个 string类型的field和value的映射表.它的添加、删除操作都是 O(1) (平均)。hash特别适合用于存储对象。相较于将对象的每个字段存成单个 string类型。将一个对象存储在 hash 类型中会占用更少的内存,并且可以更方便的存取整个对象。省内存的原因是新建一个hash对象时开始是用 zipmap(又称为 small hash)来存储的。这个 zipmap其实并不是hash table,但是 zipmap相比正常的hash实现可以节省不少hash 本身需要的一些元数据存储开销。尽管 zipmap的添加,删除,查找都是 O(n),但是由于一般对象的 field数量都不太多。所以使用 zipmap 也是很快的,也就是说添加删除平均还是 O(1)。如果 field 或者 value的大小超出一定限制后, Redis会在内部自动将zipmap替换成正常的 hash实现.  这个限制可以在配置文件中指定 hash-max-zipmap-entries 64 #配置字段最多 64个 hash-max-zipmap-value 512 #配置value 最大为512字节 。

1.1  hset 

    设置hash field为指定值,如果 key 不存在,则先创建。

127.0.0.1:6379> hset myhash field1 Hello 
(integer) 1 
127.0.0.1:6379> 
1.2  hsetnx 

    设置hash field为指定值,如果 key 不存在,则先创建。如果 field已经存在,返回0,nx是not exist的意思。 

127.0.0.1:6379> hsetnx myhash field "Hello" 
(integer) 1 
127.0.0.1:6379> hsetnx myhash field "Hello" 
(integer) 0 
127.0.0.1:6379> 
   第一次执行是成功的,但第二次执行相同的命令失败,原因是field已经存在了。

1.3  hmset 

     同时设置hash的多个field。

127.0.0.1:6379> hmset myhash field1 Hello field2 World 
OK 
127.0.0.1:6379> 

1.4  hget

    获取指定的hash field。

127.0.0.1:6379> hget myhash field1 
"Hello" 
127.0.0.1:6379> hget myhash field2 
"World" 
127.0.0.1:6379> hget myhash field3 
(nil) 
127.0.0.1:6379> 
由于数据库没有 field3,所以取到的是一个空值nil 。


1.5  hmget 

     获取全部指定的 hash filed。

127.0.0.1:6379> hmget myhash field1 field2 field3 
1) "Hello" 
2) "World" 
3) (nil) 
127.0.0.1:6379> 

由于数据库没有 field3,所以取到的是一个空值nil 

1.6   hincrby 

指定的hash filed  加上给定值。

127.0.0.1:6379> hset myhash field3 20 
(integer) 1 
127.0.0.1:6379> hget myhash field3 
"20" 
127.0.0.1:6379> hincrby myhash field3 -8 
(integer) 12 
127.0.0.1:6379> hget myhash field3 
"12" 
127.0.0.1:6379> 
在本例中我们将 field3 的值从20 降到了12,即做了一个减8 的操作。

1.7 hexists 

测试指定field是否存在。 

127.0.0.1:6379> hexists myhash field1 
(integer) 1            
127.0.0.1:6379> hexists myhash field9 
(integer) 0 
127.0.0.1:6379>


1.8  hlen 

返回指定hash的 field数量。 

127.0.0.1:6379> hlen myhash 
(integer) 4 
127.0.0.1:6379> 
通过上例可以看到 myhash中有4 个field。 


1.9  hdel 

   删除某个键值对

127.0.0.1:6379> hlen myhash 
(integer) 4 
127.0.0.1:6379> hdel myhash field1 
(integer) 1 
127.0.0.1:6379> hlen myhash 
(integer) 3 
127.0.0.1:6379> 


1.10  hkeys 

返回hash的所有 field。

127.0.0.1:6379> hkeys myhash 
1) "field2" 
2) "field" 
3) "field3" 
127.0.0.1:6379> 
说明这个hash中有3 个field 


1.11 hvals

返回hash的所有 value。 

127.0.0.1:6379> hvals myhash 
1) "World" 
2) "Hello" 
3) "12" 


1.12  hgetall 

获取某个hash中全部的filed及value。 

127.0.0.1:6379> hgetall myhash 
1) "field2" 
2) "World" 
3) "field" 
4) "Hello" 
5) "field3" 
6) "12" 
127.0.0.1:6379> 
可见,一下子将 myhash中所有的field及对应的value 都取出来了。


二  list类型及操作 


      list 是一个链表结构,主要功能是 push、pop、获取一个范围的所有值等等,操作中 key 理解为链表的名字。 
 
      Redis的list类型其实就是一个每个子元素都是 string类型的双向链表。链表的最大长度是(2的32次方)。我们可以通过 push,pop操作从链表的头部或者尾部添加删除元素。这使得 list既可以用作栈,也可以用作队列。 
 
       有意思的是list的 pop操作还有阻塞版本的,当我们[lr]pop一个 list对象时,如果list是空,或者不存在,会立即返回 nil。但是阻塞版本的 b[lr]pop可以则可以阻塞,当然可以加超时时间,超时后也会返回 nil。为什么要阻塞版本的 pop 呢,主要是为了避免轮询。举个简单的例子如果我们用 list来实现一个工作队列。执行任务的thread可以调用阻塞版本的 pop去获取任务这样就可以避免轮询去检查是否有任务存在。当任务来时候工作线程可以立即返回,也可以避免轮询带来的延迟。说了这么多,接下来看一下实际操作的方法吧。

2.1  lpush

     在key对应list的头部添加字符串元素 。

127.0.0.1:6379> lpush mylist "hello" 
(integer) 1 
127.0.0.1:6379> lpush mylist "world" 
(integer) 2 
127.0.0.1:6379> lrange mylist 0 -1 
1) "world"   
2) "hello" 
127.0.0.1:6379> 
在此处我们先插入了一个hello,然后在hello的头部插入了一个 world。其中 lrange 是用于取mylist的内容(从头部开始)。


2.2  rpush 
     在key对应list的尾部添加字符串元素 

127.0.0.1:6379> rpush mylist2 "hello" 
(integer) 1 
127.0.0.1:6379> rpush mylist2 "world" 
(integer) 2 
127.0.0.1:6379> lrange mylist2 0 -1 
1) "hello" 
2) "world" 
127.0.0.1:6379> 
在此处我们先插入了一个hello,然后在hello的尾部插入了一个world。

2.3  linsert 

    在key对应list的特定位置之前或之后添加字符串元素 

127.0.0.1:6379> rpush mylist3 "hello" 
(integer) 1 
127.0.0.1:6379> rpush mylist3 "world" 
(integer) 2 
127.0.0.1:6379> linsert mylist3 before "world" "there" 
(integer) 3 
127.0.0.1:6379> lrange mylist3 0 -1 
1) "hello" 
2) "there" 
3) "world" 
127.0.0.1:6379> 
     在此处我们先插入了一个 hello,然后在 hello 的尾部插入了一个 world,然后又在 world 的前面插入了there。

2.4 lset
     设置list中指定下标的元素值(下标从0开始) 
127.0.0.1:6379> rpush mylist4 "one" 
(integer) 1 
127.0.0.1:6379> rpush mylist4 "two"         
(integer) 2 
127.0.0.1:6379> rpush mylist4 "three" 
(integer) 3 
redis 127.0.0.1:6379> lset mylist4 0 "four" 
OK 
127.0.0.1:6379> lset mylist4 -2 "five" 
OK 
redis 127.0.0.1:6379> lrange mylist4 0 -1 
1) "four" 
2) "five" 
3) "three" 
127.0.0.1:6379> 
在此处我们依次插入了 one,two,three,然后将标是0的值设置为four,再将下标是-2 的值设置为five。 

2.5   lrem 
     从key对应list中删除 count个和value相同的元素。 count>0 时,按从头到尾的顺序删除,具体如下: 

127.0.0.1:6379> rpush mylist5 "hello" 
(integer) 1 
127.0.0.1:6379> rpush mylist5 "hello" 
(integer) 2 
127.0.0.1:6379> rpush mylist5 "foo" 
(integer) 3 
127.0.0.1:6379> rpush mylist5 "hello" 
(integer) 4 
127.0.0.1:6379> lrem mylist5 2 "hello" 
(integer) 2 
127.0.0.1:6379> lrange mylist5 0 -1 
1) "foo" 
2) "hello" 
127.0.0.1:6379> 


2.6 ltrim 
保留指定key  的值范围内的数据 

127.0.0.1:6379> rpush mylist8 "one" 
(integer) 1 
127.0.0.1:6379> rpush mylist8 "two" 
(integer) 2 
127.0.0.1:6379> rpush mylist8 "three" 
(integer) 3 
127.0.0.1:6379> rpush mylist8 "four" 
(integer) 4 
127.0.0.1:6379> ltrim mylist8 1 -1 
OK 
127.0.0.1:6379> lrange mylist8 0 -1 
1) "two" 
2) "three" 
3) "four" 


2.7 lpop 

    从list的头部删除元素,并返回删除元素 

127.0.0.1:6379> lrange mylist 0 -1 
1) "hello" 
2) "world" 
127.0.0.1:6379> lpop mylist 
"hello" 
127.0.0.1:6379> lrange mylist 0 -1 
1) "world" 
127.0.0.1:6379> 


2.8   rpop 

    从list的尾部删除元素,并返回删除元素 

127.0.0.1:6379> lrange mylist2 0 -1 
1) "hello" 
2) "world" 
127.0.0.1:6379> rpop mylist2 
"world" 
127.0.0.1:6379> lrange mylist2 0 -1 
1) "hello" 

2.9  rpoplpush 

     从第一个list的尾部移除元素并添加到第二个 list的头部,最后返回被移除的元素值,整个操作是原子的.如果第一个list是空或者不存在返回 nil 

127.0.0.1:6379> lrange mylist5 0 -1 
1) "three" 
2) "foo" 
3) "hello" 
127.0.0.1:6379> lrange mylist6 0 -1 
1) "hello" 
2) "foo" 
127.0.0.1:6379> rpoplpush mylist5 mylist6 
"hello" 
127.0.0.1:6379> lrange mylist5 0 -1 
1) "three" 
2) "foo" 
127.0.0.1:6379> lrange mylist6 0 -1 
1) "hello" 
2) "hello" 
3) "foo" 
127.0.0.1:6379> 

2.10  lindex 

   返回名称为key的 list中index位置的元素 

127.0.0.1:6379> lrange mylist5 0 -1 
1) "three" 
2) "foo" 
127.0.0.1:6379> lindex mylist5 0 
"three" 
127.0.0.1:6379> lindex mylist5 1 
"foo" 
127.0.0.1:6379> 


2.11  llen

返回key 对应list的长度

127.0.0.1:6379> llen mylist5 
(integer) 2 














  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御前两把刀刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值