背景:
Redis基本數(shù)據(jù)結(jié)構(gòu)
五種數(shù)據(jù)結(jié)構(gòu)
這五種數(shù)據(jù)結(jié)構(gòu)分別是STRING(字符串)、LIST(列表)、SET(集合)、HASH(哈希)、ZSET(有序集合);
字符串:包括字符串、整數(shù)和浮點數(shù);
列表:一個鏈表,鏈表上面的每個結(jié)點都是一個字符串,其遵從隊列的訪問格式-先進先出,也就是從鏈表的結(jié)尾進行插入,鏈表的頭部進行彈出;
集合:里面是一個容器,他不允許存在相同的元素,每個值都是獨一無二的;
哈希:是一個鍵值對組合而成的無序散列表,其的鍵同樣是不允許重復(fù)的;
有序集合:是在集合的基礎(chǔ)之上進行了排序;
Redis Hset 命令
語法
redis Hset 命令基本語法如下:
redis 127.0.0.1:6379> HSET KEY_NAME FIELD VALUE
實例
實例
redis 127.0.0.1:6379> HSET myhash field1 "foo"
OK
redis 127.0.0.1:6379> HGET myhash field1
"foo"
redis 127.0.0.1:6379> HSET website google "www.g.cn" # 設(shè)置一個新域
(integer) 1
redis 127.0.0.1:6379>HSET website google "www.google.com" # 覆蓋一個舊域
點贊表的結(jié)構(gòu)設(shè)計
CREATE TABLE `ins_awesome` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ins_id` int(11) NOT NULL COMMENT '帖子id',
`uid` int(11) NOT NULL COMMENT '點贊人id',
`awed_uid` int(11) NOT NULL COMMENT '被點贊人id',
`type` int(11) DEFAULT NULL COMMENT '1是贊 2是取消贊',
`create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時間',
`update_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_uv` (`ins_id`,`uid`),
KEY `idx_uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=25193 DEFAULT CHARSET=utf8mb4;
redisTemplate的相關(guān)api:
/**
* Increment {@code value} of a hash {@code hashKey} by the given {@code delta}.
*
* @param key must not be {@literal null}.
* @param hashKey must not be {@literal null}.
* @param delta
* @return {@literal null} when used in pipeline / transaction.
*/
Long increment(H key, HK hashKey, long delta);
具體Java實現(xiàn)
private Long storyIncrease(Integer id, String type) {
Long l = stringRedisTemplate.opsForHash().increment(storyKey(id), type, 1L);
stringRedisTemplate.expire(storyKey(id), 3L, TimeUnit.DAYS);
return l;
}
為什么用stringredistemplate