1、安装redis和连接redis
参考:ubuntu安装单个redis服务_ubuntu redis单机版安装-CSDN博客
2、Redis数据类型
以下操作我们在图形化界面演示。
2.1、五种常用数据类型介绍
Redis存储的是key-value结构的数据,其中key是字符串类型,value有5种常用的数据类型:字符串 string、哈希 hash、列表 list、集合 set、有序集合 sorted set / zset。

2.2解释说明
字符串(string):普通字符串,Redis中最简单的数据类型。
哈希(hash):也叫散列,类似于Java中的HashMap结构。
列表(list):按照插入顺序排序,可以有重复元素,类似于Java中的LinkedList。
集合(set):无序集合,没有重复元素,类似于Java中的HashSet。
有序集合(sorted set/zset):集合中每个元素关联一个分数(score),根据分数升序排序,没有重复元素。
3、字符串操作命令
Redis 中字符串类型常用命令:
3.1、设置有效期
SETEX key seconds value 设置指定key的值,并将 key 的过期时间设为 seconds 秒。
setEX bbb 20 222

然后查看该缓存信息

3.2 已存在的key不能重复设置值
SETNX key value 只有在 key 不存在时设置 key 的值。

然后查看redis的缓存,aaa的值还是112

4、哈希操作命令
Redis hash 是一个string类型的 field 和 value 的映射表,hash特别适合用于存储对象,常用命令:
HSET key field value 将哈希表 key 中的字段 field 的值设为 value
HSET student1 name heming

然后查看redis里面的内容
HGET key field 获取存储在哈希表中指定字段的值


HDEL key field 删除存储在哈希表中的指定字段
hdel student1 name

此时不存在student1这个哈希
如果student1哈希有2个属性,删除一个属性后,另外那个属性还能看到。如下所示


HKEYS key 获取哈希表中所有字段
HVALS key 获取哈希表中所有值

5、 列表操作命令