aboutsummaryrefslogtreecommitdiff
path: root/util/cache.ts
diff options
context:
space:
mode:
authorFridon <[email protected]>2022-06-20 17:04:00 +0400
committerFridon <[email protected]>2022-06-20 17:04:00 +0400
commit82a2f864b8f676b575d075151d7a2db0fe663b51 (patch)
tree6198ec3cd7e2b96d08f631ed78c8a90d6d1ea362 /util/cache.ts
parent5a801fa98fb052d9f4bbd90f497f076e9c5425c1 (diff)
downloadcountryfetch-82a2f864b8f676b575d075151d7a2db0fe663b51.tar.xz
countryfetch-82a2f864b8f676b575d075151d7a2db0fe663b51.zip
store and read data in ~/.cache/countries
Diffstat (limited to 'util/cache.ts')
-rw-r--r--util/cache.ts39
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;
+ }
+ }
+}