Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
bigdatainterservice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曹鹏
bigdatainterservice
Commits
fc96901e
Commit
fc96901e
authored
Aug 01, 2019
by
chentian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
使用注解实现@Cacheable缓存关联关系,项目配置redis
parent
e7390ecc
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
173 additions
and
2 deletions
+173
-2
pom.xml
+4
-1
src/main/java/com/founder/interservice/config/RedisConfig.java
+87
-0
src/main/java/com/founder/interservice/controller/IphoneTrackController.java
+18
-1
src/main/java/com/founder/interservice/test.java
+24
-0
src/main/resources/application-dev.yml
+20
-0
src/main/resources/application-prod.yml
+20
-0
No files found.
pom.xml
View file @
fc96901e
...
...
@@ -40,7 +40,6 @@
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
...
...
@@ -221,6 +220,10 @@
<artifactId>
commons-io
</artifactId>
<version>
2.4
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/founder/interservice/config/RedisConfig.java
0 → 100644
View file @
fc96901e
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
src/main/java/com/founder/interservice/controller/IphoneTrackController.java
View file @
fc96901e
...
...
@@ -15,6 +15,7 @@ import com.founder.interservice.util.DateUtil;
import
com.founder.interservice.util.ResultVOUtil
;
import
com.founder.interservice.util.StringUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.CrossOrigin
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
...
@@ -117,7 +118,23 @@ public class IphoneTrackController {
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
;
}
/**
* 通过手机号调取手机轨迹
...
...
src/main/java/com/founder/interservice/test.java
0 → 100644
View file @
fc96901e
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
src/main/resources/application-dev.yml
View file @
fc96901e
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
:
xzxt
:
driver-class-name
:
oracle.jdbc.driver.OracleDriver
...
...
src/main/resources/application-prod.yml
View file @
fc96901e
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
:
xzxt
:
driver-class-name
:
oracle.jdbc.driver.OracleDriver
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment