From 38cfbdbdc2754f09dd4a6894635490df6e22d812 Mon Sep 17 00:00:00 2001 From: Arthur Ydalgo Date: Tue, 3 Jan 2023 15:04:06 -0400 Subject: [PATCH] init --- index.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 13 +++++++ package.json | 23 +++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 index.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..8b0a837 --- /dev/null +++ b/index.js @@ -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; + } +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1373fd5 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "time-based-cache", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "time-based-cache", + "version": "1.0.0", + "license": "ISC" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3abe5cb --- /dev/null +++ b/package.json @@ -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" +}