`
bit1129
  • 浏览: 1052184 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【Redis四】Redis数据类型

 
阅读更多

概述

Redis是一个高性能的数据结构服务器,称之为数据结构服务器的原因是,它提供了丰富的数据类型以满足不同的应用场景,本文对Redis的数据类型以及对这些类型可能的操作进行总结。

Redis常用的数据类型包括string、set、list、hash以及sorted set.Redis本身是K/V系统,这里的数据类型指的是value的类型,而不是key的类型,key的类型只有一种即string,Redis要求key这个字符串的长度必须大于1。

Redis提供了type命令用于判断key对应的value的类型,例如 type key,返回值有如下几种,

none,string,hash,set,sortedset,list,其中none表示value是nil。

 

string

string提供了如下操作:

  • SET/GET: 设置和读取键值,这是最常用的操作
localhost:6381> set key1 10
OK
localhost:6381> set key2 abc
OK
localhost:6381> set key3 this is a book
(error) ERR syntax error
localhost:6381> set key3 "this is a book"
OK
localhost:6381> get key1
"10"
localhost:6381> ket ke2
(error) ERR unknown command 'ket'
localhost:6381> get key3
"this is a book"
localhost:6381> get key2
"abc"
localhost:6381>

 

  • INCR:为整型value做增1操作
localhost:6381> INCR count
(integer) 1
localhost:6381> set key1 abc
OK
localhost:6381> INCR key1
(error) ERR value is not an integer or out of range

 

  • INCRBY:为整型value做增BY操作,如INCRBY count -10,则对count做减10操作
localhost:6381> INCRBY key4 10
(integer) 10
localhost:6381> INCRBY key4 100
(integer) 110
localhost:6381> INCRBY key4 -100
(integer) 10
localhost:6381> INCRBY key4 -200
(integer) -190
localhost:6381>

 

  • GETSET:读取并设值,这是一个原子操作,类似于JUC的CAS原语,或者AtomicInteger的incrementAndGet等操作

 

localhost:6381> getset abc 1
"2"
localhost:6381> getset key5 1
(nil)
localhost:6381> get key5
"1"
localhost:6381> getset key5 2
"1"
localhost:6381> get key5
"2"
localhost:6381>

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics