Skip to content

Commit

Permalink
✨ Add Cache (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sokolov authored May 17, 2020
1 parent 58a1376 commit c9cef77
Show file tree
Hide file tree
Showing 5 changed files with 5,648 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.eslintcache
.idea/
lib/
node_modules/
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"@vuepress/shared-utils": "^1.4.1",
"cac": "6.5.5",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-plugin-import": "^2.20.2",
Expand All @@ -47,8 +48,7 @@
"vue-router": "^3.1.6",
"vuepress-types": "^0.9.1"
},
"dependencies": {
},
"dependencies": {},
"prettier": {
"trailingComma": "all",
"tabWidth": 2,
Expand Down
40 changes: 40 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { readFileSync, writeFileSync } from 'fs';
import { CacheData, CommitSHA, Contributor } from './types';

export default class Cache {
cache: CacheData;

private readonly cachePath: string;

constructor(cachePath: string) {
this.cache = Cache.loadCache(cachePath);
this.cachePath = cachePath;
}

get(key: CommitSHA): Contributor | undefined {
return this.cache[key];
}

set(key: CommitSHA, value: Contributor): void {
this.cache[key] = { ...value, updatedAt: new Date() };
}

delete(key: CommitSHA): void {
delete this.cache[key];
}

saveCache(): void {
const data = JSON.stringify(this.cache);
writeFileSync(this.cachePath, data, { encoding: 'utf8' });
}

private static loadCache(cachePath: string): CacheData {
let file: string;
try {
file = readFileSync(cachePath, { encoding: 'utf8' });
} catch (e) {
file = '';
}
return file ? JSON.parse(file) : {};
}
}
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Contributor {
name: string;
profileUrl: string;
profilePic?: string;
updatedAt?: Date;
}
export type CommitSHA = string;
export type CacheData = Record<CommitSHA, Contributor>;
Loading

0 comments on commit c9cef77

Please sign in to comment.