前言
这一篇文章将讲述Redis中的hash类型命令,同样也是通过demo来讲述,其他部分这里就不在赘述了。
项目Github地址:https://github.com/rainbowda/learnWay/tree/master/learnRedis/case-hash
案例
demo功能是通讯录,整个demo的大致页面如下
准备工作
首先定义一个key的前缀,已经存储自增id的key
private static final String CONTACTS_KEY_PREFIX = "contacts:";
private static final String CONTACTS_ID_KEY = "contactsID";
通讯录相关的key将会以contacts:1、contacts:2、contacts:3的形式存储
redis操作对象
private RedisTemplate redisTemplate;
//string 命令操作对象
private ValueOperations valueOperations;
//hash 命令操作对象
private HashOperations hashOperatio
疑惑
如果读者和我一样是学Java的,刚听到hash时的第一反应是这个不是一个算法吗?当时我也是这样想的。那么先来看看hash在Redis中的结构,如下图(图片来源于Redis in Action)。