springboot - Cache Cache beginning to understand

1, to understand cache

Cache to improve performance: calling the existing data in the cache to reduce the operation of the database.

Here Insert Picture Description
A cache manager manages a plurality of cache components, each component has a plurality of cache key, value pairs.

An example:
a company (Cache Manager) have multiple departments (cache component) which has its own department name (cacheName), each department also have more staff (key, value key-value pairs).

Wherein the key-value pairs are stored way:
Key default value is a string method parameter, value is the return value of the method.

2, the use of cache

1), the main program to add the cache annotations @EnableCaching, open the cache

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;



@MapperScan(value="com.example.mapper")
@SpringBootApplication
@EnableCaching
public class CacheApplication {

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

}

2) to test the use of (my notes here cache is marked in the Service business layer)

package com.example.service;

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

import com.example.bean.Employee;
import com.example.mapper.EmployeeMapper;

@CacheConfig(cacheNames="emp")
@Service
public class EmployeeService {
	@Autowired
	EmployeeMapper employeeMapper;
	
	@Cacheable(cacheNames = {"emp"}, key="#id"/*, keyGenerator="cacheKey", condition = "#id>1 and #root.methodName eq 'getEmp' ", unless = "#id==2"*/)
	public Employee getEmp(int id){
		System.out.println("查询...");
		return employeeMapper.get(id);
	}
	
	@CachePut(value="emp", key="#employee.id")
	public Employee update(Employee employee){
		System.out.println("update:" + employee);
		employeeMapper.update(employee);
		return employee;
	}
	
	@CacheEvict(value="emp", key="#id")
	public int delete(int id){
		System.out.println("delete:" + id);
		return id;
	}
	
	@Caching(
			cacheable = {
					@Cacheable(value="emp", key = "#lastName")
			},
			put = {
					@CachePut(value="emp", key="#result.id"),
					@CachePut(value="emp", key="#result.email")
			}
	)
	public Employee getByName(String lastName){
		return employeeMapper.getByName(lastName);
	}

	
}	

Here Insert Picture Description
Here Insert Picture Description

@CacheConfig (cacheNames = "EMP")

This class represents the cacheName / value are assigned to "emp", it indicates that these caches are of the same class. So this comment is to configure the number of cache annotated method marked with common attributes!

@Cacheable(cacheNames = {“emp”}, key="#id"/, keyGenerator=“cacheKey”, condition = "#id>1 and #root.methodName eq ‘getEmp’ ", unless = “#id==2”/)

cacheNames: category name indicates that this cache, you can write only one, you can write multiple, multiple use curly braces {} Oh.

key: indicates the name of the key buffer, the equivalent of <key, value cache>

keyGenerator: key generation strategy, the custom key name (key and two alternative)

condition: indicate conditions caching enabled

unless: indicate if the expression is true not executed cache

@CachePut

Every call represents the database and update the cache to make up after @Cacheable data update or take the old data deficiencies, the two can complement each other

@Cacheable(cacheNames = {"emp"}, key="#id"/*, keyGenerator="cacheKey", condition = "#id>1 and #root.methodName eq 'getEmp' ", unless = "#id==2"*/)
	public Employee getEmp(int id){
		System.out.println("查询...");
		return employeeMapper.get(id);
	}
	
	@CachePut(value="emp", key="#employee.id")
	public Employee update(Employee employee){
		System.out.println("update:" + employee);
		employeeMapper.update(employee);
		return employee;
	}

When updating, the classification will be named cache update id value, so the second extraction employee information although still get cached data, but this time the cached data has been updated.

@CacheEvict

Clear the cache

@Caching

It denotes complex specially configured cache condition, the loading of different types of annotations and attributes with an array

Published 31 original articles · won praise 1 · views 829

Guess you like

Origin blog.csdn.net/qq_42039738/article/details/104067511