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  
    
  • 相关阅读:
    时隔三个月,不言谢归来了。学习播客继续更新日常所学。
    一个网易云音乐的外链URL简单的获取方法
    VisualStudioCode常用主题,与插件设置
    页面a标签统一跳转方法:base 标签
    Web前端开发规范(记录一):团队约定-基本原则
    JS中,正则表达式:match(/^(.*)(.)(.{1,8})$/)[3]分析
    Morgan模块使用
    Config模块使用
    MongoDB安装
    JSP-九大内置对象
  • 原文地址:https://www.cnblogs.com/laoniu85/p/5224079.html
Copyright © 2011-2022 走看看