Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
eb9e6d23
Commit
eb9e6d23
authored
3 years ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
perf(server): reduce db query
parent
aebd77ad
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/backend/src/misc/cache.ts
+14
-3
14 additions, 3 deletions
packages/backend/src/misc/cache.ts
packages/backend/src/models/repositories/user.ts
+10
-2
10 additions, 2 deletions
packages/backend/src/models/repositories/user.ts
with
24 additions
and
5 deletions
packages/backend/src/misc/cache.ts
+
14
−
3
View file @
eb9e6d23
...
...
@@ -28,11 +28,22 @@ export class Cache<T> {
this
.
cache
.
delete
(
key
);
}
public
async
fetch
(
key
:
string
|
null
,
fetcher
:
()
=>
Promise
<
T
>
):
Promise
<
T
>
{
/**
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
*/
public
async
fetch
(
key
:
string
|
null
,
fetcher
:
()
=>
Promise
<
T
>
,
validator
?:
(
cachedValue
:
T
)
=>
boolean
):
Promise
<
T
>
{
const
cachedValue
=
this
.
get
(
key
);
if
(
cachedValue
!==
undefined
)
{
// Cache HIT
return
cachedValue
;
if
(
validator
)
{
if
(
validator
(
cachedValue
))
{
// Cache HIT
return
cachedValue
;
}
}
else
{
// Cache HIT
return
cachedValue
;
}
}
// Cache MISS
...
...
This diff is collapsed.
Click to expand it.
packages/backend/src/models/repositories/user.ts
+
10
−
2
View file @
eb9e6d23
...
...
@@ -8,6 +8,10 @@ import { awaitAll, Promiseable } from '@/prelude/await-all.js';
import
{
populateEmojis
}
from
'
@/misc/populate-emojis.js
'
;
import
{
getAntennas
}
from
'
@/misc/antenna-cache.js
'
;
import
{
USER_ACTIVE_THRESHOLD
,
USER_ONLINE_THRESHOLD
}
from
'
@/const.js
'
;
import
{
Cache
}
from
'
@/misc/cache.js
'
;
import
{
Instance
}
from
'
../entities/instance.js
'
;
const
userInstanceCache
=
new
Cache
<
Instance
|
null
>
(
1000
*
60
*
60
*
3
);
type
IsUserDetailed
<
Detailed
extends
boolean
>
=
Detailed
extends
true
?
Packed
<
'
UserDetailed
'
>
:
Packed
<
'
UserLite
'
>
;
type
IsMeAndIsUserDetailed
<
ExpectsMe
extends
boolean
|
null
,
Detailed
extends
boolean
>
=
...
...
@@ -254,8 +258,11 @@ export class UserRepository extends Repository<User> {
isModerator
:
user
.
isModerator
||
falsy
,
isBot
:
user
.
isBot
||
falsy
,
isCat
:
user
.
isCat
||
falsy
,
showTimelineReplies
:
user
.
showTimelineReplies
||
falsy
,
instance
:
user
.
host
?
Instances
.
findOne
({
host
:
user
.
host
}).
then
(
instance
=>
instance
?
{
// TODO: typeorm 3.0にしたら .then(x => x || null) は消せる
instance
:
user
.
host
?
userInstanceCache
.
fetch
(
user
.
host
,
()
=>
Instances
.
findOne
({
host
:
user
.
host
}).
then
(
x
=>
x
||
null
),
v
=>
v
!=
null
).
then
(
instance
=>
instance
?
{
name
:
instance
.
name
,
softwareName
:
instance
.
softwareName
,
softwareVersion
:
instance
.
softwareVersion
,
...
...
@@ -334,6 +341,7 @@ export class UserRepository extends Repository<User> {
mutedInstances
:
profile
!
.
mutedInstances
,
mutingNotificationTypes
:
profile
!
.
mutingNotificationTypes
,
emailNotificationTypes
:
profile
!
.
emailNotificationTypes
,
showTimelineReplies
:
user
.
showTimelineReplies
||
falsy
,
}
:
{}),
...(
opts
.
includeSecrets
?
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment