Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录
切换导航
N
nestjs-redis
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
王智锐 (C)
nestjs-redis
Commits
29907003
Unverified
提交
29907003
authored
3月 16, 2020
作者:
ziyun.zhu
提交者:
GitHub
3月 16, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39 from polyakovoleg/custom_error_handler
feat: add custom event handler
上级
7bb40209
a8d7ad21
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
36 行增加
和
31 行删除
+36
-31
README.md
README.md
+10
-1
redis-client.provider.ts
lib/redis-client.provider.ts
+23
-27
redis.interface.ts
lib/redis.interface.ts
+3
-3
没有找到文件。
README.md
浏览文件 @
29907003
...
@@ -61,6 +61,15 @@ export default {
...
@@ -61,6 +61,15 @@ export default {
url
:
'redis://:authpassword@127.0.0.1:6380/4'
,
url
:
'redis://:authpassword@127.0.0.1:6380/4'
,
}
}
```
```
With custom error handler
```
typescript
export
default
{
url
:
'redis://:authpassword@127.0.0.1:6380/4'
,
onClientReady
:
(
client
)
=>
{
client
.
on
(
'error'
,
(
err
)
=>
{}
)},
}
```
With multi client
With multi client
```
typescript
```
typescript
export
default
[
export
default
[
...
@@ -197,4 +206,4 @@ interface RedisOptions {
...
@@ -197,4 +206,4 @@ interface RedisOptions {
showFriendlyErrorStack
?:
boolean
;
showFriendlyErrorStack
?:
boolean
;
}
}
```
```
That's it!
That's it!
\ No newline at end of file
lib/redis-client.provider.ts
浏览文件 @
29907003
import
*
as
Redis
from
'ioredis'
;
import
*
as
Redis
from
'ioredis'
;
import
*
as
uuid
from
'uuid'
;
import
*
as
uuid
from
'uuid'
;
import
{
Provider
}
from
'@nestjs/common'
;
import
{
REDIS_CLIENT
,
REDIS_MODULE_OPTIONS
}
from
'./redis.constants'
;
import
{
REDIS_CLIENT
,
REDIS_MODULE_OPTIONS
}
from
'./redis.constants'
;
import
{
RedisModuleAsyncOptions
,
RedisModuleOptions
}
from
'./redis.interface'
;
import
{
RedisModuleAsyncOptions
,
RedisModuleOptions
}
from
'./redis.interface'
;
...
@@ -11,40 +12,35 @@ export interface RedisClient {
...
@@ -11,40 +12,35 @@ export interface RedisClient {
size
:
number
;
size
:
number
;
}
}
export
const
createClient
=
()
=>
({
async
function
getClient
(
options
:
RedisModuleOptions
):
Promise
<
Redis
.
Redis
>
{
const
{
onClientReady
,
url
,
...
opt
}
=
options
;
const
client
=
url
?
new
Redis
(
url
)
:
new
Redis
(
opt
);
if
(
onClientReady
)
{
onClientReady
(
client
)
}
return
client
;
}
export
const
createClient
=
():
Provider
=>
({
provide
:
REDIS_CLIENT
,
provide
:
REDIS_CLIENT
,
useFactory
:
(
options
:
RedisModuleOptions
|
RedisModuleOptions
[])
=>
{
useFactory
:
async
(
options
:
RedisModuleOptions
|
RedisModuleOptions
[]):
Promise
<
RedisClient
>
=>
{
const
clients
=
new
Map
<
string
,
Redis
.
Redis
>
();
const
clients
=
new
Map
<
string
,
Redis
.
Redis
>
();
const
defaultKey
=
uuid
();
const
defaultKey
=
uuid
();
if
(
Array
.
isArray
(
options
))
{
if
(
Array
.
isArray
(
options
))
{
for
(
const
o
of
options
)
{
await
Promise
.
all
(
if
(
o
.
name
)
{
options
.
map
(
async
o
=>
{
if
(
clients
.
has
(
o
.
name
))
{
const
key
=
o
.
name
||
defaultKey
;
throw
new
RedisClientError
(
`client
${
o
.
name
}
is exists`
);
if
(
clients
.
has
(
key
))
{
}
throw
new
RedisClientError
(
`
${
o
.
name
||
'default'
}
client is exists`
);
if
(
o
.
url
)
{
clients
.
set
(
o
.
name
,
new
Redis
(
o
.
url
));
}
else
{
clients
.
set
(
o
.
name
,
new
Redis
(
o
));
}
}
}
else
{
clients
.
set
(
key
,
await
getClient
(
o
));
if
(
clients
.
has
(
defaultKey
))
{
}),
throw
new
RedisClientError
(
'default client is exists'
);
);
}
if
(
o
.
url
)
{
clients
.
set
(
defaultKey
,
new
Redis
(
o
.
url
));
}
else
{
clients
.
set
(
defaultKey
,
new
Redis
(
o
));
}
}
}
}
else
{
}
else
{
if
(
options
.
url
)
{
clients
.
set
(
defaultKey
,
await
getClient
(
options
));
clients
.
set
(
defaultKey
,
new
Redis
(
options
.
url
));
}
else
{
clients
.
set
(
defaultKey
,
new
Redis
(
options
));
}
}
}
return
{
return
{
defaultKey
,
defaultKey
,
clients
,
clients
,
...
...
lib/redis.interface.ts
浏览文件 @
29907003
import
{
ModuleMetadata
}
from
'@nestjs/common/interfaces'
;
import
{
ModuleMetadata
}
from
'@nestjs/common/interfaces'
;
import
{
RedisOptions
}
from
'ioredis'
;
import
{
Redis
,
Redis
Options
}
from
'ioredis'
;
export
interface
RedisModuleOptions
extends
RedisOptions
{
export
interface
RedisModuleOptions
extends
RedisOptions
{
name
?:
string
;
name
?:
string
;
url
?:
string
;
url
?:
string
;
onClientReady
?(
client
:
Redis
):
Promise
<
void
>
;
}
}
export
interface
RedisModuleAsyncOptions
export
interface
RedisModuleAsyncOptions
extends
Pick
<
ModuleMetadata
,
'imports'
>
{
extends
Pick
<
ModuleMetadata
,
'imports'
>
{
useFactory
?:
(
useFactory
?:
(
...
args
:
any
[]
...
args
:
any
[]
)
=>
)
=>
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论