Spring Boot缓存实战

一 新建项目

1 新增依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.2.0</version>
    </dependency>
</dependencies>

2 配置application.properties

spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe
spring.datasource.username=system
spring.datasource.password=oracle

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true


spring.jackson.serialization.indent_output=true

二 新建实体类

package com.wisely.ch8_5.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    
    private String name;
    
    private Integer age;
    
    private String address;
    
    
    public Person() {
        super();
    }
    public Person(Long id, String name, Integer age, String address) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }


}

三 新建实体类的Repository

package com.wisely.ch8_5.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.wisely.ch8_5.domain.Person;

public interface PersonRepository extends JpaRepository<Person, Long> {
    

}

四 业务服务

1 接口

package com.wisely.ch8_5.service;

import com.wisely.ch8_5.domain.Person;

public interface DemoService {
    public Person save(Person person);
    
    public void remove(Long id);
    
    public Person findOne(Person person);

}

2 实现类

package com.wisely.ch8_5.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.wisely.ch8_5.dao.PersonRepository;
import com.wisely.ch8_5.domain.Person;
import com.wisely.ch8_5.service.DemoService;


@Service
public class DemoServiceImpl implements DemoService {
    
    @Autowired
    PersonRepository personRepository;

    @Override
    //@CachePut缓存新增或更新的数据到缓存,其中缓存的名称为people,数据的key是person的id
    @CachePut(value = "people", key = "#person.id")
    public Person save(Person person) {
        Person p = personRepository.save(person);
        System.out.println("为id、key为:"+p.getId()+"数据做了缓存");
        return p;
    }

    @Override
    // @CacheEvict从缓存people中删除key为id的数据
    //如果没有指定key,则方法参数作为key
    @CacheEvict(value = "people")
    public void remove(Long id) {
        System.out.println("删除了id、key为"+id+"的数据缓存");
        //这里不做实际删除操作
    }

    @Override
    //@Cacheable缓存key为person的id数据到缓存people中
    @Cacheable(value = "people", key = "#person.id")
    public Person findOne(Person person) {
        Person p = personRepository.findOne(person.getId());
        System.out.println("为id、key为:"+p.getId()+"数据做了缓存");
        return p;
    }

}

五 控制器

package com.wisely.ch8_5.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisely.ch8_5.domain.Person;
import com.wisely.ch8_5.service.DemoService;

@RestController
public class CacheController {
    
    @Autowired
    DemoService demoService;
    
    @RequestMapping("/put")
    public Person put(Person person){
        return demoService.save(person);
        
    }
    
    @RequestMapping("/able")
    public Person cacheable(Person person){
        return demoService.findOne(person);
    }
    
    @RequestMapping("/evit")
    public String  evit(Long id){
         demoService.remove(id);
         return "ok";
    }
}

六 开启缓存支持

@SpringBootApplication
//在Spring Boot中还是要使用@EnableCaching开启缓存支持
@EnableCaching
public class Ch85Application {

    public static void main(String[] args) {
        SpringApplication.run(Ch85Application.class, args);
    }
}

七 测试

1 运行项目,数据库生成数据表person,表数据如下:

2 测试@Cacheable

第一次访问:http://localhost:8080/able?id=1,第一次将调用方法查询数据库,并将数据方到缓存people中,此时控制台输出如下:

第二次访问:http://localhost:8080/able?id=1,此时控制没有输入上图信息。表示没有调用这个方法,页面直接从数据缓存中获得数据。

3 测试@CachePut

浏览器输入:http://localhost:8080/put?name=cc&age=22&address=成都,此时控制台输出如下:

再次访问:http://localhost:8080/able?id=7,控制台无输出,从缓存直接获得数据。

4 测试@CacheEvit

访问:http://localhost:8080/able?id=1,控制台无输出,表明数据已从缓存中获取

访问:http://localhost:8080/evit?id=1,从缓存中删除key为1的缓存数据,控制台输出如下:

删除了id、key为1的数据缓存

访问:http://localhost:8080/able?id=1,控制台输出如下,说明重新了缓存

5 下面看看数据库管理软件下的数据

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值