|
| 1 | +import { ImageSource, ImageListFilter, ImageListItemData, ImageInfo } from './image-source.model'; |
| 2 | +import { TimCacheService } from '../tim-cache/tim-cache.service'; |
| 3 | +import { Observable } from 'rxjs'; |
| 4 | +import { ImageLayerHistoryV1Compatibility } from '../registry/registry.model'; |
| 5 | + |
| 6 | +export class CachingImageSource extends ImageSource { |
| 7 | + public get name() { |
| 8 | + return this.wrapped.name; |
| 9 | + } |
| 10 | + |
| 11 | + public get registryDNS() { |
| 12 | + return this.wrapped.registryDNS; |
| 13 | + } |
| 14 | + |
| 15 | + constructor( |
| 16 | + protected readonly wrapped: ImageSource, |
| 17 | + protected readonly cache: TimCacheService, |
| 18 | + protected readonly cacheDurationInMinutes: number) { |
| 19 | + super(); |
| 20 | + this.priority = wrapped.priority; |
| 21 | + this.hasAuthentication = wrapped.hasAuthentication; |
| 22 | + this.credentials = wrapped.credentials; |
| 23 | + this.supportsDeletions = wrapped.supportsDeletions; |
| 24 | + } |
| 25 | + |
| 26 | + public loadList(filter?: ImageListFilter): Observable<ImageListItemData[]> { |
| 27 | + return this.getFromCache(`imagesource/${this.registryDNS}/images/${JSON.stringify(filter)}`, |
| 28 | + () => this.wrapped.loadList(filter)); |
| 29 | + } |
| 30 | + |
| 31 | + public loadImageInfo(image: string): Observable<ImageInfo> { |
| 32 | + return this.getFromCache(`imagesource/${this.registryDNS}/${image}/info`, |
| 33 | + () => this.wrapped.loadImageInfo(image)); |
| 34 | + } |
| 35 | + |
| 36 | + public loadImageHistory(image: string): Observable<ImageLayerHistoryV1Compatibility[]> { |
| 37 | + return this.getFromCache(`imagesource/${this.registryDNS}/${image}/history`, |
| 38 | + () => this.wrapped.loadImageHistory(image)); |
| 39 | + } |
| 40 | + |
| 41 | + public loadImageTags(image: string): Observable<string[]> { |
| 42 | + return this.getFromCache(`imagesource/${this.registryDNS}/${image}/tags`, |
| 43 | + () => this.wrapped.loadImageTags(image)); |
| 44 | + } |
| 45 | + |
| 46 | + public isImageOwner(image: string) { |
| 47 | + return this.wrapped.isImageOwner(image); |
| 48 | + } |
| 49 | + |
| 50 | + public isRegistryOwner(reg: string) { |
| 51 | + return this.wrapped.isRegistryOwner(reg); |
| 52 | + } |
| 53 | + |
| 54 | + public deleteImage(image: string) { |
| 55 | + if (this.wrapped.deleteImage) { |
| 56 | + return this.wrapped.deleteImage(image); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public getBasicAuth() { |
| 61 | + if (this.wrapped.getBasicAuth) { |
| 62 | + return this.wrapped.getBasicAuth(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + protected getFromCache<T>(key: string, fallback: () => Observable<T>): Observable<T> { |
| 67 | + if (typeof this.cacheDurationInMinutes === 'number' && this.cacheDurationInMinutes > 0) { |
| 68 | + return this.cache.get(key, fallback, this.cacheDurationInMinutes); |
| 69 | + } else { |
| 70 | + return fallback(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments