一、缓存数据库性能Redis、memcached、Ehcache
Redis存储数据类型丰富,对于存储数据量不是很大的情况下处理性能效果较好
memcached对于大量的数据存储和读取性能要优于Redis
Ehcache最大的特点是轻量级,而且存储的数据类型为对象
二、Redis序列化常见的方式:
JdkSerializationRedisSerializer:JDK自带的序列化方式、存储的字符串内容在序列化的情况下偏长,会占用过多的内存
OxmSerializer:序列化的时间相对较长
Jackson2JsonRedisSerializer:json数据格式、序列化时间和序列化之后内容的长度都要优于前两种,对比前者我选择的序列化方式是第三种
三、使用流程(安装redis服务能正常连接--省略)
服务启动类
@SpringBootApplication @EnableEurekaClient public class RedisServer { public static void main(String[] args) { new SpringApplicationBuilder(RedisServer.class).web(true).run(args); } }
pom.xml
<!-- 整合redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <!-- redis客户端操作 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency>
配置文件:
application.yml
server: port: 9009 spring: application: name: springCloud-redis eureka: instance: hostname: localhost client: register-with-eureka: true fetch-registry: false service-url: defaultZone: http://localhost:9001/eureka/
bootstrap.yml
spring: redis: database: 0 # Redis服务器地址 host: localhost # Redis服务器连接端口 port: 6379 # Redis服务器连接密码(默认为空) password: # 连接池最大连接数(使用负值表示没有限制) timeout: 0 # 连接超时时间(毫秒) pool: max-active: 8 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1 # 连接池中的最大空闲连接 max-idle: 8 # 连接池中的最小空闲连接 min-idle: 0
config类配置
package redis.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; 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.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.util.HashMap; import java.util.Map; @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); //设置缓存过期时间 rcm.setDefaultExpiration(60);//秒 return rcm; } /** * RedisTemplate配置 * @param factory * @return */ @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); //定义value的序列化方式 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
编写接口和实现类
public interface RedisService { void set(String key, Object value); Object get(String key); }
@Service public class RedisServiceImpl implements RedisService{ @Resource private RedisTemplate<String,Object> redisTemplate; //stringRedisTemplate只能缓存key-value的String类型 @Override public void set(String key, Object value) { ValueOperations<String,Object> vo = redisTemplate.opsForValue(); vo.set(key, value); } @Override public Object get(String key) { ValueOperations<String,Object> vo = redisTemplate.opsForValue(); return vo.get(key); } }
controller层实现:
@RestController @RequestMapping("/redis") public class RedisController { @Autowired private RedisService redisService; @RequestMapping("/save") public String save(){ //这里用于测试,key值可以自定义实现 redisService.set("123456","test-redis"); return "SUCCESS"; } @RequestMapping("/get") public String get(){ //这里用于测试,key值可以自定义实现 return (String) redisService.get("123456"); } }
四、注册服务启动,调用接口测试--省略