zoukankan      html  css  js  c++  java
  • 使用SpringCache进行缓存数据库查询

    1、在SpringBoot的启动类上添加注解@EnableCaching,开启SpringCache缓存支持

    @SpringBootApplication
    // 开启SpringCache缓存支持
    @EnableCaching
    public class GatheringApplication {
        public static void main(String[] args) {
            SpringApplication.run(GatheringApplication.class, args);
        }
    }
    

    2、在service的方法上添加对应的注解

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    // 使用SpringCache进行缓存数据库查询
    @Cacheable(value = "gathering", key = "#id")
    public Gathering findById(String id) {
    	return gatheringDao.findById(id).get();
    }
    
    /**
     * 修改
     *
     * @param gathering
     */
    // 修改数据库数据后需要删除redis中的缓存
    @CacheEvict(value = "gathering", key = "#gathering.id")
    public void update(Gathering gathering) {
    	gatheringDao.save(gathering);
    }
    
    /**
     * 删除
     *
     * @param id
     */
    // 删除数据库数据后需要删除redis中的缓存
    @CacheEvict(value = "gathering", key = "#id")
    public void deleteById(String id) {
    	gatheringDao.deleteById(id);
    }
    
  • 相关阅读:
    Python格式符号
    Python基础知识
    HDFS 数据流程
    HDFS IO流操作
    HDFS API操作
    Hadoop 客户端环境准备
    Hadop 环境搭建 windows10+hadoop2.7.7
    Hadoop HDFS shell
    Hadoop HDFS 基础
    centos 更改清华源
  • 原文地址:https://www.cnblogs.com/tian-ci/p/10543061.html
Copyright © 2011-2022 走看看