Redis 基本操作

  • 启动
启动server:redis-server.exe redis.windows.conf
启动client:redis-cli.exe -h 127.0.0.1 -p 6379
  • 配置
--get all config 
config get *
config get configname

--set config
config set configname configvalue
  • Abount key
-- key
set key value    --set key and value
del key          -- delete key
exists key       -- if exists
expire aaa 10    -- set the timeout 10s
pexpire aaa 5000  -- set timeout 5000mm
expireat aaa 1504771560  --set timeout at unixstamp
ttl key --show the seconds left before expire
rename key key1
  • String(字符串,整数,浮点数)
127.0.0.1:6379> set name zouyufei
127.0.0.1:6379> mset name zouyufei age 20 (multi set)
127.0.0.1:6379> get name
127.0.0.1:6379> getrange name 1 3   (index from 0)
127.0.0.1:6379> setrange name 7 aa (name = name from 0-7  +  aa)
127.0.0.1:6379> mget name age  (get multi keys)
127.0.0.1:6379> setex gender 10 male (set gender=male, and timeout=10s)
127.0.0.1:6379>setnx name zouyf (set name=zouyf if name doesn't exist)
127.0.0.1:6379> msetnx name zouyufei age 20 (multi set when all keys don't exist)
127.0.0.1:6379> strlen name (get name length)
127.0.0.1:6379> incr age (age ++)
127.0.0.1:6379> incrby age 10 (age+=10)
127.0.0.1:6379> decr age (age--)
127.0.0.1:6379> decrby age 10 (age -=10)
127.0.0.1:6379> append name dtc (name=name+dtc)
  • Hash(包含键值队的无序散列表)
127.0.0.1:6379> hset mac cpu i7
127.0.0.1:6379> hmset mac cpu i5 memory 10G price 4999 (set multi)
127.0.0.1:6379> hgetall mac
127.0.0.1:6379> hget mac cpu
127.0.0.1:6379> hmget mac screen memory (get multi )
127.0.0.1:6379> hdel mac address screen (delete address and screen from mac)
127.0.0.1:6379> hexists mac name
127.0.0.1:6379> hincrby mac price 10 (mac's price +=10)
127.0.0.1:6379> hkeys mac (get all keys in mac)
127.0.0.1:6379> hlen mac (key count)
127.0.0.1:6379> hsetnx mac cpu i8 (set value only key doesn't exist)
127.0.0.1:6379> hvals mac (get all values)
  • List (一个链表,链表上每个结点都包含一个字符串)
127.0.0.1:6379> lpush list 1 (if list doesn't exist, create the list)
127.0.0.1:6379> lpush list 2 3 4 (push the elements from left)
127.0.0.1:6379> lpushx list 6 (if list doesn't exist, do nothing)
127.0.0.1:6379> lrange list 0 2
127.0.0.1:6379> lindex list 2 (get the second element)
127.0.0.1:6379> llen list (get length)
127.0.0.1:6379> lpop list (get first element from left - head)
127.0.0.1:6379> lrem list 2 5 (remove 5 twice from head)
127.0.0.1:6379> lset list 0 100 (set the first element from left to 100)
127.0.0.1:6379> ltrim list 0 4 (remain from 0 -4 elements)
127.0.0.1:6379> rpoplpush list list (pop first element from right and push to list from left)

127.0.0.1:6379> rpush list 2 3 4 (push the elements from right)
127.0.0.1:6379> rpushx list 6 (if list doesn't exist, do nothing)
127.0.0.1:6379> rpop list (get first element from right - tail)
  • Set (字符串无序收集器,每个元素唯一)
127.0.0.1:6379> sadd set 1 2 3 4
127.0.0.1:6379> srem set 4 (remove)
127.0.0.1:6379> smembers set (show all)
127.0.0.1:6379> scard set  (show size)
127.0.0.1:6379> sdiff set set1 (get the difference between set and set1)
127.0.0.1:6379> sdiffstore diffset set set1 ( store the difference between set and set1 to diffset)
127.0.0.1:6379> sinter set set1 (交集)
127.0.0.1:6379> sinterstore interset set set1
127.0.0.1:6379> sunion set set1 (并集)
127.0.0.1:6379> sunionstore unionset set set1
127.0.0.1:6379> sismember set 1 (if 1 belongs to set)
127.0.0.1:6379> smove set set1 1 (remove 1 from set to set1)
127.0.0.1:6379> spop set 2 (drop 2 elements randomly from set)
127.0.0.1:6379> srandmember set 2 (get 2 elements randomly from set)
  • Sorted Set (字符串成员与浮点数分值之间的有序映射)
127.0.0.1:6379> zadd zet 1 a
127.0.0.1:6379> zadd zet 2 b
127.0.0.1:6379> zadd zet 1 a 2 b 3 c (add a,b,c with weight 1 2 3)

127.0.0.1:6379> zrank zet a (show the index of a order by score asc- from 0)
127.0.0.1:6379> zrevrank zet2 a (show the index of a order by score desc)
127.0.0.1:6379> zscore zet1 a(show the score of a)
127.0.0.1:6379> zrange zet 0 10 withscores (show the elements whose index between 0 and 10)
[127.0.0.1:6379>zrangebylex zet1 [a [c (show the elements whose score between a and c's score)]


127.0.0.1:6379> zcard zet (size)
127.0.0.1:6379> zcount zet 1 2 (get the count whose score between 1 and 2)
127.0.0.1:6379> zlexcount zet1 [a [b (get the count which weight between a and b's weight )
127.0.0.1:6379> zincrby zet 10 a (add 10 to the weight of a)
127.0.0.1:6379> zinterstore interzet 2 zet zet1 (交集)
127.0.0.1:6379> zunionstore unionzet 2 zet zet1(并集)
127.0.0.1:6379> zrem zet a

127.0.0.1:6379> zremrangebylex zet [a [b (remove elements whose score between a and b's)
127.0.0.1:6379> zremrangebyscore zet 0 4 (remove elements whose score between 0 and 4)
127.0.0.1:6379> zrevrange zet1 0 1(get the last 2 elements)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 223,858评论 6 521
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,753评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 170,876评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,560评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,574评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,097评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,477评论 3 427
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,452评论 0 278
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,980评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,017评论 3 343
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,168评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,807评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,497评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,976评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,094评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,659评论 3 380
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,196评论 2 363

推荐阅读更多精彩内容

  • 简介 redis是key-value的数据,所以每个数据都是一个键值对 键的类型是字符串 值的类型分为五种:字符串...
    _琳哥阅读 95评论 0 0
  • 了解下未封装过的redis直接操作 redis趣味课程 redis支持的数据类型 string字符串型listse...
    暴打程序员阅读 364评论 1 0
  • redis基本操作命令 redis是一个key-value存储系统。和Memcached类似,它支持存储的valu...
    全能程序猿阅读 20,893评论 0 4
  • Redis基本命令 官方参考try string / number list set hashes Redis 命...
    Air_cc阅读 1,156评论 0 1
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,270评论 0 16