Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
bc0e55ed
Commit
bc0e55ed
authored
Jul 29, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
local cache in feign
parent
2e5f22ef
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
149 additions
and
13 deletions
+149
-13
LocalCache.java
...e/src/main/java/com/tanpu/community/cache/LocalCache.java
+48
-0
CacheConfig.java
...src/main/java/com/tanpu/community/config/CacheConfig.java
+3
-2
FileController.java
...n/java/com/tanpu/community/controller/FileController.java
+9
-0
BatchFeignCallService.java
...va/com/tanpu/community/service/BatchFeignCallService.java
+7
-9
FeignService.java
...c/main/java/com/tanpu/community/service/FeignService.java
+82
-2
No files found.
community-service/src/main/java/com/tanpu/community/cache/LocalCache.java
0 → 100644
View file @
bc0e55ed
package
com
.
tanpu
.
community
.
cache
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.common.util.JsonUtil
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.Resource
;
import
java.util.function.Supplier
;
@Service
public
class
LocalCache
{
@Autowired
private
CaffeineCacheManager
cacheManager
;
private
Cache
localCache
;
@PostConstruct
public
void
init
()
{
localCache
=
cacheManager
.
getCache
(
"local"
);
if
(
localCache
==
null
)
{
throw
new
BizException
(
"local cache not found!"
);
}
}
public
<
T
>
T
getObject
(
String
key
,
Class
<
T
>
clz
)
{
return
localCache
.
get
(
key
,
clz
);
}
public
<
T
>
T
getObject
(
String
key
,
Supplier
<
T
>
func
,
Class
<
T
>
clz
)
{
T
value
=
localCache
.
get
(
key
,
clz
);
if
(
value
!=
null
)
{
return
value
;
}
T
ret
=
func
.
get
();
if
(
ret
!=
null
)
{
localCache
.
put
(
key
,
ret
);
}
return
ret
;
}
}
community-service/src/main/java/com/tanpu/community/config/CacheConfig.java
View file @
bc0e55ed
...
@@ -23,10 +23,11 @@ public class CacheConfig {
...
@@ -23,10 +23,11 @@ public class CacheConfig {
}
}
@Bean
@Bean
public
Ca
cheManager
c
acheManager
()
{
public
Ca
ffeineCacheManager
caffeineC
acheManager
()
{
CaffeineCacheManager
cacheManager
=
new
CaffeineCacheManager
();
CaffeineCacheManager
cacheManager
=
new
CaffeineCacheManager
();
cacheManager
.
setCacheNames
(
Arrays
.
asList
(
"local"
));
cacheManager
.
setCacheNames
(
Arrays
.
asList
(
"local"
));
cacheManager
.
setCacheSpecification
(
"maximumSize=10,expireAfterWrite=10s"
);
// todo 配置化
cacheManager
.
setCacheSpecification
(
"maximumSize=1000,expireAfterWrite=30s"
);
return
cacheManager
;
return
cacheManager
;
}
}
}
}
community-service/src/main/java/com/tanpu/community/controller/FileController.java
View file @
bc0e55ed
...
@@ -5,6 +5,7 @@ import com.tanpu.community.api.beans.resp.FileUploadResp;
...
@@ -5,6 +5,7 @@ import com.tanpu.community.api.beans.resp.FileUploadResp;
import
com.tanpu.community.manager.FileManager
;
import
com.tanpu.community.manager.FileManager
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
...
@@ -16,6 +17,9 @@ public class FileController {
...
@@ -16,6 +17,9 @@ public class FileController {
@Autowired
@Autowired
private
FileManager
fileManager
;
private
FileManager
fileManager
;
@Autowired
private
CaffeineCacheManager
localCache
;
@PostMapping
(
"/uploadFile"
)
@PostMapping
(
"/uploadFile"
)
@ResponseBody
@ResponseBody
public
CommonResp
<
FileUploadResp
>
uploadToRemote
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
)
{
public
CommonResp
<
FileUploadResp
>
uploadToRemote
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
)
{
...
@@ -24,6 +28,11 @@ public class FileController {
...
@@ -24,6 +28,11 @@ public class FileController {
@GetMapping
(
"/test"
)
@GetMapping
(
"/test"
)
public
String
test
()
{
public
String
test
()
{
localCache
.
getCache
(
"local"
).
put
(
"999"
,
"6666666"
);
System
.
out
.
println
((
String
)
localCache
.
getCache
(
"local"
).
get
(
"999"
).
get
());
for
(
int
i
=
0
;
i
<
30
;
i
++)
{
for
(
int
i
=
0
;
i
<
30
;
i
++)
{
System
.
out
.
println
(
fileManager
.
getId
(
""
+
i
/
2
));
System
.
out
.
println
(
fileManager
.
getId
(
""
+
i
/
2
));
}
}
...
...
community-service/src/main/java/com/tanpu/community/service/BatchFeignCallService.java
View file @
bc0e55ed
...
@@ -27,6 +27,7 @@ import com.tanpu.community.feign.product.FeignForPublicFund;
...
@@ -27,6 +27,7 @@ import com.tanpu.community.feign.product.FeignForPublicFund;
import
com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom
;
import
com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom
;
import
com.tanpu.community.feign.zhibo.FeignClientForZhibo
;
import
com.tanpu.community.feign.zhibo.FeignClientForZhibo
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
...
@@ -54,7 +55,6 @@ public class BatchFeignCallService {
...
@@ -54,7 +55,6 @@ public class BatchFeignCallService {
@Resource
@Resource
private
FeignClientForCourse
feignForCourse
;
private
FeignClientForCourse
feignForCourse
;
@Resource
@Resource
private
FeignForFund
feignForFund
;
private
FeignForFund
feignForFund
;
...
@@ -67,6 +67,9 @@ public class BatchFeignCallService {
...
@@ -67,6 +67,9 @@ public class BatchFeignCallService {
@Resource
@Resource
private
OSSFileService
ossFileService
;
private
OSSFileService
ossFileService
;
@Autowired
private
FeignService
feignService
;
@Resource
@Resource
private
TopicService
topicService
;
private
TopicService
topicService
;
...
@@ -189,17 +192,12 @@ public class BatchFeignCallService {
...
@@ -189,17 +192,12 @@ public class BatchFeignCallService {
}
}
if
(!
CollectionUtils
.
isEmpty
(
shortVideoIds
))
{
if
(!
CollectionUtils
.
isEmpty
(
shortVideoIds
))
{
// 短视频列表
// 短视频列表
CommonResp
<
List
<
ShortVideoBaseInfoResp
>>
commonResp
=
List
<
ShortVideoBaseInfoResp
>
list
=
feignService
.
batchGetShortVideoBaseInfo
(
setToList
(
shortVideoIds
));
feignClientForTanpuroom
.
getShortVideoBaseInfo
(
setToList
(
shortVideoIds
));
shortVideoMap
.
putAll
(
list
.
stream
().
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getSourceId
,
item
->
item
)));
if
(
commonResp
.
isSuccess
()
&&
!
CollectionUtils
.
isEmpty
(
commonResp
.
getData
()))
{
shortVideoMap
.
putAll
(
commonResp
.
getData
().
stream
()
.
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getSourceId
,
item
->
item
)));
}
}
}
if
(!
CollectionUtils
.
isEmpty
(
curriculumIds
))
{
if
(!
CollectionUtils
.
isEmpty
(
curriculumIds
))
{
// 课程列表
// 课程列表
List
<
ShortVideoBaseInfoResp
>
commonResp
=
List
<
ShortVideoBaseInfoResp
>
commonResp
=
feignService
.
getCurriculumByColumnRelId
(
setToList
(
curriculumIds
));
feignClientForFatools
.
getCurriculumByColumnRelId
(
setToList
(
curriculumIds
));
if
(
commonResp
!=
null
&&
!
CollectionUtils
.
isEmpty
(
commonResp
))
{
if
(
commonResp
!=
null
&&
!
CollectionUtils
.
isEmpty
(
commonResp
))
{
curriculumMap
.
putAll
(
commonResp
.
stream
()
curriculumMap
.
putAll
(
commonResp
.
stream
()
.
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getColumnRelId
,
item
->
item
)));
.
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getColumnRelId
,
item
->
item
)));
...
...
community-service/src/main/java/com/tanpu/community/service/FeignService.java
View file @
bc0e55ed
...
@@ -3,15 +3,55 @@ package com.tanpu.community.service;
...
@@ -3,15 +3,55 @@ package com.tanpu.community.service;
import
com.tanpu.common.api.CommonResp
;
import
com.tanpu.common.api.CommonResp
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp
;
import
com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp
;
import
com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp
;
import
com.tanpu.community.api.beans.vo.feign.fatools.UserInfoNew
;
import
com.tanpu.community.cache.LocalCache
;
import
com.tanpu.community.feign.course.FeignClientForCourse
;
import
com.tanpu.community.feign.fatools.FeignClientForFatools
;
import
com.tanpu.community.feign.fatools.FeignClientForFatools
;
import
com.tanpu.community.feign.product.FeignClientForProducts
;
import
com.tanpu.community.feign.product.FeignForFund
;
import
com.tanpu.community.feign.product.FeignForPublicFund
;
import
com.tanpu.community.feign.tanpuroom.FeignClientForTanpuroom
;
import
com.tanpu.community.feign.zhibo.FeignClientForZhibo
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.function.Function
;
@Service
@Service
public
class
FeignService
{
public
class
FeignService
{
@Autowired
@Resource
private
FeignClientForFatools
fatools
;
private
FeignClientForTanpuroom
feignClientForTanpuroom
;
@Resource
private
FeignClientForZhibo
feignClientForZhibo
;
@Resource
private
FeignClientForProducts
feignForProduct
;
@Resource
private
FeignClientForCourse
feignForCourse
;
@Resource
private
FeignForFund
feignForFund
;
@Resource
private
FeignForPublicFund
feignForPublicFund
;
@Resource
private
FeignClientForFatools
feignClientForFatools
;
@Resource
private
LocalCache
localCache
;
public
UserInfoResp
getUserInfoById
(
String
userId
)
{
public
UserInfoResp
getUserInfoById
(
String
userId
)
{
CommonResp
<
UserInfoResp
>
userInfoNewCommonResp
=
fatools
.
queryUsersListNew
(
userId
);
CommonResp
<
UserInfoResp
>
userInfoNewCommonResp
=
fatools
.
queryUsersListNew
(
userId
);
...
@@ -20,4 +60,44 @@ public class FeignService {
...
@@ -20,4 +60,44 @@ public class FeignService {
}
}
return
userInfoNewCommonResp
.
getData
();
return
userInfoNewCommonResp
.
getData
();
}
}
public
List
<
ShortVideoBaseInfoResp
>
batchGetShortVideoBaseInfo
(
List
<
String
>
sourceIds
)
{
return
batchExecute
(
"batchGetShortVideoBaseInfo_"
,
sourceIds
,
ShortVideoBaseInfoResp
.
class
,
ids
->
{
CommonResp
<
List
<
ShortVideoBaseInfoResp
>>
resp
=
feignClientForTanpuroom
.
getShortVideoBaseInfo
(
ids
);
if
(
resp
.
isSuccess
())
{
return
resp
.
getData
();
}
else
{
return
new
ArrayList
<>();
}
});
}
public
List
<
ShortVideoBaseInfoResp
>
getCurriculumByColumnRelId
(
List
<
String
>
curriculumIds
)
{
return
batchExecute
(
"getCurriculumByColumnRelId_"
,
curriculumIds
,
ShortVideoBaseInfoResp
.
class
,
ids
->
{
return
feignClientForFatools
.
getCurriculumByColumnRelId
(
ids
);
});
}
private
<
T
>
List
<
T
>
batchExecute
(
String
keyPrefix
,
List
<
String
>
keys
,
Class
<
T
>
clz
,
Function
<
List
<
String
>,
List
<
T
>>
func
)
{
List
<
T
>
retList
=
new
ArrayList
<>();
List
<
String
>
feignIds
=
new
ArrayList
<>();
for
(
String
key
:
keys
)
{
String
cacheKey
=
keyPrefix
+
key
;
T
ret
=
localCache
.
getObject
(
cacheKey
,
clz
);
if
(
ret
!=
null
)
{
retList
.
add
(
ret
);
}
else
{
feignIds
.
add
(
key
);
}
if
(!
feignIds
.
isEmpty
())
{
List
<
T
>
remoteList
=
func
.
apply
(
feignIds
);
if
(
remoteList
!=
null
)
{
retList
.
addAll
(
remoteList
);
}
}
}
return
retList
;
}
}
}
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