Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurYdalgo committed Jan 3, 2023
1 parent 3818277 commit 38cfbdb
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
86 changes: 86 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
class CacheStore {
constructor(cacheKeyPrefix = null) {
this.cacheKeyPrefix = cacheKeyPrefix;
}
isNumber(value) {
return !isNaN(parseFloat(value)) && !isNaN(value - 0)
}
isDate(value) {
return value instanceof Date && !isNaN(value.valueOf())
}
put(key, value, ttl = null) {
let createdAt = new Date();
let expiresAt = null;

if (this.isNumber(ttl)) {
let date = new Date();
date.setSeconds(date.getSeconds() + ttl);
expiresAt = date.toISOString();
}

if (this.isDate(ttl)) {
expiresAt = ttl.toISOString();
}

let cachedData = {
value, createdAt, expiresAt
};

if (this.cacheKeyPrefix) {
key = this.cacheKeyPrefix + key;
}

localStorage.setItem(key, JSON.stringify(cachedData));

return value;
}
delete(key) {
if (this.cacheKeyPrefix) {
key = this.cacheKeyPrefix + key;
}

return localStorage.removeItem(key);
}
remember(key, ttl, callback) {
if (this.cacheKeyPrefix) {
key = this.cacheKeyPrefix + key;
}

let localStorageItem = localStorage.getItem(key);

let cachedData = localStorageItem ? JSON.parse(localStorageItem) : undefined;

// If cached data exists and doesn't expire, or if cached data expires, but still hasn't
if (cachedData && (!cachedData.expiresAt || (cachedData.expiresAt && cachedData.expiresAt > new Date().toISOString()))) {
return cachedData.value;
}

this.put(key, undefined, ttl);

let value = callback();

return this.put(key, value, ttl);
}
rememberForever(key, callback) {
return this.remember(key, null, callback);
}
get(key, defaultValue = undefined) {
if (this.cacheKeyPrefix) {
key = this.cacheKeyPrefix + key;
}

let localStorageItem = localStorage.getItem(key);

if (!localStorageItem) {
return defaultValue;
}

let cachedData = JSON.parse(localStorageItem);

if (cachedData.expiresAt && cachedData.expiresAt < new Date().toISOString()) {
return defaultValue;
}

return cachedData.value;
}
}
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "time-based-cache",
"version": "1.0.1",
"description": "A Laravel-like Cache Store for Javascript",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ArthurYdalgo/time-based-cache-js.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"files": [
"index.js"
],
"main": "index.js",
"bugs": {
"url": "https://github.com/ArthurYdalgo/time-based-cache-js/issues"
},
"homepage": "https://github.com/ArthurYdalgo/time-based-cache-js#readme"
}

0 comments on commit 38cfbdb

Please sign in to comment.