zoukankan      html  css  js  c++  java
  • 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  
    
  • 相关阅读:
    jmeter:dubbo接口测试
    聊聊基准测试的MVP方案
    建立团队的性能文化
    针对 Intellij IDEA 2018.2 版本 异常退出问题
    maven settings.xml配置优化
    Windows安装MySQL
    String的intern()方法详解
    Ubuntu下安装JDK图文教程详解 jdk-java6-30 .bin 的处理方法
    Iterator迭代器快捷键
    $.each $.map $.filter 区别 Script
  • 原文地址:https://www.cnblogs.com/laoniu85/p/5224079.html
Copyright © 2011-2022 走看看