Pārlūkot izejas kodu

Merge pull request #146 from ente-io/add-cache-entry-delete-api

added cache entry delete support
Abhinav Kumar 2 gadi atpakaļ
vecāks
revīzija
909a0f27de
2 mainītis faili ar 17 papildinājumiem un 2 dzēšanām
  1. 12 2
      src/services/diskCache.ts
  2. 5 0
      src/types/cache.ts

+ 12 - 2
src/services/diskCache.ts

@@ -1,11 +1,12 @@
 import DiskLRUService from '../services/diskLRU';
 import crypto from 'crypto';
-import { existsSync, readFile, writeFile } from 'promise-fs';
+import { existsSync, readFile, writeFile, unlink } from 'promise-fs';
 import path from 'path';
+import { LimitedCache } from '../types/cache';
 
 const MAX_CACHE_SIZE = 1000 * 1000 * 1000; // 1GB
 
-export class DiskCache {
+export class DiskCache implements LimitedCache {
     constructor(private cacheBucketDir: string) {}
 
     async put(cacheKey: string, response: Response): Promise<void> {
@@ -29,6 +30,15 @@ export class DiskCache {
             return undefined;
         }
     }
+    async delete(cacheKey: string): Promise<boolean> {
+        const cachePath = getAssetCachePath(this.cacheBucketDir, cacheKey);
+        if (existsSync(cachePath)) {
+            await unlink(cachePath);
+            return true;
+        } else {
+            return false;
+        }
+    }
 }
 function getAssetCachePath(cacheDir: string, cacheKey: string) {
     // hashing the key to prevent illegal filenames

+ 5 - 0
src/types/cache.ts

@@ -0,0 +1,5 @@
+export interface LimitedCache {
+    match: (key: string) => Promise<Response>;
+    put: (key: string, data: Response) => Promise<void>;
+    delete: (key: string) => Promise<boolean>;
+}