目前spring cache是比较流行的缓存开发架构,通过spring进行统一管理,可以很好的更换缓存产品而无需修改业务代码,下面举个栗子。
pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties文件:
spring.cache.type=redis
spring.cache.cache-names=epCamp,epScene
# Redis服务器地址
spring.redis.host=192.168.1.235
# Redis服务器连接端口 使用默认端口6379可以省略配置
spring.redis.port=6979
# Redis服务器连接密码(默认为空)
spring.redis.password=smartgo2019
# 连接池最大连接数(如果配置<=0,则没有限制 )
spring.redis.jedis.pool.max-active=50
启动类中使用@EnableCaching注解开启spring cache:
package com.ismartgo.ep;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import tk.mybatis.spring.annotation.MapperScan;
/**
* springboot启动器
* @author zsy
*/
// 开启缓存
@EnableCaching
@MapperScan(basePackages = "com.ismartgo.*.mapper")
@ServletComponentScan
@SpringBootApplication
public class CampApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CampApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(CampApplication.class, args);
}
}
RedisConfig类:
package com.ismartgo.ep.common.config;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.mapping.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
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.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
@Configuration
@EnableCaching // 这个使用springCache----->如果只用redis,可以不用添加
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
业务代码:
@Cacheable(value = "epCamp", key = "#campCode", unless = "#result == null")
public EpCamp getCampByCode(String campCode) {
if (campCode == null)
return null;
EpCamp camp = new EpCamp();
camp.setCampCode(campCode);
camp.setIsDeleted(YesOrNoEnum.no.getValue());
camp = campMapper.selectOne(camp);
return camp;
}
其中方法上面的value对应redis库中的一个块
存在redis的结构:
下一面文章将讲解spring cache 的注解使用