一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
二、开启注解
@SpringBootApplication
public class SpringcacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcacheApplication.class, args);
}
}
package com.example.springcache.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableCaching
@Slf4j
public class CacheConfig extends CachingConfigurerSupport {
final RedisConnectionFactory connectionFactory;
public CacheConfig(RedisConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
@Bean
@Primary
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
String re= String.format("%s:%s(%s)",target.getClass().getName(),method.getName(),
CollectionUtils.arrayToList(params));
log.debug("缓存生成的key:{}。",re);
return re;
}
};
}
}
package com.example.springcache.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
三、在需要缓存数据的方法上加上@Cacheable注解,cacheNames+key成为该缓存值的key,通过#获取方法上的参数。方法的返回值成为缓存的value。
package com.example.springcache.service;
import com.example.springcache.bean.User;
import com.example.springcache.dao.UserDao;
import com.github.pagehelper.page.PageMethod;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
final UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
@Cacheable(cacheNames = "user")
public List<User> listUser(Integer pageSize,Integer pageCount){
if(pageCount != null || pageSize != null){
PageMethod.startPage(pageSize, pageCount);
}
return userDao.selectUser();
}
}