SpringBoot + Redis 实现缓存机制

版权声明:原创文章欢迎转载,转载请备注来源:https://blog.csdn.net/weixin_41986096 欢迎关注微信公众号摘星族 https://blog.csdn.net/weixin_41986096/article/details/83962113

SpringBoot + Mybtis +Redis 缓存使用

往期回顾:

上一节简单的介绍了SpringBoot + Properties

实现分布式服务动态配置内外部文件,

application.properties配置文件欲被

SpringBoot自动加载,需要放置到指定的位置:

src/main/resource目录下。

@Component注解:

目的是为了JavaBean可以被SpringBoot

项目启动时候被扫描到并加载到Spring容器之中。

@ConfigurationProperties(prefix="key")

属性绑定注解

CommandLineRunner和ApplicationRunner,

他们的执行时机为容器启动完成的时候

详见:

SpringBoot + Dubbo + Mybatis 分层实现分布式服务

项目结构

注:项目构建通过: http://start.spring.io/ 快速构建web 项目,
具体操作可以参考《SpringBoot使用SpringDataJPA完成数据查询 -Demo》。
本次项目搭建应用的组件是springboot + dubbo + mybatis + redis
演示是基于之前构建的项目为基础
详情可以参考:
SpringBoot + Dubbo + Mybatis 分层实现分布式服务
本次项目简单叙述如何在springboot框架当中使用Redis作为缓存
在基础环境确定好了之后,我们项目的目录结构如下:
项目结构与之前相同
可参考《SpringBoot + Dubbo + Mybatis 分层实现分布式服务

hdd-doorplate-dubbo-server

我们需要在dubbo的服务层Pom文件当中引入Spring Boot Redis 依赖

<!-- Spring Boot Redis 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId>
    <version>${spring-boot-starter-redis-version}</version>
</dependency>

然后在我们的配置文件application.properties当中配置reids

## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0

配置完成之后修改DoorplateServerImpl实现的方法

获取数据逻辑为:
如果缓存存在,从缓存中获取数据信息
如果缓存不存在,从 DB 中获取数据信息,然后插入缓存

// 注册为 Dubbo 服务
@Service(version = "1.0.0")
public class DoorplateServerImpl implements DoorplateServer {
    private static final Logger LOGGER = LoggerFactory.getLogger(DoorplateServerImpl.class);
    @Resource
    private DoorplateDao doorplateDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<String> list() {
        List<String> list = new ArrayList<String>();
        list.add("城市中心运动公园");
        return list;
    }

    @Override
    public Doorplate listById(String id) {

        String key = "doorplate_" + id;
        ValueOperations<String, Doorplate> operations = redisTemplate.opsForValue();

        // 缓存存在
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            Doorplate doorplate = operations.get(key);

            LOGGER.info("查询中: 从缓存中获取了数据 >> " + doorplate.toString());
            return doorplate;
        }
        // 从 DB 中获取城市信息
        Doorplate doorplate =doorplateDao.findById(id);

        // 插入缓存
        operations.set(key, doorplate);
        LOGGER.info("查询中: 缓存没有,插入缓存 >> " + doorplate.toString());

        return doorplate;

    }
}

Spring封装了RedisTemplate对象来进行对Redis的各种操作,它支持所有的Redis原生的api。RedisTemplate位于spring-data-redis包下。

RedisTemplate中定义了对5种数据结构操作:

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

测试

同样,我们启动Dubbo服务,Redis服务,Zookeeper注册中心,Web服务。
然后访问接口

http://127.0.0.1:6661/user/listById?id=1

返回结果:

{"id":"1","name":"房屋","address":"城市中心运动公园"}

后台输出为:
--查询中: 缓存没有,插入缓存

2018-11-11 16:30:28.976  INFO 25304 --- [:20880-thread-2] c.s.h.d.dubbo.impl.DoorplateServerImpl   : 查询中: 缓存没有,插入缓存 >> com.herbert.hdd.doorplate.entity.Doorplate@5c5b53a6

再次访问应该是从缓存中拿取:
--查询中: 从缓存中获取了数据

2018-11-11 16:30:32.016  INFO 25304 --- [:20880-thread-3] c.s.h.d.dubbo.impl.DoorplateServerImpl   : 查询中: 从缓存中获取了数据 >> com.herbert.hdd.doorplate.entity.Doorplate@324e0d28

猜你喜欢

转载自blog.csdn.net/weixin_41986096/article/details/83962113