Commit fc96901e by chentian

使用注解实现@Cacheable缓存关联关系,项目配置redis

parent e7390ecc
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
...@@ -221,6 +220,10 @@ ...@@ -221,6 +220,10 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.4</version> <version>2.4</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.founder.interservice.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;
import java.util.HashMap;
import java.util.Map;
/**
* Created by changc on 2019/3/14.
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
/**
* 生成key的策略
* @return
*/
@Bean
public KeyGenerator keyGenerator() {
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(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
rcm.setUsePrefix(false);
//设置默认的过期时间,单位秒
//rcm.setDefaultExpiration(300);不设置过期时间,永久有效
//缓存超时时间Map,key为cacheName,value为超时,单位是秒
Map<String, Long> expiresMap = new HashMap<>();
//缓存用户信息的cacheName和超时时间
expiresMap.put("asj", 10L);
//缓存产品信息的cacheName和超时时间
//expiresMap.put("product", 600L);
return rcm;
}
/**
* 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;
}
}
\ No newline at end of file
...@@ -15,6 +15,7 @@ import com.founder.interservice.util.DateUtil; ...@@ -15,6 +15,7 @@ import com.founder.interservice.util.DateUtil;
import com.founder.interservice.util.ResultVOUtil; import com.founder.interservice.util.ResultVOUtil;
import com.founder.interservice.util.StringUtil; import com.founder.interservice.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -117,7 +118,23 @@ public class IphoneTrackController { ...@@ -117,7 +118,23 @@ public class IphoneTrackController {
return resultObj; return resultObj;
} }
//使用注解实现@Cacheable缓存,调用方法前,先用key去缓存里查询,如果有则不进入方法,直接读取缓存,如果缓存中没有,则进入方法,读取完数据之后,把数据存储到缓存中
@Cacheable(value="relation", key="'objectRelationAll_'+#objValue")
@RequestMapping("/getObjectRelationAllByCacheable")
@ResponseBody
public ResultObj getObjectRelationAllByCacheable(String objValue){
ResultObj resultObj = null;
try{
System.out.println("===============没有读取缓存=============");
//resultObj=new ResultObj();
// resultObj.setObjType("1");
//resultObj.setObjValue("17703542882");
resultObj = iphoneTrackService.getObjectRelationAll(objValue,"");
}catch (Exception e){
e.printStackTrace();
}
return resultObj;
}
/** /**
* 通过手机号调取手机轨迹 * 通过手机号调取手机轨迹
......
package com.founder.interservice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created by liuys on 2019/6/3.
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
public void test1Me(){
redisTemplate.opsForValue().set("chent","lalala");
System.out.println("lalalal");
}
}
\ No newline at end of file
spring: spring:
#redis
redis:
# Redis数据库索引(默认为0)
database: 3
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password:
# 连接超时时间(毫秒)
timeout: 0
pool:
max-active: 50
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 20
# 连接池中的最小空闲连接
min-idle: 0
datasource: datasource:
xzxt: xzxt:
driver-class-name: oracle.jdbc.driver.OracleDriver driver-class-name: oracle.jdbc.driver.OracleDriver
......
spring: spring:
#redis
redis:
# Redis数据库索引(默认为0)
database: 3
# Redis服务器地址
host: 77.1.24.81
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password:
# 连接超时时间(毫秒)
timeout: 0
pool:
max-active: 50
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 20
# 连接池中的最小空闲连接
min-idle: 0
datasource: datasource:
xzxt: xzxt:
driver-class-name: oracle.jdbc.driver.OracleDriver driver-class-name: oracle.jdbc.driver.OracleDriver
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment