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
80025161
Commit
80025161
authored
Mar 18, 2016
by
strong
Browse files
Options
Browse Files
Download
Plain Diff
Merge commit '
eb2843d1
' into SANDBOX
parents
4e899005
eb2843d1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
111 additions
and
69 deletions
+111
-69
app.js
app.js
+1
-0
cacheConfig.js
app/utils/cacheConfig.js
+0
-6
cacheable.js
app/utils/cacheable.js
+36
-20
loadUserLevel.js
app/utils/loadUserLevel.js
+26
-0
util.js
app/utils/util.js
+40
-43
cacheConfig.js
config/cacheConfig.js
+8
-0
No files found.
app.js
View file @
80025161
...
...
@@ -39,4 +39,5 @@ mongoModels.forEach(function(model) {
global
.
mongodb
=
mongodb
;
require
(
'./config/express'
)(
app
,
config
);
require
(
'./config/cacheConfig'
);
app
.
listen
(
config
.
port
);
\ No newline at end of file
app/utils/cacheConfig.js
deleted
100644 → 0
View file @
4e899005
var
cache
=
require
(
'./cacheable'
);
var
forumAboutMEService
=
require
(
'../service/forumAboutMEService'
);
cache
.
cacheable
(
forumAboutMEService
,
'me2other'
,
{
ns
:
'forum.service.forumAboutMEService222'
,
dur
:
10000
});
//cache.clear(forumAboutMEService,'me2other','forum.service.forumAboutMEService222')
\ No newline at end of file
app/utils/cacheable.js
View file @
80025161
...
...
@@ -5,36 +5,52 @@ 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
){
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 +146,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
);
},
...
...
app/utils/loadUserLevel.js
0 → 100644
View file @
80025161
var
_
=
require
(
'lodash'
);
var
request
=
require
(
'request'
);
var
env
=
process
.
env
.
NODE_ENV
;
var
API_ADDRESS
=
'http://localhost:8080'
;
if
(
env
==
'sandbox'
)
{
API_ADDRESS
=
'http://rest.wxpai.cn'
;
}
else
if
(
env
==
'production'
)
{
API_ADDRESS
=
'https://rest.wxpai.cn'
;
}
exports
.
loadLevelFromAPI
=
function
(
ent_code
,
open_id
,
callback
){
if
(
!
ent_code
||
!
open_id
){
return
callback
&&
callback
(
null
,
null
);
}
var
url
=
API_ADDRESS
+
'/v1.0/internal/member/exp/byopenid?openId='
+
open_id
+
'&entCode='
+
ent_code
;
request
.
get
({
url
:
url
,
json
:
{}
},
function
(
e
,
r
,
body
)
{
if
(
e
)
{
console
.
log
(
e
)
}
return
callback
&&
callback
(
null
,(
body
&&
body
.
data
)
||
null
);
});
};
\ No newline at end of file
app/utils/util.js
View file @
80025161
'use strict'
;
var
_
=
require
(
'lodash'
);
var
request
=
require
(
'request'
);
var
env
=
process
.
env
.
NODE_ENV
;
var
API_ADDRESS
=
'http://localhost:8080'
;
if
(
env
==
'sandbox'
)
{
API_ADDRESS
=
'http://rest.wxpai.cn'
;
}
else
if
(
env
==
'production'
)
{
API_ADDRESS
=
'https://rest.wxpai.cn'
;
}
var
loadUserLevel
=
require
(
'./loadUserLevel'
);
var
async
=
require
(
'async'
);
exports
.
loadLevel
=
function
(
ent_code
,
items
,
callback
){
var
openIds
=
[];
...
...
@@ -30,33 +24,35 @@ exports.loadLevel=function(ent_code,items,callback){
}
});
request
.
post
({
url
:
API_ADDRESS
+
'/v1.0/internal/member/list/byopenids'
,
json
:
{
"entCode"
:
ent_code
,
"openIds"
:
openIds
}
},
function
(
e
,
r
,
body
)
{
if
(
body
&&
body
.
data
)
{
var
tasks
=
[];
_
.
forEach
(
openIds
,
function
(
open_id
)
{
tasks
.
push
(
function
(
cont
)
{
loadUserLevel
.
loadLevelFromAPI
(
ent_code
,
open_id
,
cont
);
});
});
async
.
parallel
(
tasks
,
function
(
err
,
results
)
{
if
(
results
&&
results
.
length
>
0
)
{
//results: [{'egwegweg':1},{'gergergerg':3}]
_
.
forEach
(
items
,
function
(
d
,
i
)
{
if
(
items
[
i
].
toObject
)
{
items
[
i
]
=
items
[
i
].
toObject
();
}
_
.
forEach
(
body
.
data
,
function
(
r
,
j
)
{
if
(
d
.
from
&&
d
.
from
.
uid
===
r
.
user
.
openId
){
items
[
i
].
from
.
exp
=
r
.
exp
;
}
if
(
d
.
to
&&
d
.
to
.
uid
===
r
.
user
.
openId
){
items
[
i
].
to
.
exp
=
r
.
exp
;
}
if
(
d
.
commentLevel1From
&&
d
.
commentLevel1From
.
uid
===
r
.
user
.
openId
){
items
[
i
].
commentLevel1From
.
exp
=
r
.
exp
;
}
if
(
d
.
commentLevel2From
&&
d
.
commentLevel2From
.
uid
===
r
.
user
.
openId
){
items
[
i
].
commentLevel2From
.
exp
=
r
.
exp
;
}
if
(
d
.
commentLevel2ThreadFrom
&&
d
.
commentLevel2ThreadFrom
.
uid
===
r
.
user
.
openId
){
items
[
i
].
commentLevel2ThreadFrom
.
exp
=
r
.
exp
;
_
.
forEach
(
results
,
function
(
r
,
j
)
{
if
(
r
){
if
(
d
.
from
&&
d
.
from
.
uid
&&
r
[
d
.
from
.
uid
]){
items
[
i
].
from
.
exp
=
r
[
d
.
from
.
uid
];
}
if
(
d
.
to
&&
d
.
to
.
uid
&&
r
[
d
.
to
.
uid
]){
items
[
i
].
to
.
exp
=
r
[
d
.
to
.
uid
];
}
if
(
d
.
commentLevel1From
&&
d
.
commentLevel1From
.
uid
&&
r
[
d
.
commentLevel1From
.
uid
]){
items
[
i
].
commentLevel1From
.
exp
=
r
[
d
.
commentLevel1From
.
uid
];
}
if
(
d
.
commentLevel2From
&&
d
.
commentLevel2From
.
uid
&&
r
[
d
.
commentLevel2From
.
uid
]){
items
[
i
].
commentLevel2From
.
exp
=
r
[
d
.
commentLevel2From
.
uid
];
}
if
(
d
.
commentLevel2ThreadFrom
&&
d
.
commentLevel2ThreadFrom
.
uid
&&
r
[
d
.
commentLevel2ThreadFrom
.
uid
]){
items
[
i
].
commentLevel2ThreadFrom
.
exp
=
r
[
d
.
commentLevel2ThreadFrom
.
uid
];
}
}
});
...
...
@@ -88,22 +84,23 @@ exports.loadLevelByUser=function(ent_code,items,callback){
openIds
.
push
(
d
.
uid
);
}
});
request
.
post
({
url
:
API_ADDRESS
+
'/v1.0/internal/member/list/byopenids'
,
json
:
{
"entCode"
:
ent_code
,
"openIds"
:
openIds
}
},
function
(
e
,
r
,
body
)
{
if
(
body
&&
body
.
data
)
{
var
tasks
=
[];
_
.
forEach
(
openIds
,
function
(
open_id
)
{
tasks
.
push
(
function
(
cont
){
loadUserLevel
.
loadLevelFromAPI
(
ent_code
,
open_id
,
cont
);
});
});
async
.
parallel
(
tasks
,
function
(
err
,
results
){
if
(
results
&&
results
.
length
>
0
)
{
//results: [{'egwegweg':1},{'gergergerg':3}]
_
.
forEach
(
items
,
function
(
d
,
i
)
{
if
(
items
[
i
].
toObject
)
{
items
[
i
]
=
items
[
i
].
toObject
();
}
_
.
forEach
(
body
.
data
,
function
(
r
,
j
)
{
if
(
d
.
uid
===
r
.
user
.
openId
){
items
[
i
].
exp
=
r
.
exp
;
_
.
forEach
(
results
,
function
(
r
)
{
if
(
r
){
if
(
d
.
uid
&&
r
[
d
.
uid
]){
items
[
i
].
exp
=
r
[
d
.
uid
];
}
}
});
...
...
config/cacheConfig.js
0 → 100644
View file @
80025161
var
cache
=
require
(
'./../app/utils/cacheable'
);
var
forumAboutMEService
=
require
(
'./../app/service/forumAboutMEService'
);
var
loadUserLevel
=
require
(
'./../app/utils/loadUserLevel'
);
//cache.cacheable(forumAboutMEService, 'me2other', { ns: 'forum.service.forumAboutMEService.me2other', dur: 60 * 10 });
cache
.
cacheable
(
loadUserLevel
,
'loadLevelFromAPI'
,
{
ns
:
'forum.utils.loadUserLevel.loadLevelFromAPI'
,
dur
:
60
*
60
});
//cache.clear(service1,'method1','forum.service.service1.method1')
\ No newline at end of file
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