diff options
| author | Fridon <[email protected]> | 2022-06-20 17:47:11 +0400 |
|---|---|---|
| committer | Fridon <[email protected]> | 2022-06-20 17:47:11 +0400 |
| commit | a64258e7748134d4e0a114e0d893c139ad9c331b (patch) | |
| tree | b81edb45f8aca4d79530a52891ae9f5a998b0098 /src/util/cache.ts | |
| parent | 82a2f864b8f676b575d075151d7a2db0fe663b51 (diff) | |
| download | countryfetch-a64258e7748134d4e0a114e0d893c139ad9c331b.tar.xz countryfetch-a64258e7748134d4e0a114e0d893c139ad9c331b.zip | |
refactor
Diffstat (limited to 'src/util/cache.ts')
| -rw-r--r-- | src/util/cache.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/util/cache.ts b/src/util/cache.ts new file mode 100644 index 0000000..3a0c5d4 --- /dev/null +++ b/src/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; + } + } +} |
