一。引入依赖
<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源码分析视频 (复制链在浏览器打开)