more
This commit is contained in:
		
							parent
							
								
									22e041f440
								
							
						
					
					
						commit
						f812e06d17
					
				
					 4 changed files with 55 additions and 50 deletions
				
			
		
							
								
								
									
										50
									
								
								src/cache/adapter.ts
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										50
									
								
								src/cache/adapter.ts
									
										
									
									
										vendored
									
									
								
							|  | @ -1,5 +1,3 @@ | |||
| import { Collection } from '../utils/collection.ts' | ||||
| 
 | ||||
| /** | ||||
|  * ICacheAdapter is the interface to be implemented by Cache Adapters for them to be usable with Harmony. | ||||
|  * | ||||
|  | @ -22,51 +20,3 @@ export interface ICacheAdapter { | |||
|   /** Entirely deletes a Cache */ | ||||
|   deleteCache: (cacheName: string) => any | ||||
| } | ||||
| 
 | ||||
| /** Default Cache Adapter for in-memory caching. */ | ||||
| export class DefaultCacheAdapter implements ICacheAdapter { | ||||
|   data: { | ||||
|     [name: string]: Collection<string, any> | ||||
|   } = {} | ||||
| 
 | ||||
|   async get(cacheName: string, key: string): Promise<undefined | any> { | ||||
|     const cache = this.data[cacheName] | ||||
|     if (cache === undefined) return | ||||
|     return cache.get(key) | ||||
|   } | ||||
| 
 | ||||
|   async set( | ||||
|     cacheName: string, | ||||
|     key: string, | ||||
|     value: any, | ||||
|     expire?: number | ||||
|   ): Promise<any> { | ||||
|     let cache = this.data[cacheName] | ||||
|     if (cache === undefined) { | ||||
|       this.data[cacheName] = new Collection() | ||||
|       cache = this.data[cacheName] | ||||
|     } | ||||
|     cache.set(key, value) | ||||
|     if (expire !== undefined) | ||||
|       setTimeout(() => { | ||||
|         cache.delete(key) | ||||
|       }, expire) | ||||
|   } | ||||
| 
 | ||||
|   async delete(cacheName: string, key: string): Promise<boolean> { | ||||
|     const cache = this.data[cacheName] | ||||
|     if (cache === undefined) return false | ||||
|     return cache.delete(key) | ||||
|   } | ||||
| 
 | ||||
|   async array(cacheName: string): Promise<any[] | undefined> { | ||||
|     const cache = this.data[cacheName] | ||||
|     if (cache === undefined) return | ||||
|     return cache.array() | ||||
|   } | ||||
| 
 | ||||
|   async deleteCache(cacheName: string): Promise<boolean> { | ||||
|     // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
 | ||||
|     return delete this.data[cacheName] | ||||
|   } | ||||
| } | ||||
|  |  | |||
							
								
								
									
										50
									
								
								src/cache/default.ts
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/cache/default.ts
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,50 @@ | |||
| import { Collection } from '../utils/collection.ts' | ||||
| import type { ICacheAdapter } from './adapter.ts' | ||||
| 
 | ||||
| /** Default Cache Adapter for in-memory caching. */ | ||||
| export class DefaultCacheAdapter implements ICacheAdapter { | ||||
|   data: { | ||||
|     [name: string]: Collection<string, any> | ||||
|   } = {} | ||||
| 
 | ||||
|   async get(cacheName: string, key: string): Promise<undefined | any> { | ||||
|     const cache = this.data[cacheName] | ||||
|     if (cache === undefined) return | ||||
|     return cache.get(key) | ||||
|   } | ||||
| 
 | ||||
|   async set( | ||||
|     cacheName: string, | ||||
|     key: string, | ||||
|     value: any, | ||||
|     expire?: number | ||||
|   ): Promise<any> { | ||||
|     let cache = this.data[cacheName] | ||||
|     if (cache === undefined) { | ||||
|       this.data[cacheName] = new Collection() | ||||
|       cache = this.data[cacheName] | ||||
|     } | ||||
|     cache.set(key, value) | ||||
|     if (expire !== undefined) | ||||
|       setTimeout(() => { | ||||
|         cache.delete(key) | ||||
|       }, expire) | ||||
|   } | ||||
| 
 | ||||
|   async delete(cacheName: string, key: string): Promise<boolean> { | ||||
|     const cache = this.data[cacheName] | ||||
|     if (cache === undefined) return false | ||||
|     return cache.delete(key) | ||||
|   } | ||||
| 
 | ||||
|   async array(cacheName: string): Promise<any[] | undefined> { | ||||
|     const cache = this.data[cacheName] | ||||
|     if (cache === undefined) return | ||||
|     return cache.array() | ||||
|   } | ||||
| 
 | ||||
|   async deleteCache(cacheName: string): Promise<boolean> { | ||||
|     // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
 | ||||
|     return delete this.data[cacheName] | ||||
|   } | ||||
| } | ||||
							
								
								
									
										4
									
								
								src/cache/mod.ts
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/cache/mod.ts
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,4 @@ | |||
| export * from './adapter.ts' | ||||
| export * from './default.ts' | ||||
| // Not exported by default
 | ||||
| // export * from './redis.ts'
 | ||||
							
								
								
									
										1
									
								
								src/cache/redis.ts
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								src/cache/redis.ts
									
										
									
									
										vendored
									
									
								
							|  | @ -1,4 +1,5 @@ | |||
| import { ICacheAdapter } from './adapter.ts' | ||||
| // Not in deps.ts to allow optional dep loading
 | ||||
| import { | ||||
|   connect, | ||||
|   Redis, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue