perf(server): Cache user instance actor
This commit is contained in:
		
							parent
							
								
									cf757ed01e
								
							
						
					
					
						commit
						6b753b05d6
					
				
					 1 changed files with 15 additions and 5 deletions
				
			
		| 
						 | 
					@ -1,17 +1,27 @@
 | 
				
			||||||
import { createSystemUser } from './create-system-user';
 | 
					import { createSystemUser } from './create-system-user';
 | 
				
			||||||
import { ILocalUser } from '../models/entities/user';
 | 
					import { ILocalUser } from '../models/entities/user';
 | 
				
			||||||
import { Users } from '../models';
 | 
					import { Users } from '../models';
 | 
				
			||||||
 | 
					import { Cache } from '../misc/cache';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const ACTOR_USERNAME = 'instance.actor' as const;
 | 
					const ACTOR_USERNAME = 'instance.actor' as const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const cache = new Cache<ILocalUser>(Infinity);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function getInstanceActor(): Promise<ILocalUser> {
 | 
					export async function getInstanceActor(): Promise<ILocalUser> {
 | 
				
			||||||
 | 
						const cached = cache.get(null);
 | 
				
			||||||
 | 
						if (cached) return cached;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const user = await Users.findOne({
 | 
						const user = await Users.findOne({
 | 
				
			||||||
		host: null,
 | 
							host: null,
 | 
				
			||||||
		username: ACTOR_USERNAME
 | 
							username: ACTOR_USERNAME
 | 
				
			||||||
	});
 | 
						}) as ILocalUser | undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (user) return user as ILocalUser;
 | 
						if (user) {
 | 
				
			||||||
 | 
							cache.set(null, user);
 | 
				
			||||||
	const created = await createSystemUser(ACTOR_USERNAME);
 | 
							return user;
 | 
				
			||||||
	return created as ILocalUser;
 | 
						} else {
 | 
				
			||||||
 | 
							const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
 | 
				
			||||||
 | 
							cache.set(null, created);
 | 
				
			||||||
 | 
							return created;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue