Toggle navigation
Toggle navigation
This project
Loading...
Sign in
yoho-search
/
yoho-search-service
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
foxxy
8 years ago
Commit
696d314be4b97d4dad7a040d571b8d8e2440b3d9
1 parent
620bb3f1
切面压缩
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
3 deletions
service/src/main/java/com/yoho/search/common/compress/aop/CommpressAble.java
service/src/main/java/com/yoho/search/common/compress/aop/CommpressAspect.java
service/src/main/java/com/yoho/search/restapi/scene/SearchLikeSecneController.java
service/src/main/java/com/yoho/search/service/scene/searchlike/SearchLikeNotInShopService.java
service/src/main/java/com/yoho/search/common/compress/aop/CommpressAble.java
0 → 100644
View file @
696d314
package
com
.
yoho
.
search
.
common
.
compress
.
aop
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
({
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
CommpressAble
{
public
boolean
isCommpress
()
default
false
;
}
...
...
service/src/main/java/com/yoho/search/common/compress/aop/CommpressAspect.java
0 → 100644
View file @
696d314
package
com
.
yoho
.
search
.
common
.
compress
.
aop
;
import
java.util.Map
;
import
javax.servlet.http.HttpServletRequest
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
com.yoho.search.common.utils.HttpServletRequestUtils
;
import
com.yoho.search.common.utils.SnappyUtils
;
import
com.yoho.search.models.SearchApiResult
;
@Component
@Aspect
public
class
CommpressAspect
{
// 获取请求的参数
private
boolean
isCommpress
()
{
HttpServletRequest
httpServletRequest
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
Map
<
String
,
String
>
paramMap
=
HttpServletRequestUtils
.
transParamType
(
httpServletRequest
);
if
(
paramMap
.
containsKey
(
"isDebug"
)){
return
false
;
}
return
true
;
}
@Around
(
"@annotation(com.yoho.search.common.compress.aop.CommpressAble)"
)
public
Object
downGrade
(
ProceedingJoinPoint
pjp
)
throws
Throwable
{
// 获取方法签名
MethodSignature
signature
=
(
MethodSignature
)
pjp
.
getSignature
();
final
Class
<?>
returnType
=
signature
.
getMethod
().
getReturnType
();
if
(!
returnType
.
isAssignableFrom
(
SearchApiResult
.
class
))
{
return
pjp
.
proceed
();
}
// 从降级缓存中可以获取结果,则直接返回[说明此刻是降级状态]
Object
result
=
pjp
.
proceed
();
if
(
result
==
null
)
{
return
result
;
}
CommpressAble
commpressAble
=
signature
.
getMethod
().
getAnnotation
(
CommpressAble
.
class
);
SearchApiResult
searchApiResult
=(
SearchApiResult
)
result
;
if
(
commpressAble
.
isCommpress
()
&&
isCommpress
()){
searchApiResult
.
setData
(
SnappyUtils
.
compress
(
searchApiResult
.
getData
())).
setCompress
(
true
);
}
return
searchApiResult
;
}
}
...
...
service/src/main/java/com/yoho/search/restapi/scene/SearchLikeSecneController.java
View file @
696d314
...
...
@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
com.yoho.search.common.compress.aop.CommpressAble
;
import
com.yoho.search.common.downgrade.aop.DownGradeAble
;
import
com.yoho.search.common.utils.HttpServletRequestUtils
;
import
com.yoho.search.models.SearchApiResult
;
...
...
@@ -50,6 +51,7 @@ public class SearchLikeSecneController {
}
//@DownGradeAble(key = "searchLikeNotInShop")
@CommpressAble
(
isCommpress
=
true
)
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/searchLikeNotInShop"
)
@ResponseBody
public
SearchApiResult
searchLikeNotInShop
(
HttpServletRequest
request
)
{
...
...
service/src/main/java/com/yoho/search/service/scene/searchlike/SearchLikeNotInShopService.java
View file @
696d314
...
...
@@ -19,7 +19,6 @@ import org.springframework.stereotype.Service;
import
com.alibaba.fastjson.JSONObject
;
import
com.yoho.search.common.cache.model.SearchCache
;
import
com.yoho.search.common.utils.SnappyUtils
;
import
com.yoho.search.core.es.model.SearchParam
;
import
com.yoho.search.models.SearchApiResult
;
import
com.yoho.search.service.base.SearchDynamicConfigService
;
...
...
@@ -70,7 +69,7 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
String
redisCacheKey
=
searchCacheKeyHelper
.
getSearchLikeNotInShopKeyCache
(
productSkn
,
pageSize
);
JSONObject
cacheObject
=
searchCacheService
.
getJSONObjectFromCache
(
this
.
searchCache
,
redisCacheKey
);
if
(
cacheObject
!=
null
)
{
return
new
SearchApiResult
().
setData
(
SnappyUtils
.
compress
(
cacheObject
)).
setCompress
(
true
);
return
new
SearchApiResult
().
setData
(
cacheObject
);
}
// 4、获取当前查询的SKN的基本信息
...
...
@@ -106,7 +105,7 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
// 8、结果加入缓存
searchCacheService
.
addJSONObjectToCache
(
this
.
searchCache
,
redisCacheKey
,
result
);
return
new
SearchApiResult
().
setData
(
SnappyUtils
.
compress
(
result
)).
setCompress
(
true
);
return
new
SearchApiResult
().
setData
(
result
);
}
catch
(
Exception
e
)
{
logger
.
error
(
e
.
getMessage
(),
e
);
return
new
SearchApiResult
().
setData
(
null
).
setMessage
(
"searchLikeNotInShop Exception"
).
setCode
(
500
);
...
...
Please
register
or
login
to post a comment