Day14 Redis应用实战-Hash操作

Redis 资料型态Hash

  • Hash是用来储存多组栏位值,可以是数字或字串.使用者可以对值进行操作,跟资料结构中的dictionary概念很像.

  • 可用指令

    • HSET
    • HSETNX
    • HGET
    • HEXISTS
    • HDEL
    • HLEN
    • HSTRLEN
    • HINCRBY
    • HINCRBYFLOAT
    • HMSET
    • HMGET
    • MKEYS
    • HVALS
    • HGETALL
    • HSCAN
  • 资料结构

    • Hash如果栏位值个数 < 512且总长度 < 64 bytes,则使用ziplist,其余使用hashtable.

HSET

设定hash可以有多个field and value 组成.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

# 底层资料结构为ziplist
127.0.0.1:6379> object encoding books
"ziplist"

HKEYS

取得hash中所有的field.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hkeys books
1) "name"
2) "price"

HVALS

取得hash中所有的value.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hvals books
1) "abc"
2) "10"

HGETALL

取得hash中所有的field and value.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"

HGET

取得hash中指定field的value.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hget books name
"abc"

127.0.0.1:6379> hget books price
"10"

127.0.0.1:6379> hget books no
(nil)

HINCRBY

设定hash中指定的field的value依照设定数值进行递增.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hincrby books price 1
(integer) 11

127.0.0.1:6379> hget books price
"11"

HINCRBYFLOAT

设定hash中指定的field的value依照设定浮点数值进行递增.

127.0.0.1:6379> hset books name abc price 10 discountrate 0.2
(integer) 3

127.0.0.1:6379> hincrbyfloat books discountrate 0.5
"0.7"

127.0.0.1:6379> hget books discountrate
"0.7"

HDEL

删除hash中指定field.

127.0.0.1:6379> hset books name abc price 10 discountrate 0.2
(integer) 3

127.0.0.1:6379> hget books discountrate
"0.7"

127.0.0.1:6379> hdel books discountrate
(integer) 1

127.0.0.1:6379> hget books discountrate
(nil)

HMGET

取得hash中多个指定field的value.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hmget books name price
1) "abc"
2) "10"

HMSET

设定hash中key的多个指定field的value.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hmget books name price
1) "abc"
2) "10"

127.0.0.1:6379> hmset books name def price 33
OK

127.0.0.1:6379> hmget books name price
1) "def"
2) "33"

HSETNX

设定hash中key的field和value但需要不存在既有的fiels时才写入field与value.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

# 目前设定books所有的field and value
127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"

# 需要设定books中field name的value会失败
127.0.0.1:6379> hsetnx books name def
(integer) 0

127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"

# 需要设定books中field discountrate的value因此field不存在所以成功
127.0.0.1:6379> hsetnx books discountrate 0.2
(integer) 1

127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"
5) "discountrate"
6) "0.2"


HEXISTS

确认hash中key指定的field是否存在.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"

# books中的field name 存在
127.0.0.1:6379> hexists books name
(integer) 1

# books中的field discountrate 不存在
127.0.0.1:6379> hexists books discountrate
(integer) 0

HLEN

取得hash key中的field个数.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hlen books
(integer) 2

HSTRLEN

取得hash key中指定field的value字串长度.

127.0.0.1:6379> hset books name abc price 10
(integer) 2

127.0.0.1:6379> hstrlen books name
(integer) 3

HSCAN

扫描hash key中指定field 特定match pattern与次数.

# 存在name
127.0.0.1:6379> hscan books 0 match name count 1
1) "0"
2) 1) "name"
   2) "abc"

# 不存在discountrate
127.0.0.1:6379> hscan books 0 match discountrate
1) "0"
2) (empty array)

Hash 与 String 优缺点比较

  • Hash优点

    • 将所有相关的field and value放在同一个key中储存,减少记忆体消耗
    • 多个field只需要一个key,减少key重覆的可能
    • 可以一次存取多个field and value只需要一个取得key的指令,减少CPU与记忆体的消耗
    • String能做到的Hash也都可以
  • Hash缺点

    • Field不能设定过期时间
    • 没有bit相关操作
    • 当值很大时,无法分散到其他服务器(Redis Cluster架构)

<<:  [C 语言笔记--Day18] 用 linked list 实作 merge sort

>>:  Day14 - 使用 Kamigo 进行权限控管

错误处理

Rust将错误分成两大类 不可复原的(unrecoverable) 可复原的(recoverable...

【D13】取得:加权指数历史资料,并观察量与指数的关系

前言 昨天取得九月的加权指数资料,但是时间太短,看不出来量与价格的关系,所以就要取得更远的资料罗。取...

Day 16:堆积(heap)与字首树(trie)

上一回写到树,今天的主题是以树结构为基础的资料结构。 堆积 二元树(binary tree)是每个节...

抽象类别和介面 (2)

什麽是抽象类别 an abstract class, or abstract base class ...

RISC-V on Rust 从零开始(7) - 实作指令基本框架

使用Spike执行RISC-V gnu toolchain编译出来的ELF档,就可以得到完整执行此E...