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
6b62264c
Commit
6b62264c
authored
Jul 29, 2021
by
吴泽佳
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev' of
http://47.100.44.39:10001/tp-backend/tanpu-community
into dev
parents
15b271a6
5fce0cce
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
149 additions
and
14 deletions
+149
-14
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
-3
No files found.
community-service/src/main/java/com/tanpu/community/cache/LocalCache.java
0 → 100644
View file @
6b62264c
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 @
6b62264c
...
...
@@ -23,10 +23,11 @@ public class CacheConfig {
}
@Bean
public
Ca
cheManager
c
acheManager
()
{
public
Ca
ffeineCacheManager
caffeineC
acheManager
()
{
CaffeineCacheManager
cacheManager
=
new
CaffeineCacheManager
();
cacheManager
.
setCacheNames
(
Arrays
.
asList
(
"local"
));
cacheManager
.
setCacheSpecification
(
"maximumSize=10,expireAfterWrite=10s"
);
// todo 配置化
cacheManager
.
setCacheSpecification
(
"maximumSize=1000,expireAfterWrite=30s"
);
return
cacheManager
;
}
}
community-service/src/main/java/com/tanpu/community/controller/FileController.java
View file @
6b62264c
...
...
@@ -5,6 +5,7 @@ import com.tanpu.community.api.beans.resp.FileUploadResp;
import
com.tanpu.community.manager.FileManager
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
...
...
@@ -16,6 +17,9 @@ public class FileController {
@Autowired
private
FileManager
fileManager
;
@Autowired
private
CaffeineCacheManager
localCache
;
@PostMapping
(
"/uploadFile"
)
@ResponseBody
public
CommonResp
<
FileUploadResp
>
uploadToRemote
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
)
{
...
...
@@ -24,6 +28,11 @@ public class FileController {
@GetMapping
(
"/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
++)
{
System
.
out
.
println
(
fileManager
.
getId
(
""
+
i
/
2
));
}
...
...
community-service/src/main/java/com/tanpu/community/service/BatchFeignCallService.java
View file @
6b62264c
...
...
@@ -27,6 +27,7 @@ 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.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
...
...
@@ -54,7 +55,6 @@ public class BatchFeignCallService {
@Resource
private
FeignClientForCourse
feignForCourse
;
@Resource
private
FeignForFund
feignForFund
;
...
...
@@ -67,6 +67,9 @@ public class BatchFeignCallService {
@Resource
private
OSSFileService
ossFileService
;
@Autowired
private
FeignService
feignService
;
@Resource
private
TopicService
topicService
;
...
...
@@ -189,17 +192,12 @@ public class BatchFeignCallService {
}
if
(!
CollectionUtils
.
isEmpty
(
shortVideoIds
))
{
// 短视频列表
CommonResp
<
List
<
ShortVideoBaseInfoResp
>>
commonResp
=
feignClientForTanpuroom
.
getShortVideoBaseInfo
(
setToList
(
shortVideoIds
));
if
(
commonResp
.
isSuccess
()
&&
!
CollectionUtils
.
isEmpty
(
commonResp
.
getData
()))
{
shortVideoMap
.
putAll
(
commonResp
.
getData
().
stream
()
.
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getSourceId
,
item
->
item
)));
}
List
<
ShortVideoBaseInfoResp
>
list
=
feignService
.
batchGetShortVideoBaseInfo
(
setToList
(
shortVideoIds
));
shortVideoMap
.
putAll
(
list
.
stream
().
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getSourceId
,
item
->
item
)));
}
if
(!
CollectionUtils
.
isEmpty
(
curriculumIds
))
{
// 课程列表
List
<
ShortVideoBaseInfoResp
>
commonResp
=
feignClientForFatools
.
getCurriculumByColumnRelId
(
setToList
(
curriculumIds
));
List
<
ShortVideoBaseInfoResp
>
commonResp
=
feignService
.
getCurriculumByColumnRelId
(
setToList
(
curriculumIds
));
if
(
commonResp
!=
null
&&
!
CollectionUtils
.
isEmpty
(
commonResp
))
{
curriculumMap
.
putAll
(
commonResp
.
stream
()
.
collect
(
Collectors
.
toMap
(
ShortVideoBaseInfoResp:
:
getColumnRelId
,
item
->
item
)));
...
...
community-service/src/main/java/com/tanpu/community/service/FeignService.java
View file @
6b62264c
...
...
@@ -3,21 +3,100 @@ package com.tanpu.community.service;
import
com.tanpu.common.api.CommonResp
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp
;
import
com.tanpu.community.api.beans.vo.feign.course.ShortVideoBaseInfoResp
;
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.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.cache.Cache
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
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
public
class
FeignService
{
@Autowired
private
FeignClientForFatools
fatools
;
@Resource
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
)
{
CommonResp
<
UserInfoResp
>
userInfoNewCommonResp
=
fatools
.
queryUsersListNew
(
userId
);
CommonResp
<
UserInfoResp
>
userInfoNewCommonResp
=
f
eignClientForF
atools
.
queryUsersListNew
(
userId
);
if
(
userInfoNewCommonResp
.
isNotSuccess
())
{
throw
new
BizException
(
"内部接口调用失败"
);
}
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