-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3818277
commit 38cfbdb
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |