mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 07:24:13 +00:00 
			
		
		
		
	encapsulate MemoryKVCache<T>
				
					
				
			This commit is contained in:
		
							parent
							
								
									5496aa27db
								
							
						
					
					
						commit
						114b698034
					
				
					 2 changed files with 11 additions and 17 deletions
				
			
		| 
						 | 
					@ -135,14 +135,14 @@ export class CacheService implements OnApplicationShutdown {
 | 
				
			||||||
					if (user == null) {
 | 
										if (user == null) {
 | 
				
			||||||
						this.userByIdCache.delete(body.id);
 | 
											this.userByIdCache.delete(body.id);
 | 
				
			||||||
						this.localUserByIdCache.delete(body.id);
 | 
											this.localUserByIdCache.delete(body.id);
 | 
				
			||||||
						for (const [k, v] of this.uriPersonCache.cache.entries()) {
 | 
											for (const [k, v] of this.uriPersonCache.entries) {
 | 
				
			||||||
							if (v.value?.id === body.id) {
 | 
												if (v.value?.id === body.id) {
 | 
				
			||||||
								this.uriPersonCache.delete(k);
 | 
													this.uriPersonCache.delete(k);
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					} else {
 | 
										} else {
 | 
				
			||||||
						this.userByIdCache.set(user.id, user);
 | 
											this.userByIdCache.set(user.id, user);
 | 
				
			||||||
						for (const [k, v] of this.uriPersonCache.cache.entries()) {
 | 
											for (const [k, v] of this.uriPersonCache.entries) {
 | 
				
			||||||
							if (v.value?.id === user.id) {
 | 
												if (v.value?.id === user.id) {
 | 
				
			||||||
								this.uriPersonCache.set(k, user);
 | 
													this.uriPersonCache.set(k, user);
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -187,22 +187,12 @@ export class RedisSingleCache<T> {
 | 
				
			||||||
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
 | 
					// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class MemoryKVCache<T> {
 | 
					export class MemoryKVCache<T> {
 | 
				
			||||||
	/**
 | 
						private readonly cache = new Map<string, { date: number; value: T; }>();
 | 
				
			||||||
	 * データを持つマップ
 | 
						private readonly gcIntervalHandle = setInterval(() => this.gc(), 1000 * 60 * 3);
 | 
				
			||||||
	 * @deprecated これを直接操作するべきではない
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public cache: Map<string, { date: number; value: T; }>;
 | 
					 | 
				
			||||||
	private lifetime: number;
 | 
					 | 
				
			||||||
	private gcIntervalHandle: NodeJS.Timeout;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	constructor(lifetime: MemoryKVCache<never>['lifetime']) {
 | 
						constructor(
 | 
				
			||||||
		this.cache = new Map();
 | 
							private readonly lifetime: number,
 | 
				
			||||||
		this.lifetime = lifetime;
 | 
						) {}
 | 
				
			||||||
 | 
					 | 
				
			||||||
		this.gcIntervalHandle = setInterval(() => {
 | 
					 | 
				
			||||||
			this.gc();
 | 
					 | 
				
			||||||
		}, 1000 * 60 * 3);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@bindThis
 | 
						@bindThis
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
| 
						 | 
					@ -298,6 +288,10 @@ export class MemoryKVCache<T> {
 | 
				
			||||||
	public dispose(): void {
 | 
						public dispose(): void {
 | 
				
			||||||
		clearInterval(this.gcIntervalHandle);
 | 
							clearInterval(this.gcIntervalHandle);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public get entries() {
 | 
				
			||||||
 | 
							return this.cache.entries();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class MemorySingleCache<T> {
 | 
					export class MemorySingleCache<T> {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue