• spring-boot 整合redis作为数据缓存


    添加依赖

    <dependency>  
              <groupId>org.springframework.boot</groupId>  
              <artifactId>spring-boot-starter-redis</artifactId>  
          </dependency>  
    

    Redis配置

    package com.wisely.ij.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.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.core.RedisTemplate;  
    import org.springframework.data.redis.core.StringRedisTemplate;  
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;  
      
    import java.lang.reflect.Method;  
      
      
    @Configuration  
    @EnableCaching  
    public class RedisConfig extends CachingConfigurerSupport{  
      
        @Bean  
        public KeyGenerator wiselyKeyGenerator(){  
            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();  
                }  
            };  
      
        }  
      
        @Bean  
        public CacheManager cacheManager(  
                @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {  
            return new RedisCacheManager(redisTemplate);  
        }  
      
        @Bean  
        public RedisTemplate<String, String> redisTemplate(  
                RedisConnectionFactory factory) {  
            StringRedisTemplate template = new StringRedisTemplate(factory);  
            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.afterPropertiesSet();  
            return template;  
        }  
    }  
    
    # REDIS (RedisProperties)  
    spring.redis.database= # database name  
    spring.redis.host=localhost # server host  
    spring.redis.password= # server password  
    spring.redis.port=6379 # connection port  
    spring.redis.pool.max-idle=8 # pool settings ...  
    spring.redis.pool.min-idle=0  
    spring.redis.pool.max-active=8  
    spring.redis.pool.max-wait=-1  
    spring.redis.sentinel.master= # name of Redis server  
    spring.redis.sentinel.nodes= # comma-separated list of host:port pairs  
    
  • 相关阅读:
    MySQL支持的数据类型(2)( 日期)
    MySQL支持的数据类型(1)( 整数,小数,位)
    MySQL查询语句(select)详解(2)
    MySQL查询语句(select)详解(1)
    MySQL sql_safe_updates 分析
    MySQL中INFORMATION_SCHEMA是什么?(2)
    js event事件对象概括
    js String字符串对象常见方法总结
    js Array数组对象常见方法总结
    纯js轮播图
  • 原文地址:https://www.cnblogs.com/laoniu85/p/5224079.html
Copyright © 2020-2023  润新知