diff options
| author | Fridon <[email protected]> | 2022-06-20 17:04:00 +0400 |
|---|---|---|
| committer | Fridon <[email protected]> | 2022-06-20 17:04:00 +0400 |
| commit | 82a2f864b8f676b575d075151d7a2db0fe663b51 (patch) | |
| tree | 6198ec3cd7e2b96d08f631ed78c8a90d6d1ea362 /util/cache.ts | |
| parent | 5a801fa98fb052d9f4bbd90f497f076e9c5425c1 (diff) | |
| download | countryfetch-82a2f864b8f676b575d075151d7a2db0fe663b51.tar.xz countryfetch-82a2f864b8f676b575d075151d7a2db0fe663b51.zip | |
store and read data in ~/.cache/countries
Diffstat (limited to 'util/cache.ts')
| -rw-r--r-- | util/cache.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/util/cache.ts b/util/cache.ts new file mode 100644 index 0000000..3a0c5d4 --- /dev/null +++ b/util/cache.ts @@ -0,0 +1,39 @@ +import { join } from "https://deno.land/[email protected]/path/mod.ts"; +import { ensureDirSync } from "https://deno.land/[email protected]/fs/mod.ts"; +import { environment } from "../environment/environment.ts"; + +export class Cache { + path = environment.cacheDir; + + public saveJson(name: string, data: {}) { + ensureDirSync(this.path); + + Deno.writeTextFileSync( + join(this.path, `${name}.json`), + JSON.stringify(data) + ); + } + + public saveTxt(name: string, value: string) { + ensureDirSync(this.path); + Deno.writeTextFileSync(join(this.path, `${name}.txt`), value); + } + + public readJson(name: string): {} | [] | undefined { + let data; + try { + data = Deno.readTextFileSync(join(this.path, `${name}.json`)); + } catch (err) { + return undefined; + } + return JSON.parse(data); + } + + public readTxt(name: string) { + try { + return Deno.readTextFileSync(join(this.path, `${name}.txt`)); + } catch (err) { + return undefined; + } + } +} |
