Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
brandkbs2
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
shenjunjie
brandkbs2
Commits
3903c936
Commit
3903c936
authored
Jan 11, 2023
by
shenjunjie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature' into 'release'
渠道榜单缓存调优 See merge request
!166
parents
9f9fa5a3
2e9fdd00
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
65 deletions
+76
-65
src/main/java/com/zhiwei/brandkbs2/pojo/ChannelRecord.java
+1
-1
src/main/java/com/zhiwei/brandkbs2/service/impl/ChannelServiceImpl.java
+75
-64
No files found.
src/main/java/com/zhiwei/brandkbs2/pojo/ChannelRecord.java
View file @
3903c936
...
...
@@ -162,7 +162,7 @@ public class ChannelRecord {
JSONObject
json
=
new
JSONObject
(
esMap
);
this
.
rangeStartTime
=
json
.
getLongValue
(
RANGE_START_TIME
);
this
.
rangeEndTime
=
json
.
getLongValue
(
RANGE_END_TIME
);
this
.
platform
=
json
.
getString
(
"platform"
);
this
.
platform
=
GlobalPojo
.
getPlatformNameById
(
json
.
getString
(
"platform_id"
)
);
this
.
realSource
=
json
.
getString
(
"real_source"
);
this
.
source
=
json
.
getString
(
"source"
);
this
.
projectId
=
json
.
getString
(
"project_id"
);
...
...
src/main/java/com/zhiwei/brandkbs2/service/impl/ChannelServiceImpl.java
View file @
3903c936
...
...
@@ -434,65 +434,81 @@ public class ChannelServiceImpl implements ChannelService {
List
<
ChannelListVO
>
resList
=
new
ArrayList
<>();
try
{
String
projectId
=
UserThreadLocal
.
getProjectId
();
String
redisKey
=
RedisUtil
.
getChannelRecordList
(
projectId
,
startTime
,
endTime
,
emotion
);
// 部分条件不缓存
if
(
null
!=
keyword
)
{
return
getEmotionList
(
projectId
,
contendId
,
platform
,
keyword
,
sorter
,
startTime
,
endTime
,
size
,
emotion
,
cache
);
}
String
redisKey
=
RedisUtil
.
getChannelRecordList
(
projectId
,
startTime
,
endTime
,
platform
,
emotion
);
String
resultStr
;
// 返回缓存
if
(
cache
&&
StringUtils
.
isNotEmpty
(
resultStr
=
redisUtil
.
get
(
redisKey
)))
{
return
JSON
.
parseArray
(
resultStr
,
ChannelListVO
.
class
);
}
Map
<
String
,
Pair
<
Long
,
ChannelRecord
>>
keyMap
=
new
HashMap
<>();
EsClientDao
.
SearchHelper
searchHelper
=
createSearchHelperByChannelCriteria
(
projectId
,
null
,
Collections
.
singleton
(
contendId
),
platform
,
keyword
,
startTime
,
endTime
);
// 分页查询所有结果
List
<
SearchResponse
>
searchResponses
=
channelEsDao
.
searchScrollResponse
(
searchHelper
);
for
(
SearchResponse
searchResponse
:
searchResponses
)
{
for
(
SearchHit
hit
:
searchResponse
.
getHits
().
getHits
())
{
ChannelRecord
channelRecord
=
new
ChannelRecord
(
hit
.
getSourceAsMap
());
keyMap
.
compute
(
channelRecord
.
getKey
(),
(
k
,
v
)
->
{
if
(
null
==
v
)
{
return
Pair
.
of
(
channelRecord
.
getRangeEndTime
(),
channelRecord
);
}
else
{
// 比较数据时间
if
(
channelRecord
.
getRangeEndTime
()
>
v
.
getLeft
())
{
v
.
getRight
().
setChannelInfo
(
channelRecord
);
}
// 合并文章数据
v
.
getRight
().
getRecord
().
mergeRecord
(
channelRecord
.
getRecord
());
return
v
;
resList
=
getEmotionList
(
projectId
,
contendId
,
platform
,
keyword
,
sorter
,
startTime
,
endTime
,
size
,
emotion
,
cache
);
// 配合天级缓存开启
redisUtil
.
setExpire
(
redisKey
,
JSON
.
toJSONString
(
resList
));
}
catch
(
IOException
e
)
{
ExceptionCast
.
cast
(
CommonCodeEnum
.
FAIL
,
"es查询异常"
);
}
return
resList
;
}
private
List
<
ChannelListVO
>
getEmotionList
(
String
projectId
,
String
contendId
,
String
platform
,
String
keyword
,
String
sorter
,
Long
startTime
,
Long
endTime
,
int
size
,
int
emotion
,
boolean
cache
)
throws
IOException
{
List
<
ChannelListVO
>
resList
=
new
ArrayList
<>();
Map
<
String
,
Pair
<
Long
,
ChannelRecord
>>
keyMap
=
new
HashMap
<>();
EsClientDao
.
SearchHelper
searchHelper
=
createSearchHelperByChannelCriteria
(
projectId
,
null
,
Collections
.
singleton
(
contendId
),
platform
,
keyword
,
startTime
,
endTime
);
// 分页查询所有结果
List
<
SearchResponse
>
searchResponses
=
channelEsDao
.
searchScrollResponse
(
searchHelper
);
for
(
SearchResponse
searchResponse
:
searchResponses
)
{
for
(
SearchHit
hit
:
searchResponse
.
getHits
().
getHits
())
{
ChannelRecord
channelRecord
=
new
ChannelRecord
(
hit
.
getSourceAsMap
());
keyMap
.
compute
(
channelRecord
.
getKey
(),
(
k
,
v
)
->
{
if
(
null
==
v
)
{
return
Pair
.
of
(
channelRecord
.
getRangeEndTime
(),
channelRecord
);
}
else
{
// 比较数据时间
if
(
channelRecord
.
getRangeEndTime
()
>
v
.
getLeft
())
{
v
.
getRight
().
setChannelInfo
(
channelRecord
);
}
});
}
// 合并文章数据
v
.
getRight
().
getRecord
().
mergeRecord
(
channelRecord
.
getRecord
());
return
v
;
}
});
}
// 过滤掉不符合时间条件的数据并排序
List
<
ChannelRecord
>
channelRecords
=
keyMap
.
values
().
stream
().
map
(
pair
->
{
ChannelRecord
channelRecord
=
pair
.
getRight
();
// 情感过滤
if
(
emotion
==
EmotionEnum
.
ALL
.
getState
()
||
emotion
==
channelRecord
.
getEmotion
())
{
List
<
ChannelIndex
.
Article
>
articles
=
ChannelIndex
.
Record
.
filterArticles
(
startTime
,
endTime
,
channelRecord
.
getRecord
().
getArticles
());
articles
.
sort
(
Comparator
.
comparingLong
(
ChannelIndex
.
Article
::
getTime
).
reversed
());
channelRecord
.
getRecord
().
setArticles
(
articles
);
return
channelRecord
;
}
return
null
;
}).
filter
(
Objects:
:
nonNull
).
sorted
((
x
,
y
)
->
{
// emotionIndex降序
if
(
null
!=
sorter
&&
sorter
.
contains
(
"index"
))
{
if
(
x
.
getEmotionIndex
()
>
y
.
getEmotionIndex
())
{
return
-
1
;
}
else
if
(
Objects
.
equals
(
x
.
getEmotionIndex
(),
y
.
getEmotionIndex
()))
{
return
0
;
}
return
1
;
}
// 稿件数排序
if
(
x
.
getRecord
().
getArticles
().
size
()
>
y
.
getRecord
().
getArticles
().
size
())
{
}
// 过滤掉不符合时间条件的数据并排序
List
<
ChannelRecord
>
channelRecords
=
keyMap
.
values
().
stream
().
map
(
pair
->
{
ChannelRecord
channelRecord
=
pair
.
getRight
();
// 情感过滤
if
(
emotion
==
EmotionEnum
.
ALL
.
getState
()
||
emotion
==
channelRecord
.
getEmotion
())
{
List
<
ChannelIndex
.
Article
>
articles
=
ChannelIndex
.
Record
.
filterArticles
(
startTime
,
endTime
,
channelRecord
.
getRecord
().
getArticles
());
articles
.
sort
(
Comparator
.
comparingLong
(
ChannelIndex
.
Article
::
getTime
).
reversed
());
channelRecord
.
getRecord
().
setArticles
(
articles
);
return
channelRecord
;
}
return
null
;
}).
filter
(
Objects:
:
nonNull
).
sorted
((
x
,
y
)
->
{
// emotionIndex降序
if
(
null
!=
sorter
&&
sorter
.
contains
(
"index"
))
{
if
(
x
.
getEmotionIndex
()
>
y
.
getEmotionIndex
())
{
return
-
1
;
}
else
if
(
Objects
.
equals
(
x
.
get
Record
().
getArticles
().
size
(),
y
.
getRecord
().
getArticles
().
size
()))
{
}
else
if
(
Objects
.
equals
(
x
.
get
EmotionIndex
(),
y
.
getEmotionIndex
()))
{
return
0
;
}
return
1
;
}).
limit
(
size
)
}
// 稿件数排序
if
(
x
.
getRecord
().
getArticles
().
size
()
>
y
.
getRecord
().
getArticles
().
size
())
{
return
-
1
;
}
else
if
(
Objects
.
equals
(
x
.
getRecord
().
getArticles
().
size
(),
y
.
getRecord
().
getArticles
().
size
()))
{
return
0
;
}
return
1
;
}).
limit
(
size
)
// .map(channelRecord -> {
// Channel channel = channelDao.queryUnique(channelRecord.getChannelFid());
// if (null != channel) {
...
...
@@ -502,22 +518,17 @@ public class ChannelServiceImpl implements ChannelService {
// return ChannelListVO.createFromChannel(channelRecord, channelRecord.getRecord().getArticles().size());
// }
// })
.
collect
(
Collectors
.
toList
());
Map
<
String
,
ChannelRecord
>
collect
=
Maps
.
uniqueIndex
(
channelRecords
,
ChannelRecord:
:
getChannelFid
);
Map
<
String
,
Channel
>
fidChannel
=
channelDao
.
queryUniqueAsync
(
collect
.
keySet
());
collect
.
forEach
((
fid
,
record
)
->
{
Channel
channel
=
fidChannel
.
get
(
fid
);
if
(
null
!=
channel
)
{
resList
.
add
(
ChannelListVO
.
createFromChannel
(
channel
,
record
.
getRecord
().
getArticles
().
size
()));
}
else
{
resList
.
add
(
ChannelListVO
.
createFromChannel
(
record
,
record
.
getRecord
().
getArticles
().
size
()));
}
});
// 配合天级缓存开启
redisUtil
.
setExpire
(
redisKey
,
JSON
.
toJSONString
(
resList
));
}
catch
(
IOException
e
)
{
ExceptionCast
.
cast
(
CommonCodeEnum
.
FAIL
,
"es查询异常"
);
}
.
collect
(
Collectors
.
toList
());
Map
<
String
,
ChannelRecord
>
collect
=
Maps
.
uniqueIndex
(
channelRecords
,
ChannelRecord:
:
getChannelFid
);
Map
<
String
,
Channel
>
fidChannel
=
channelDao
.
queryUniqueAsync
(
collect
.
keySet
());
collect
.
forEach
((
fid
,
record
)
->
{
Channel
channel
=
fidChannel
.
get
(
fid
);
if
(
null
!=
channel
)
{
resList
.
add
(
ChannelListVO
.
createFromChannel
(
channel
,
record
.
getRecord
().
getArticles
().
size
()));
}
else
{
resList
.
add
(
ChannelListVO
.
createFromChannel
(
record
,
record
.
getRecord
().
getArticles
().
size
()));
}
});
return
resList
;
}
...
...
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