spring boot 整合redis 将代码记录以备以后使用

2017-09-13 by u010520802

spring boot 整合redis

配置文件
在application.properties 中添加redis配置项spring.redis.database=0 spring.redis.host=192.168.128.36spring.redis.port=6379spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.timeout=3 配置类
建立RedisConfig.java 类package com.boot;import java.lang.reflect.Method;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.core.env.Environment;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.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;@[email protected] class RedisConfig extends CachingConfigurerSupport { @Autowired private Environment env; @Bean JedisConnectionFactory connectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(env.getProperty("spring.redis.host")); factory.setPort(Integer.parseInt(env.getProperty("spring.redis.port").trim())); factory.setUsePool(true); return factory; } @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @SuppressWarnings("rawtypes") @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); // 设置缓存过期时间 // rcm.setDefaultExpiration(60);//秒 return rcm; } @Bean Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return jackson2JsonRedisSerializer; } @SuppressWarnings({ "rawtypes", "unchecked" }) @Bean public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory connectionFactory, Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(connectionFactory); template.setDefaultSerializer(jackson2JsonRedisSerializer); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); return template; }}用法示例
建立 RedisService.javaimport java.lang.reflect.Field;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;import com.common.util.StringUtil;@Servicepublic class RedisService { @Autowired private RedisTemplate<String, String> template; @Override public String getTerminalFromRedis(String Key) { return template.opsForValue().get(Key); } @Override public void setRedis(String key, String value) { template.template.opsForValue().set(key, value); }}
最新更新:

第七城市

栏目导航(关闭)