Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
yohobuy-node
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
1
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Plain Diff
Browse Files
Authored by
周少峰
8 years ago
Commit
6f997b7e4e22072904f0e1fcce59bd9a13ff1fd3
2 parents
e657b21e
97db439f
Merge branch 'hotfix/add-cache' into gray
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
3 deletions
apps/product/models/detail-product-api.js
apps/product/models/detail-product-api.js
View file @
6f997b7
...
...
@@ -7,6 +7,43 @@
const
api
=
global
.
yoho
.
API
;
const
config
=
global
.
yoho
.
config
;
const
redis
=
global
.
yoho
.
redis
;
const
cache
=
global
.
yoho
.
cache
;
const
logger
=
global
.
yoho
.
logger
;
const
uuid
=
require
(
'uuid'
);
function
_cacheGet
(
key
)
{
return
cache
.
get
(
key
).
then
((
data
)
=>
{
if
(
data
)
{
return
JSON
.
parse
(
data
);
}
return
Promise
.
reject
();
});
}
// 半个月
const
HALF_MONTH
=
60
*
60
*
24
*
15
;
function
_cacheSave
(
key
,
value
)
{
return
cache
.
set
(
key
,
value
,
HALF_MONTH
)
.
catch
(
err
=>
logger
.
error
(
`
save
cache
data
fail
:
$
{
err
.
toString
()}
`
));
}
function
_cached
(
fn
,
ctx
)
{
const
pre
=
'recommend-cache:'
+
(
fn
.
name
||
'random:'
+
uuid
.
v4
())
+
':'
;
return
function
()
{
const
args
=
Array
.
prototype
.
slice
.
call
(
arguments
);
const
key
=
pre
+
JSON
.
stringify
(
args
||
'[]'
);
return
_cacheGet
(
key
).
catch
(()
=>
{
return
fn
.
apply
(
ctx
,
args
).
then
(
result
=>
{
_cacheSave
(
key
,
result
);
return
result
;
});
});
};
}
/**
* 商品的 banner
...
...
@@ -159,11 +196,11 @@ const getLikeAsync = (skn, limit) => {
};
// 根据small_sort从redis获取分类下的关键词
const
getRecommendKeywords
=
(
smallSort
)
=>
{
function
_getRecommendKeywords
(
smallSort
)
{
return
redis
.
all
([[
'get'
,
`
global
:
yoho
:
seo
:
keywords
:
sortId
:
$
{
smallSort
}:
page
:
1
`
]]).
then
(
res
=>
{
return
res
[
0
];
});
}
;
}
module
.
exports
=
{
getProductBannerAsync
,
...
...
@@ -177,5 +214,5 @@ module.exports = {
getShopRecommendAsync
,
getBundleAsync
,
getLikeAsync
,
getRecommendKeywords
getRecommendKeywords
:
_cached
(
_getRecommendKeywords
)
};
...
...
Please
register
or
login
to post a comment