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
1377ea41
Commit
1377ea41
authored
1 year ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
perf(backend): improve cache of federated instances
parent
6e1ae7b2
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/core/FederatedInstanceService.ts
+18
-8
18 additions, 8 deletions
packages/backend/src/core/FederatedInstanceService.ts
packages/backend/src/core/entities/UserEntityService.ts
+4
-10
4 additions, 10 deletions
packages/backend/src/core/entities/UserEntityService.ts
with
22 additions
and
18 deletions
packages/backend/src/core/FederatedInstanceService.ts
+
18
−
8
View file @
1377ea41
import
{
Inject
,
Injectable
}
from
'
@nestjs/common
'
;
import
Redis
from
'
ioredis
'
;
import
type
{
InstancesRepository
}
from
'
@/models/index.js
'
;
import
type
{
Instance
}
from
'
@/models/entities/Instance.js
'
;
import
{
MemoryKVCache
}
from
'
@/misc/cache.js
'
;
import
{
MemoryKVCache
,
RedisKVCache
}
from
'
@/misc/cache.js
'
;
import
{
IdService
}
from
'
@/core/IdService.js
'
;
import
{
DI
}
from
'
@/di-symbols.js
'
;
import
{
UtilityService
}
from
'
@/core/UtilityService.js
'
;
...
...
@@ -9,23 +10,32 @@ import { bindThis } from '@/decorators.js';
@
Injectable
()
export
class
FederatedInstanceService
{
p
rivate
cache
:
Memory
KVCache
<
Instance
>
;
p
ublic
federatedInstanceCache
:
Redis
KVCache
<
Instance
|
null
>
;
constructor
(
@
Inject
(
DI
.
redis
)
private
redisClient
:
Redis
.
Redis
,
@
Inject
(
DI
.
instancesRepository
)
private
instancesRepository
:
InstancesRepository
,
private
utilityService
:
UtilityService
,
private
idService
:
IdService
,
)
{
this
.
cache
=
new
MemoryKVCache
<
Instance
>
(
1000
*
60
*
60
);
this
.
federatedInstanceCache
=
new
RedisKVCache
<
Instance
|
null
>
(
this
.
redisClient
,
'
federatedInstance
'
,
{
lifetime
:
1000
*
60
*
60
*
24
,
// 24h
memoryCacheLifetime
:
1000
*
60
*
30
,
// 30m
fetcher
:
(
key
)
=>
this
.
instancesRepository
.
findOneBy
({
host
:
key
}),
toRedisConverter
:
(
value
)
=>
JSON
.
stringify
(
value
),
fromRedisConverter
:
(
value
)
=>
JSON
.
parse
(
value
),
// TODO: date型の考慮
});
}
@
bindThis
public
async
fetch
(
host
:
string
):
Promise
<
Instance
>
{
host
=
this
.
utilityService
.
toPuny
(
host
);
const
cached
=
this
.
c
ache
.
get
(
host
);
const
cached
=
await
this
.
federatedInstanceC
ache
.
get
(
host
);
if
(
cached
)
return
cached
;
const
index
=
await
this
.
instancesRepository
.
findOneBy
({
host
});
...
...
@@ -37,10 +47,10 @@ export class FederatedInstanceService {
firstRetrievedAt
:
new
Date
(),
}).
then
(
x
=>
this
.
instancesRepository
.
findOneByOrFail
(
x
.
identifiers
[
0
]));
this
.
c
ache
.
set
(
host
,
i
);
this
.
federatedInstanceC
ache
.
set
(
host
,
i
);
return
i
;
}
else
{
this
.
c
ache
.
set
(
host
,
index
);
this
.
federatedInstanceC
ache
.
set
(
host
,
index
);
return
index
;
}
}
...
...
@@ -49,10 +59,10 @@ export class FederatedInstanceService {
public
async
updateCachePartial
(
host
:
string
,
data
:
Partial
<
Instance
>
):
Promise
<
void
>
{
host
=
this
.
utilityService
.
toPuny
(
host
);
const
cached
=
this
.
c
ache
.
get
(
host
);
const
cached
=
await
this
.
federatedInstanceC
ache
.
get
(
host
);
if
(
cached
==
null
)
return
;
this
.
c
ache
.
set
(
host
,
{
this
.
federatedInstanceC
ache
.
set
(
host
,
{
...
cached
,
...
data
,
});
...
...
This diff is collapsed.
Click to expand it.
packages/backend/src/core/entities/UserEntityService.ts
+
4
−
10
View file @
1377ea41
...
...
@@ -9,13 +9,13 @@ import type { Packed } from '@/misc/json-schema.js';
import
type
{
Promiseable
}
from
'
@/misc/prelude/await-all.js
'
;
import
{
awaitAll
}
from
'
@/misc/prelude/await-all.js
'
;
import
{
USER_ACTIVE_THRESHOLD
,
USER_ONLINE_THRESHOLD
}
from
'
@/const.js
'
;
import
{
MemoryKVCache
,
RedisKVCache
}
from
'
@/misc/cache.js
'
;
import
type
{
Instance
}
from
'
@/models/entities/Instance.js
'
;
import
type
{
LocalUser
,
RemoteUser
,
User
}
from
'
@/models/entities/User.js
'
;
import
{
birthdaySchema
,
descriptionSchema
,
localUsernameSchema
,
locationSchema
,
nameSchema
,
passwordSchema
}
from
'
@/models/entities/User.js
'
;
import
type
{
UsersRepository
,
UserSecurityKeysRepository
,
FollowingsRepository
,
FollowRequestsRepository
,
BlockingsRepository
,
MutingsRepository
,
DriveFilesRepository
,
NoteUnreadsRepository
,
ChannelFollowingsRepository
,
UserNotePiningsRepository
,
UserProfilesRepository
,
InstancesRepository
,
AnnouncementReadsRepository
,
AnnouncementsRepository
,
PagesRepository
,
UserProfile
,
RenoteMutingsRepository
}
from
'
@/models/index.js
'
;
import
{
bindThis
}
from
'
@/decorators.js
'
;
import
{
RoleService
}
from
'
@/core/RoleService.js
'
;
import
{
FederatedInstanceService
}
from
'
@/core/FederatedInstanceService.js
'
;
import
type
{
OnModuleInit
}
from
'
@nestjs/common
'
;
import
type
{
AntennaService
}
from
'
../AntennaService.js
'
;
import
type
{
CustomEmojiService
}
from
'
../CustomEmojiService.js
'
;
...
...
@@ -53,7 +53,7 @@ export class UserEntityService implements OnModuleInit {
private
customEmojiService
:
CustomEmojiService
;
private
antennaService
:
AntennaService
;
private
roleService
:
RoleService
;
private
user
Instance
Cach
e
:
R
ed
isKVCache
<
Instance
|
null
>
;
private
federated
Instance
Servic
e
:
F
ed
eratedInstanceService
;
constructor
(
private
moduleRef
:
ModuleRef
,
...
...
@@ -119,13 +119,6 @@ export class UserEntityService implements OnModuleInit {
//private antennaService: AntennaService,
//private roleService: RoleService,
)
{
this
.
userInstanceCache
=
new
RedisKVCache
<
Instance
|
null
>
(
this
.
redisClient
,
'
userInstance
'
,
{
lifetime
:
1000
*
60
*
60
*
24
,
// 24h
memoryCacheLifetime
:
1000
*
60
*
30
,
// 30m
fetcher
:
(
key
)
=>
this
.
instancesRepository
.
findOneBy
({
host
:
key
}),
toRedisConverter
:
(
value
)
=>
JSON
.
stringify
(
value
),
fromRedisConverter
:
(
value
)
=>
JSON
.
parse
(
value
),
// TODO: date型の考慮
});
}
onModuleInit
()
{
...
...
@@ -135,6 +128,7 @@ export class UserEntityService implements OnModuleInit {
this
.
customEmojiService
=
this
.
moduleRef
.
get
(
'
CustomEmojiService
'
);
this
.
antennaService
=
this
.
moduleRef
.
get
(
'
AntennaService
'
);
this
.
roleService
=
this
.
moduleRef
.
get
(
'
RoleService
'
);
this
.
federatedInstanceService
=
this
.
moduleRef
.
get
(
'
FederatedInstanceService
'
);
}
//#region Validators
...
...
@@ -349,7 +343,7 @@ export class UserEntityService implements OnModuleInit {
avatarBlurhash
:
user
.
avatarBlurhash
,
isBot
:
user
.
isBot
??
falsy
,
isCat
:
user
.
isCat
??
falsy
,
instance
:
user
.
host
?
this
.
user
InstanceCache
.
fetch
(
user
.
host
).
then
(
instance
=>
instance
?
{
instance
:
user
.
host
?
this
.
federatedInstanceService
.
federated
InstanceCache
.
fetch
(
user
.
host
).
then
(
instance
=>
instance
?
{
name
:
instance
.
name
,
softwareName
:
instance
.
softwareName
,
softwareVersion
:
instance
.
softwareVersion
,
...
...
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