• 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  
    
  • 相关阅读:
    HDOJ.1263
    另一种跳转actvity方式
    [转]Android中用Java获取时间实例
    获得当前时间的方法
    3d动画切换
    登录跳转效果
    activity 成popupwindow效果
    自定义preference的使用等等
    edittext输入框的背景效果
    自定义ListPreference
  • 原文地址:https://www.cnblogs.com/laoniu85/p/5224079.html
Copyright © 2020-2023  润新知