Cache meta
This commit is contained in:
		| @@ -1,7 +1,11 @@ | ||||
| import { Meta } from '../models/entities/meta'; | ||||
| import { getConnection } from 'typeorm'; | ||||
|  | ||||
| export default async function(): Promise<Meta> { | ||||
| let cache: Meta; | ||||
|  | ||||
| export async function fetchMeta(noCache = false): Promise<Meta> { | ||||
| 	if (!noCache && cache) return cache; | ||||
|  | ||||
| 	return await getConnection().transaction(async transactionalEntityManager => { | ||||
| 		// バグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する | ||||
| 		const meta = await transactionalEntityManager.findOne(Meta, { | ||||
| @@ -11,11 +15,21 @@ export default async function(): Promise<Meta> { | ||||
| 		}); | ||||
|  | ||||
| 		if (meta) { | ||||
| 			cache = meta; | ||||
| 			return meta; | ||||
| 		} else { | ||||
| 			return await transactionalEntityManager.save(Meta, { | ||||
| 			const saved = await transactionalEntityManager.save(Meta, { | ||||
| 				id: 'x' | ||||
| 			}) as Meta; | ||||
|  | ||||
| 			cache = saved; | ||||
| 			return saved; | ||||
| 		} | ||||
| 	}); | ||||
| } | ||||
|  | ||||
| setInterval(() => { | ||||
| 	fetchMeta(true).then(meta => { | ||||
| 		cache = meta; | ||||
| 	}); | ||||
| }, 5000); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 syuilo
					syuilo