SpringBoot数据缓存Cache

        我们知道一个程序的瓶颈在于数据库,我们也知道内存的速度是大大快于硬盘的速度的。当我们需要重复的获取相同的市局的时候,我们一次又一次的请求数据库或者远程服务,导致大量的时间耗费在数据库查询或者远程方法调用上,导致程序性能的恶化,这边是数据缓存要解决的问题。

首先得添加依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

声明是缓存注解

Spring提供了四个注解来声明缓存规则(又是使用注解式的AOP的一个生动例子),这四个注解如下:

@Cacheable    在方法执行前Spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;如果没有数据,调用方法讲方法返回值放进缓存

@CachePut      无论怎么样,都回将方法返回值放入缓存中。@CachePut属性与@Cacheable保持一致

@CacheEvict    将一条或多条数据从缓存中删除

@Caching        可以通过@Caching注解组合多个注解策略在一个方法上

@Cacheable,@CachePut,@CacheEvit都有Value属性,指定的是要用缓存的名称;key属性指定的是缓存数据在缓存中存储的键值。

开启声明式缓存支持直接在启动类加个注解就好

package com.cn.sola;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@EnableCaching
public class BootEhcacheApplication {

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

实例------------------------------------------------------------------

此例子以Spring Boot默认的ConcurrentMapCacheManager作为缓存技术,最后会用Ehcache,Guava来替换缓存技术

实体类

package com.cn.sola.bean;

import java.io.Serializable;

public class Emp implements Serializable{

	private String id;
	private String name;
	private String age;
	
	private String job;
	private String boss;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getBoss() {
		return boss;
	}
	public void setBoss(String boss) {
		this.boss = boss;
	}
	
}

Controller

package com.cn.sola.controller;

import java.util.List;
import java.util.Map;

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

import com.cn.sola.bean.Emp;
import com.cn.sola.service.EhcacheService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("xe")
@Api(tags= {"Ehcache学习"})
public class EhcacheController {
	
	@Autowired
	private EhcacheService ehcacheservice;

	@ApiOperation("ehcache查询")
	@GetMapping("getlist")
	public List<Map<String,Object>> getEmp(String id){
		return ehcacheservice.getEmp(id);
		
	}
	
	@ApiOperation("ehcache删除")
	@GetMapping("remove")
	public boolean removetOne(String id){
		return ehcacheservice.removetOne(id);
		
	}
	
	@ApiOperation("ehcache新增")
	@PostMapping("add")
	public boolean add(@RequestBody Emp emp){
		return ehcacheservice.add(emp);
		
	}
	
	
}

Service

package com.cn.sola.service;

import java.util.List;
import java.util.Map;

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.cn.sola.bean.Emp;
import com.cn.sola.dao.EhcacheMapper;

@Service
public class EhcacheService {

	@Autowired
	private EhcacheMapper ehcachemapper;
	
	@Cacheable(value="sola",key="#id")//value属性是指缓存名 key是指就是key
	public List<Map<String, Object>> getEmp(String id) {
		
		return ehcachemapper.getEmp(id);
	}

	@CacheEvict(value="sola")//从缓存SOLA中删除key为id的数据
	public boolean removetOne(String id) {
		// TODO Auto-generated method stub
		return ehcachemapper.removetOne(id);
	}

	@CachePut(value="sola",key="#emp.id")//缓存新增的或更新数据到缓存,其中缓存的名称为sola,数据的Key是emp的id
	public boolean add(Emp emp) {
		// TODO Auto-generated method stub
		
		return ehcachemapper.add(emp);
	}
	
	

}
剩下部分正常没区别,运行时记得要开启缓存支持~~~~~



额。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

猜你喜欢

转载自blog.csdn.net/jiulanhao/article/details/80987201