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

2017-09-13 20:47:37来源:CSDN作者: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;@Configuration@EnableCachingpublic 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.java
import 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);    }}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台