Redis实现微博用户注册&发送微博

微博用户注册

整个的流程是:
1:首先注册用户填写用户信息,先写入到db中
2:然后在存储数据到redis的hash结构中 key=”reg:user:id”

package com.kuangstudy.controller.reg;
import com.kuangstudy.entity.User;
import com.kuangstudy.service.UserService;
import com.kuangstudy.vo.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.reflect.generics.visitor.Reifier;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
 * @description:
 * @author: xuke
 * @time: 2021/5/22 20:05
 */
@RestController
public class RegController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("reguser")
    @ApiOperation("用户注册")
    public R regUser(User user) {
        // 1: 先把用户注册到DB中
        userService.saveOrUpdate(user);
        // 2: 然后查询最新的用户信息放入到redis的hash重
        User user1 = userService.getById(user.getId());
        Map<String, Object> map = R.beanToMap(user1);
        // 3: 准备用存入的key,将用户信息存入到redis的hash中
        String key = "reg:user:" + user.getId();
        redisTemplate.opsForHash().putAll(key, map);
        // 4: 设置key的失效时间一个月
        redisTemplate.expire(key, 30, TimeUnit.DAYS);
        return R.ok();
    }
}

微博发送

package com.kuangstudy.service.list;
import com.kuangstudy.entity.Content;
import com.kuangstudy.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.MapFactoryBean;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
 * @description:
 * @author: xuke
 * @time: 2021/6/11 23:37
 */
@Service
@Slf4j
public class PushContentService {
    @Autowired
    private RedisTemplate redisTemplate;
    public Content saveContent(Content content) {
        // 1: 保存内容到db中
        // 2: 把保存的内容写入到hash中
        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = ObjectUtils.objectToMap(content);
            String key = "weibo:content:" + content.getId();
            // 3: 把微博的内容保存到hash中
            HashOperations<String, String, Object> opsForHash = redisTemplate.opsForHash();
            opsForHash.putAll(key, stringObjectMap);
            // 4: 设置微博的有效期是30天过期
            this.redisTemplate.expire(key, 30, TimeUnit.DAYS);
            return content;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值