Skip to content

Commit

Permalink
♻️ Use node-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 16, 2019
1 parent 67539ed commit 80b0b65
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 46 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@staart/site",
"version": "0.9.31",
"version": "0.9.32",
"main": "dist/index.js",
"repository": "[email protected]:staart/site",
"author": "Anand Chowdhary <[email protected]>",
Expand All @@ -26,7 +26,6 @@
"axios": "^0.19.0",
"concurrently": "^5.0.0",
"cosmiconfig": "^5.2.1",
"fraud": "^5.2.0",
"front-matter": "^3.0.2",
"fs-extra": "^8.1.0",
"git-url-parse": "^11.1.2",
Expand All @@ -49,6 +48,7 @@
"@types/sass": "^1.16.0",
"@types/signale": "^1.2.1",
"husky": "^3.0.9",
"node-cache": "^4.2.1",
"onchange": "^6.1.0",
"prettier": "^1.18.2",
"typescript": "^3.6.4"
Expand Down
22 changes: 8 additions & 14 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import Fraud from "fraud";
import { ensureDir } from "fs-extra";
const cache = new Fraud({
directory: ".cache/staart-site"
});
import NodeCache from "node-cache";
const cache = new NodeCache();

export const cached = async (name: string, f: Function) => {
export const cached = async <T>(name: string, f: Function) => {
name = name.replace(/[^a-zA-Z ]/g, "");
await ensureDir(".cache/staart-site");
try {
return await cache.read(`func-${name}`);
} catch (error) {
const result = await f();
cache.create(`func-${name}`, result);
return result;
}
let result = cache.get<T>(`func-${name}`);
if (result) return result;
result = await f();
cache.set(`func-${name}`, result);
return result;
};
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import gitUrlParse from "git-url-parse";
import { cached } from "./cache";
const explorer = cosmiconfig("staart");

export const getConfig = async (): Promise<StaartSiteConfig> => {
return cached("config", _getConfig);
export const getConfig = async () => {
const config = await cached<StaartSiteConfig>("config", _getConfig);
if (config) return config;
throw new Error("Config not found");
};

export const readPackage = async (): Promise<
Expand Down
18 changes: 11 additions & 7 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { render as scss } from "sass";
import { removeHeading } from "./parse";
import { getConfig } from "./config";

export const getTemplate = async (): Promise<string> => {
return cached("template", async () => {
export const getTemplate = async () => {
const result = await cached<string>("template", async () => {
try {
return (await readFile(await getTemplatePath())).toString();
} catch (error) {
Expand All @@ -24,10 +24,12 @@ export const getTemplate = async (): Promise<string> => {
)).toString();
}
});
if (result) return result;
throw new Error("Template not found");
};

export const getHomeContent = async (): Promise<string> => {
return cached("home", async () => {
export const getHomeContent = async () => {
const result = await cached<string>("home", async () => {
try {
return (await readFile(await getHomePath())).toString();
} catch (error) {
Expand All @@ -36,17 +38,19 @@ export const getHomeContent = async (): Promise<string> => {
)).toString();
}
});
if (result) return result;
throw new Error("Homepage not found");
};

const renderScss = (styles: string): Promise<string> =>
const renderScss = (styles: string) =>
new Promise((resolve, reject) => {
scss({ data: styles }, (error, result) => {
if (error) return reject(error);
resolve(result.css.toString());
});
});
export const getCss = async (): Promise<string> => {
return cached("css", async () => {
export const getCss = async () => {
return await cached<string>("css", async () => {
try {
return await renderScss(
(await readFile(await getStylePath())).toString()
Expand Down
22 changes: 1 addition & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,6 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.0.0.tgz#23c0df14f6a88077f5f986c0d167ec03c3d5537c"
integrity sha512-Phlt0plgpIIBOGTT/ehfFnbNlfsDEiqmzE2KRXoX1bLIlir4X/MR+zSyBEkL05ffWgnRSf/DXv+WrUAVr93/ow==

bluebird@^3.4.1:
version "3.7.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.0.tgz#56a6a886e03f6ae577cffedeb524f8f2450293cf"
integrity sha512-aBQ1FxIa7kSWCcmKHlcHFlT2jt6J/l4FzC7KcPELkOJOsPOb/bccdhmIrKDfXhwFrmc7vDoDrrepFvGqjyXGJg==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -853,14 +848,6 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

fraud@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/fraud/-/fraud-5.2.0.tgz#9561a3ff2512ccf141500545bf63d52122e92397"
integrity sha512-lgkKKbJ5udteYOLMJRq8Ckc9F24nH9Zr1f7n1cB+UZSRZdsvBARMMwJSbV8QXohVPa5hFDmZxc8BnG8hAxNxNQ==
dependencies:
node-cache "^4.2.0"
promise-serial "^0.1.5"

[email protected]:
version "0.5.2"
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
Expand Down Expand Up @@ -1707,7 +1694,7 @@ no-case@^2.2.0:
dependencies:
lower-case "^1.1.1"

node-cache@^4.2.0:
node-cache@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-4.2.1.tgz#efd8474dee4edec4138cdded580f5516500f7334"
integrity sha512-BOb67bWg2dTyax5kdef5WfU3X8xu4wPg+zHzkvls0Q/QpYycIFRLEEIdAx9Wma43DxG6Qzn4illdZoYseKWa4A==
Expand Down Expand Up @@ -2106,13 +2093,6 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

promise-serial@^0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/promise-serial/-/promise-serial-0.1.5.tgz#68fb4d0e283c912459e02377a26a8d067bf567d1"
integrity sha512-WyxsnHCk/dr3ZDckvtadj1X4/YuOUrGhIYKmnVnOyC3DOLq4xY/iyEMMjMtSe1aE8JYw1z/nOTetrMqRrd4luQ==
dependencies:
bluebird "^3.4.1"

protocols@^1.1.0, protocols@^1.4.0:
version "1.4.7"
resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.7.tgz#95f788a4f0e979b291ffefcf5636ad113d037d32"
Expand Down

1 comment on commit 80b0b65

@vercel
Copy link

@vercel vercel bot commented on 80b0b65 Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.