美文网首页
仿微博社交平台系统设计[二]--使用redis的hash数据结构

仿微博社交平台系统设计[二]--使用redis的hash数据结构

作者: xuezhongyu01 | 来源:发表于2021-03-30 18:19 被阅读0次

背景:
Redis基本数据结构

五种数据结构

这五种数据结构分别是STRING(字符串)、LIST(列表)、SET(集合)、HASH(哈希)、ZSET(有序集合);

字符串:包括字符串、整数和浮点数;
列表:一个链表,链表上面的每个结点都是一个字符串,其遵从队列的访问格式-先进先出,也就是从链表的结尾进行插入,链表的头部进行弹出;
集合:里面是一个容器,他不允许存在相同的元素,每个值都是独一无二的;
哈希:是一个键值对组合而成的无序散列表,其的键同样是不允许重复的;
有序集合:是在集合的基础之上进行了排序;
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"       # 设置一个新域
(integer) 1
 
redis 127.0.0.1:6379>HSET website google "www.google.com" # 覆盖一个旧域

点赞表的结构设计

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 '创建时间',
  `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的相关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实现

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

相关文章

网友评论

      本文标题:仿微博社交平台系统设计[二]--使用redis的hash数据结构

      本文链接:https://www.haomeiwen.com/subject/rnvghltx.html