Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yizhi_server
Project
Project
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
data_server
yizhi_server
Commits
2005bb33
Commit
2005bb33
authored
Apr 21, 2020
by
姜登
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis
parent
60814cd2
Pipeline
#21937
passed with stage
in 2 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
1 addition
and
55 deletions
+1
-55
application.js
app/extend/application.js
+1
-55
No files found.
app/extend/application.js
View file @
2005bb33
...
...
@@ -17,11 +17,7 @@ class Cache {
if
(
data
)
{
return
data
;
}
if
(
next
instanceof
Promise
)
{
data
=
await
next
;
}
else
{
data
=
await
next
(
key
);
}
data
=
await
next
(
key
);
await
this
.
set
(
key
,
data
,
ttl
);
return
data
;
}
...
...
@@ -44,18 +40,9 @@ class Cache {
this
.
app
.
logger
.
info
(
`[cache](
${
+
new
Date
()
-
startTime
}
ms)
${
this
.
name
}
.
${
key
}
:
${
jsonText
}
`
);
return
ret
;
}
async
set
(
key
,
value
,
ttl
=
60
)
{
return
await
this
.
_set
(
key
,
value
,
ttl
);
}
async
ttl
(
key
)
{
return
await
this
.
_ttl
(
key
);
}
async
lock
(
key
,
ttl
,
fn
)
{
return
await
this
.
_lock
(
key
,
ttl
,
fn
);
}
}
class
RedisCacheWrap
extends
Cache
{
...
...
@@ -63,10 +50,6 @@ class RedisCacheWrap extends Cache {
super
(
app
);
this
.
redis
=
redis
;
this
.
name
=
'redis-cache'
;
redis
.
defineCommand
(
'lock'
,
{
numberOfKeys
:
1
,
lua
:
'return redis.call("set", KEYS[1], ARGV[1], "NX", "EX", ARGV[2])'
,
});
}
async
_get
(
key
)
{
...
...
@@ -89,40 +72,6 @@ class RedisCacheWrap extends Cache {
await
redis
.
set
(
key
,
value
);
}
}
async
_ttl
(
key
)
{
const
{
redis
}
=
this
;
const
r
=
await
redis
.
ttl
(
key
);
return
r
<
0
?
undefined
:
r
;
}
async
_lock
(
key
,
ttl
,
fn
)
{
const
{
app
,
redis
}
=
this
;
const
redisLockName
=
app
.
name
+
'/'
+
app
.
config
.
env
+
'/lock/'
+
key
;
// 获取当前时间秒和截止时间
const
currentTime
=
Math
.
round
(
new
Date
().
getTime
()
/
1000
);
const
expireTime
=
+
ttl
+
currentTime
;
// 加锁lua脚本,key存在返回null,不存在则设置key-value并返回OK
const
lock
=
await
redis
.
lock
(
redisLockName
,
expireTime
,
ttl
);
if
(
!
lock
)
{
// 如存在锁判断是否超时
const
preLockTime
=
+
await
redis
.
get
(
redisLockName
);
if
(
preLockTime
>
currentTime
)
{
throw
Error
(
'系统繁忙,请稍后再试!'
);
}
// 原子方法getset,重新设置值,并返回之前设置的值
// 如超时重新设置value,并再次判断是否有其他进程获取到锁
const
resetLockTime
=
+
await
redis
.
getset
(
redisLockName
,
expireTime
);
if
(
resetLockTime
!==
preLockTime
)
{
throw
Error
(
'系统繁忙,请稍后重试!'
);
// 表示有其他进程获取到锁
}
}
// 执行fn逻辑并删除锁
try
{
return
await
fn
.
apply
(
this
,
[]);
}
finally
{
await
redis
.
del
(
redisLockName
);
}
}
}
class
NodeCacheWrap
extends
Cache
{
...
...
@@ -144,9 +93,6 @@ class NodeCacheWrap extends Cache {
cache
.
set
(
key
,
value
);
}
}
async
_ttl
(
key
)
{
return
this
.
cache
.
getTtl
(
key
);
}
}
module
.
exports
=
{
...
...
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