1.导入redis自动配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.自定义CacheManager类
package top.javazhangwei.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import top.javazhangwei.bean.Department;
import top.javazhangwei.bean.Employee;
import java.net.UnknownHostException;
@Configuration
public class RedisConf {
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Employee> employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
template.setDefaultSerializer(serializer);
return template;
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Department> depRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Department> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Department> serializer = new Jackson2JsonRedisSerializer<Department>(Department.class);
template.setDefaultSerializer(serializer);
return template;
}
//使用可序列化
@Primary
@Bean
public RedisCacheManager emploeeCacheManager(RedisTemplate<Object, Employee> employeeRedisTemplate){
RedisCacheManager cacheManager = new RedisCacheManager(employeeRedisTemplate);
cacheManager.setUsePrefix(true);
return cacheManager;
}
@Bean
public RedisCacheManager depCacheManager(RedisTemplate<Object, Department> depRedisTemplate){
RedisCacheManager cacheManager = new RedisCacheManager(depRedisTemplate);
cacheManager.setUsePrefix(true);
return cacheManager;
}
}
3.在service层指定每个sevice使用的CacheManager
package top.javazhangwei.service;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;
import top.javazhangwei.bean.Employee;
import top.javazhangwei.mapper.EmployeeMapper;
import javax.annotation.Resource;
@Service
@CacheConfig(cacheNames ={"emp"},cacheManager = "emploeeCacheManager")
public class EmployeeService {
@Resource
private EmployeeMapper employeeMapper;
/**
* 将方法的运行结果进行缓存,以后再要相同的数据,直接从缓存中获取
* 属性:
* cacheNames/value:指定缓存名字,
* key缓存数据使用的key,可以用它来指定,默认使用方法参数的值
* keyGenerator:key的生成器,可以自己指定
* condition:指定符合条件的情况下 才缓存
* unless 否定缓存,当unless指定的条件为true,方法的返回值不被缓存
* sync 是否使用异步模式
* @param id
* @return
*/
@Cacheable(/*cacheNames = {"emp"}*/)
public Employee getEmpById(Integer id){
System.out.println("查询"+id+",员工信息!");
Employee emp = employeeMapper.findByEmp(id);
return emp;
}
/**
* @CachePut:既调用方法,又更新缓存数据
* 修改了数据库的某个数据,同时更新缓存
* 1.先调用目标方法
* 2.然后在把数据放到缓存数据里
* 查询不到的原因是:查询放的 key是id,value是员对象
* 而更新操作,key是employee,value是员工对象,因此key,value值不一样
* 在更新操作中只需要把key设置为id,就可以达到效果了
*/
@CachePut(/*value ={"emp"},*/key="#result.id")
public Employee updateEmp(Employee employee){
System.out.println("updateEmp"+employee.toString());
employeeMapper.updateEmp(employee);
return employee;
}
/***
* @CacheEvict缓存清除
* 删除一条数据
* 因为value值是数组,所以赋值的时候采用数组形式
* allEntries:删除缓存中所有的数据,默认为false不删除
*/
@CacheEvict(/*value = {"emp"},*/key = "#id")
public void delEmp(Integer id){
System.out.println("删除"+id+"员工信息!");
employeeMapper.delEmp(id);
}
/**
@Caching定义负责的缓存规则
*
*/
@Caching(
cacheable = @Cacheable(/*value = {"emp"},*/key = "#lastName"),
put = {
@CachePut(/*value = {"emp"},*/key = "#result.id"),
@CachePut(/*value = {"emp"},*/key = "#result.email")
}
)
public Employee getEmpByLastName(String lastName){
return employeeMapper.getEmpByLastName(lastName);
}
}
package top.javazhangwei.service;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.javazhangwei.bean.Department;
import top.javazhangwei.mapper.DepartmentMapper;
import javax.annotation.Resource;
@Service
@CacheConfig(cacheManager = "depCacheManager")
public class DepartmentService {
@Resource
private DepartmentMapper departmentMapper;
@Cacheable(value = {"dep"})
public Department getDepById(Integer id){
Department dep = departmentMapper.getDepById(id);
System.out.println("查询部门: "+id);
return dep;
}
}
4.测试
5.idea解决8080端口被占用问题
//查出8080端口被哪个进程占用
netstat -aon|findstr 8080
//杀死盖进程
taskkill -f -pid 5292