问答平台(4),Redis入门

Redis 数据类型

1
2
3
4
5
6
Redis 是一款基于键值对的 NoSQL 数据库,它的值支持多种数据结构:
- 字符串(strings)
- 哈希(hashes)
- 列表(lists)
- 集合(sets)
- 有序集合(sorted sets)等。

Redis 存储

1
2
3
4
Redis 将所有的数据都存放在内存中,所以它的读写性能高。
同时,Redis 还可以将内存中的数据以快照(RDB)或日志(AOF)的形式保存到硬盘上,以保证数据的安全性。
快照(RDB): 不适合实时,定时任务
日志(AOF): 适合实时,占磁盘空间,恢复速度慢

Redis 应用场景

1
2
3
4
5
6
典型的应用场景包括:
- 缓存
- 排行榜
- 计数器
- 社交网络
- 消息队列等。

Redis 命令

  • 启动
    1
    redis-cli
  • 字符
    1
    2
    3
    4
    5
    6
    select 1(0-15)
    flushdb
    set test:count 1
    get test:count
    incr test:count
    decr test:count
  • 哈希
    1
    2
    3
    4
    hset test:user id 1
    hset test:user username zhangsan
    hget test:user id
    hget test:user username
  • 列表
    1
    2
    3
    4
    5
    6
    7
    8
    lpush test:ids 101 102 103
    llen test:ids
    lindex test:ids 0
    lindex test:ids 2
    lrange test:ids 0 2
    rpop test:ids
    lpop test:ids
    lrange test:ids 0 2
  • 集合
    1
    2
    3
    4
    sadd test:teachers aaa bbb ccc ddd eee
    scard test:teachers
    spop test:teachers(从集合中随机弹出一个元素)
    smembers test:teachers
  • 有序集合
    1
    2
    3
    4
    5
    zadd test:students 10 aaa 20 bbb 30 ccc 40 ddd 50 eee
    zcard test:students
    zscore test:students ccc
    zrank test:students ccc
    zrange test:students 0 2
  • 全局
    1
    2
    3
    4
    5
    6
    keys *
    keys test*
    type test:user
    exists test:user
    del test:user
    expire test:students 10(单位:s)

参考资料


问答平台(4),Redis入门
https://lcf163.github.io/2020/05/22/问答平台(4),Redis入门/
作者
乘风的小站
发布于
2020年5月22日
许可协议