Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pisns-forum-api
Project
Project
Details
Activity
Releases
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
scrmGroup
pisns-forum-api
Commits
515409d4
Commit
515409d4
authored
Mar 18, 2016
by
刘文胜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
缓存cacheable
parent
721ae3d6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
21 deletions
+38
-21
cacheConfig.js
app/utils/cacheConfig.js
+1
-1
cacheable.js
app/utils/cacheable.js
+37
-20
No files found.
app/utils/cacheConfig.js
View file @
515409d4
var
cache
=
require
(
'./cacheable'
);
var
forumAboutMEService
=
require
(
'../service/forumAboutMEService'
);
cache
.
cacheable
(
forumAboutMEService
,
'me2other'
,
{
ns
:
'forum.service.forumAboutMEService
222'
,
dur
:
1000
0
});
cache
.
cacheable
(
forumAboutMEService
,
'me2other'
,
{
ns
:
'forum.service.forumAboutMEService
.me2other5'
,
dur
:
1
0
});
//cache.clear(forumAboutMEService,'me2other','forum.service.forumAboutMEService222')
\ No newline at end of file
app/utils/cacheable.js
View file @
515409d4
...
...
@@ -5,36 +5,53 @@ var async=require('async');
/**=====<1.缓存实现开始==========**/
function
toCache
(
dur
,
k
,
v
)
{
if
(
v
){
var
str
=
''
;
try
{
str
=
JSON
.
stringify
(
v
);
if
(
str
){
redis
.
hset
(
dur
.
ns
,
k
,
str
,
function
(
err
)
{
redis
.
expire
(
k
,
dur
.
dur
);
});
redis
.
hset
(
dur
.
ns
,
k
,
1
,
function
(
err
)
{
if
(
!
err
){
redis
.
expire
(
dur
.
ns
,
dur
.
dur
);
var
str
=
''
;
try
{
str
=
JSON
.
stringify
(
v
);
}
catch
(
e
){
console
.
log
(
e
);
}
if
(
str
){
redis
.
set
(
k
,
str
,
function
(
err
){
if
(
!
err
){
console
.
log
(
'缓存过期时间:'
,
dur
.
dur
);
redis
.
expire
(
k
,
dur
.
dur
);
}
});
}
}
}
catch
(
e
){
console
.
log
(
e
);
}
});
}
}
function
fromCache
(
dur
,
k
,
cb
){
redis
.
hget
(
dur
.
ns
,
k
,
function
(
err
,
v
){
redis
.
hget
(
dur
.
ns
,
k
,
function
(
err
,
hit
){
if
(
err
){
return
cb
(
err
);
console
.
log
(
err
);
return
cb
();
}
if
(
!
hit
){
return
cb
();
}
if
(
v
){
redis
.
get
(
k
,
function
(
err
,
data
){
if
(
err
){
console
.
log
(
err
);
return
cb
();
}
if
(
!
data
){
return
cb
();
}
var
obj
=
null
;
try
{
obj
=
JSON
.
parse
(
v
);
cb
(
null
,
obj
);
obj
=
JSON
.
parse
(
data
);
}
catch
(
e
){
c
b
(
e
);
c
onsole
.
log
(
err
);
}
}
else
{
cb
(
null
,
null
);
}
cb
(
null
,
obj
);
});
});
}
...
...
@@ -130,7 +147,7 @@ module.exports = {
throw
'opts.ns 不能为空'
;
}
if
(
!
opts
.
dur
||
isNaN
(
opts
.
dur
)){
//默认缓存一小时
opts
.
dur
=
1000
*
60
*
60
;
opts
.
dur
=
60
*
60
;
}
m
[
fn
]
=
cacheable
(
m
[
fn
],
opts
);
},
...
...
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