springboot整合redis缓存

15 篇文章 0 订阅
3 篇文章 0 订阅

一。引入依赖

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

 

二。引入配置

package com.jztey.gxdemo; 

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; 
import java.util.HashMap; 
import java.util.Map;   

@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) {   
        RedisCacheManager redisCache =  new RedisCacheManager(redisTemplate); 

        Map<String,Long> map = new HashMap<String,Long>(); 
        map.put("getToken",500L); 
        redisCache.setExpires(map); 

        redisCache.setDefaultExpiration(500L); 
        return redisCache; 
    }   

    @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;   
    }   
}

 

三。application.properties

 

#redis配置 
spring.redis.host=10.3.5.38 
spring.redis.database=2 
spring.redis.timeout=10000

 

四.使用

    @Cacheable(value="getToken" ,key="#param.clientId")   
    public String getToken(OauthParam param) { 
    System.out.println("实际调用"); 
    String url = "https://internal.api.ehaoyao.com/auth/v1/oauth2/token"; 
//    HttpHeaders headers = new HttpHeaders(); 
//    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); 
//    headers.setContentType(type); 
//    headers.add("accept", "application/json"); 

    JSONObject json = new JSONObject(); 
    json.put("grant_type", param.getGrantType()); 
    json.put("client_id", param.getClientId()); 
    json.put("client_secret", param.getClientSecret()); 
    json.put("scope", param.getScope()); 

//    HttpEntity<String> formEntity = new HttpEntity<String>(json.toString(), headers); 
//    Map<String,String> urlVariables = new HashMap<String,String>(); 

    JSONObject resp = restOperations.postForObject(url, json, JSONObject.class); 
//   String resultJo = restOperations.postForObject(url, requestEntity, String.class); 
    System.out.println(resp.toString()); 
    return resp.getString("access_token"); 
  }

--------------

spring mvc+tomcat源码分析视频 (复制链在浏览器打开)

https://study.163.com/course/courseMain.htm?share=2&shareId=480000001919582&courseId=1209399899&_trace_c_p_k2_=6d81bc445e9c462ab8d6345e40f6b0bf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值