Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
A
Aragami
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
Aragami
Commits
727fc464
Commit
727fc464
authored
Aug 03, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rename method and add destroy
parent
d76866b7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
103 additions
and
63 deletions
+103
-63
README.md
README.md
+10
-3
src/aragami.ts
src/aragami.ts
+49
-28
src/base-driver.ts
src/base-driver.ts
+2
-0
src/drivers/memory.ts
src/drivers/memory.ts
+7
-0
src/drivers/redis.ts
src/drivers/redis.ts
+4
-0
src/wrappers.ts
src/wrappers.ts
+23
-28
tests/sample.spec.ts
tests/sample.spec.ts
+8
-4
No files found.
README.md
View file @
727fc464
...
...
@@ -33,6 +33,9 @@ async function main() {
const
entries
=
await
aragami
.
entries
(
User
);
await
aragami
.
delete
(
user2
);
await
aragami
.
clear
(
User
);
// use cache
const
userOfCache
=
await
aragami
.
cache
(
User
,
'
John
'
,
()
=>
database
.
get
(
'
John
'
));
// wraps a function with cache.
const
readUser
=
aragami
.
wrap
(
...
...
@@ -40,15 +43,19 @@ async function main() {
(
name
:
string
)
=>
database
.
get
(
name
),
// For data fetching
(
name
)
=>
name
,
// Specify cache key.
);
const
userFromCache
=
await
readUser
(
'
John
'
);
// use lock
await
aragami
.
lock
([
user
,
// can be object
'
John
'
,
// or string
],
()
=>
database
.
save
(
user
))
// wraps a function with lock.
const
saveUser
=
aragami
.
l
ock
(
const
saveUser
=
aragami
.
useL
ock
(
(
user
:
User
)
=>
database
.
save
(
user
),
// Lock process
(
user
)
=>
[
user
],
// Specify lock key. Can be an array.
)
await
saveUser
(
user
);
}
...
...
src/aragami.ts
View file @
727fc464
...
...
@@ -144,26 +144,34 @@ export class Aragami {
return
entries
.
map
(([
key
,
buf
])
=>
[
key
,
this
.
decode
(
cl
,
buf
)]);
}
wrap
<
T
,
A
extends
any
[]
>
(
async
cache
<
T
>
(
cl
:
ClassType
<
T
>
,
keyOrMeta
:
string
|
T
,
cb
:
()
=>
Awaitable
<
T
>
,
)
{
const
key
=
await
this
.
getKey
(
keyOrMeta
,
cl
);
if
(
!
key
)
{
return
cb
();
}
const
cachedValue
=
await
this
.
get
(
cl
,
key
);
if
(
cachedValue
!=
null
)
{
return
cachedValue
;
}
const
value
=
await
cb
();
if
(
value
!=
null
)
{
await
this
.
set
(
value
,
{
key
,
prototype
:
cl
});
}
return
value
;
}
useCache
<
T
,
A
extends
any
[]
>
(
cl
:
ClassType
<
T
>
,
cb
:
(...
args
:
A
)
=>
Awaitable
<
T
>
,
keySource
:
(...
args
:
A
)
=>
Awaitable
<
string
|
T
>
,
)
{
return
async
(...
args
:
A
):
Promise
<
T
>
=>
{
const
keyMeta
=
await
keySource
(...
args
);
const
key
=
await
this
.
getKey
(
keyMeta
,
cl
);
if
(
!
key
)
{
return
cb
(...
args
);
}
const
cachedValue
=
await
this
.
get
(
cl
,
key
);
if
(
cachedValue
)
{
return
cachedValue
;
}
const
value
=
await
cb
(...
args
);
if
(
value
)
{
await
this
.
set
(
value
,
{
key
,
prototype
:
cl
});
}
return
value
;
return
this
.
cache
(
cl
,
keyMeta
,
()
=>
cb
(...
args
));
};
}
...
...
@@ -181,23 +189,36 @@ export class Aragami {
);
}
lock
<
A
extends
any
[],
R
>
(
async
lock
<
R
>
(
keys
:
MayBeArray
<
string
|
any
>
,
cb
:
()
=>
Awaitable
<
R
>
,
):
Promise
<
R
>
{
const
keyMeta
=
makeArray
(
keys
);
const
actualKeys
=
(
await
Promise
.
all
(
keyMeta
.
map
((
o
)
=>
this
.
getLockKeys
(
o
)))
).
flat
();
if
(
!
keys
.
length
)
{
return
cb
();
}
return
this
.
driver
.
lock
(
actualKeys
,
async
()
=>
await
cb
());
}
useLock
<
A
extends
any
[],
R
>
(
cb
:
(...
args
:
A
)
=>
R
,
keySource
:
(...
args
:
A
)
=>
Awaitable
<
MayBeArray
<
string
|
any
>>
,
)
{
return
async
(...
args
:
A
):
Promise
<
Awaited
<
R
>>
=>
{
const
keyMeta
=
makeArray
(
await
keySource
(...
args
));
const
keys
=
(
await
Promise
.
all
(
keyMeta
.
map
((
o
)
=>
this
.
getLockKeys
(
o
)))
).
flat
();
if
(
!
keys
.
length
)
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return
cb
(...
args
);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return
this
.
driver
.
lock
(
keys
,
async
()
=>
await
cb
(...
args
));
return
async
(...
args
:
A
)
=>
{
const
keys
=
await
keySource
(...
args
);
return
this
.
lock
(
keys
,
()
=>
cb
(...
args
));
};
}
async
start
()
{
return
this
.
driver
.
start
();
}
async
destroy
()
{
try
{
await
this
.
driver
.
destroy
();
}
catch
(
e
)
{}
}
}
src/base-driver.ts
View file @
727fc464
...
...
@@ -46,4 +46,6 @@ export class BaseDriver {
lock
<
R
>
(
keys
:
string
[],
cb
:
()
=>
Promise
<
R
>
)
{
return
cb
();
}
async
destroy
():
Promise
<
void
>
{
}
}
src/drivers/memory.ts
View file @
727fc464
...
...
@@ -6,6 +6,13 @@ export class MemoryDriver extends BaseDriver {
private
cacheMap
=
new
Map
<
string
,
LRUCache
<
string
,
Buffer
>>
();
private
betterLock
=
new
BetterLock
();
async
destroy
()
{
for
(
const
cache
of
this
.
cacheMap
.
values
())
{
cache
.
clear
();
}
this
.
cacheMap
.
clear
();
}
private
getCacheInstance
(
baseKey
:
string
)
{
if
(
!
this
.
cacheMap
.
has
(
baseKey
))
{
this
.
cacheMap
.
set
(
...
...
src/drivers/redis.ts
View file @
727fc464
...
...
@@ -62,4 +62,8 @@ export class RedisDriver extends BaseDriver {
cb
,
);
}
async
destroy
()
{
await
this
.
redis
.
quit
();
}
}
src/wrappers.ts
View file @
727fc464
...
...
@@ -23,52 +23,47 @@ export class WrapDecoratorBuilder {
const
{
aragamiFactory
}
=
this
;
return
{
UseLock
:
():
TypedMethodDecorator
<
(...
args
:
any
[])
=>
Awaitabl
e
<
any
>>
=>
():
TypedMethodDecorator
<
(...
args
:
any
[])
=>
Promis
e
<
any
>>
=>
(
obj
,
key
,
des
)
=>
{
const
oldFun
=
des
.
value
;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
des
.
value
=
async
function
(...
args
)
{
const
aragami
=
await
aragamiFactory
(
this
);
const
wrapped
=
aragami
.
lock
(
()
=>
oldFun
.
apply
(
this
,
args
),
async
()
=>
{
const
lockKeyParams
=
await
reflector
.
getArray
(
'
AragamiWithLockKey
'
,
this
,
key
,
);
return
(
await
Promise
.
all
(
_
.
compact
(
lockKeyParams
.
map
(
async
(
fun
,
i
)
=>
{
if
(
!
fun
)
return
;
const
keyResult
=
(
await
fun
(
args
[
i
],
this
,
key
as
string
,
))
as
MayBeArray
<
any
>
;
return
makeArray
(
keyResult
);
}),
),
)
).
flat
();
},
const
lockKeyParams
=
reflector
.
getArray
(
'
AragamiWithLockKey
'
,
this
,
key
,
);
return
wrapped
();
const
keys
=
(
await
Promise
.
all
(
_
.
compact
(
lockKeyParams
.
map
(
async
(
fun
,
i
)
=>
{
if
(
!
fun
)
return
;
const
keyResult
=
(
await
fun
(
args
[
i
],
this
,
key
as
string
,
))
as
MayBeArray
<
any
>
;
return
makeArray
(
keyResult
);
}),
),
)
).
flat
();
return
aragami
.
lock
(
keys
,
()
=>
oldFun
.
apply
(
this
,
args
));
};
},
UseCache
:
<
T
>
(
cl
:
ClassType
<
T
>
,
):
TypedMethodDecorator
<
(...
args
:
any
[])
=>
Awaitabl
e
<
T
>>
=>
):
TypedMethodDecorator
<
(...
args
:
any
[])
=>
Promis
e
<
T
>>
=>
(
obj
,
key
,
des
)
=>
{
const
oldFun
=
des
.
value
;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
des
.
value
=
async
function
(...
args
)
{
const
aragami
=
await
aragamiFactory
(
this
);
const
wrapped
=
aragami
.
wrap
<
T
,
[]
>
(
const
wrapped
=
aragami
.
useCache
<
T
,
[]
>
(
cl
,
()
=>
oldFun
.
apply
(
this
,
args
),
async
()
=>
{
...
...
tests/sample.spec.ts
View file @
727fc464
...
...
@@ -7,10 +7,14 @@ describe('Aragami.', () => {
beforeEach
(()
=>
{
aragami
=
new
Aragami
({
//
redis: { uri: 'redis://localhost:6379' },
redis
:
{
uri
:
'
redis://localhost:6379
'
},
});
});
afterEach
(
async
()
=>
{
await
aragami
.
destroy
();
});
it
(
'
Should store value
'
,
async
()
=>
{
@
CachePrefix
(
'
user
'
)
@
CacheTTL
(
100
)
...
...
@@ -50,7 +54,7 @@ describe('Aragami.', () => {
await
expect
(
aragami
.
del
(
User
,
'
n.John
'
)).
resolves
.
toBeTruthy
();
await
expect
(
aragami
.
has
(
user
)).
resolves
.
toBeFalsy
();
const
wrapped
=
aragami
.
wrap
(
const
wrapped
=
aragami
.
useCache
(
User
,
(
name
:
string
,
age
:
number
)
=>
{
const
user
=
new
User
();
...
...
@@ -85,7 +89,7 @@ describe('Aragami.', () => {
});
it
(
'
should lock
'
,
async
()
=>
{
const
fun
=
aragami
.
l
ock
(
const
fun
=
aragami
.
useL
ock
(
async
(
foo
:
string
,
bar
:
string
)
=>
{
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
1000
));
return
`
${
foo
}
.
${
bar
}
`
;
...
...
@@ -110,7 +114,7 @@ describe('Aragami.', () => {
class
MyService
{
@
UseCache
(
Book
)
getBook
(@
WithKey
()
title
:
string
,
content
:
string
)
{
async
getBook
(@
WithKey
()
title
:
string
,
content
:
string
)
{
const
book
=
new
Book
();
book
.
title
=
title
;
book
.
content
=
content
;
...
...
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