SpringBoot Redis 缓存(二)

97 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情

  • 实体类一定要实现Serializable接口,因为对象要序列化保存

TestService

public interface TestService {
    String test0(Long id);

    UserInfo test1(Long id);
}

TestServiceImpl

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class TestServiceImpl implements TestService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public String test0(Long id) {
        // 模拟延迟操作
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("产生一个新字符串");
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();

        String cacheKey = "str:" + id;
        System.out.println("redis缓存此字符串 " + cacheKey + " ===> " + str);
        stringRedisTemplate.opsForValue().set(cacheKey, str);
        return str;
    }

    @Override
    public UserInfo test1(Long id) {
        // 模拟延迟操作
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("产生一个新对象");
        UserInfo userInfo = new UserInfo(id, "admin", "123456");

        String cacheKey = "user:" + id;
        System.out.println("redis缓存此对象 " + cacheKey + " ===> " + userInfo);
        redisTemplate.opsForValue().set(cacheKey, userInfo);
        return userInfo;
    }
}
  • stringRedisTemplate.opsForValue().set(cacheKey, str);

主要就是这一句,将数据保存到Redis数据库。

TestController

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {
    @Resource
    private TestService testService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("test0/{id}")
    public String test0(@PathVariable Long id) {
        // 查询缓存
        String cacheKey = "str:" + id;
        String cacheVal = stringRedisTemplate.opsForValue().get(cacheKey);
        if (cacheVal != null) {
            System.out.println("redis缓存中直接返回字符串");
            return cacheVal;
        }
        return testService.test0(id);
    }

    @GetMapping("test1/{id}")
    public UserInfo test1(@PathVariable Long id) {
        // 查询缓存
        String cacheKey = "user:" + id;
        UserInfo cacheVal = (UserInfo) redisTemplate.opsForValue().get(cacheKey);
        if (cacheVal != null) {
            System.out.println("redis缓存中直接返回对象");
            return cacheVal;
        }
        return testService.test1(id);
    }
}
  • stringRedisTemplate.opsForValue().get(cacheKey)获取缓存的数据。
  • 缓存的Key都是字符串
  • 缓存的Value,test0接口是字符串 test1接口是对象
  • 通过id来缓存的,为了区分数据类型,字符串Key的前缀是str:,对象Key的前缀是user:

四、总结

Redis应该是目前使用最多的内存数据库了,除了做缓存之外,根据Redis的数据类型特性还可以做很多好玩的东西。比如分布式锁,点赞评论等。我们这里只用到了最简单的Key->String类型。它还有Key->List,Key->Map,Key->Set,Key->ZSet等。