Created
July 2, 2021 02:31
-
-
Save danielkellyio/e28192c1056de3feee7c44057635146a to your computer and use it in GitHub Desktop.
Cache Class Helper Example
This file contains hidden or 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
/** | |
* docs: /docs/cache.md | |
*/ | |
import LZUTF8 from 'lzutf8' | |
window.LZUTF8 = LZUTF8 | |
let cache = { 'default': {} } | |
try{ | |
cache = JSON.parse( LZUTF8.decompress(localStorage.getItem('app_cache'), {inputEncoding: 'StorageBinaryString'}) ) | |
Object.keys(cache).forEach(groupName =>{ | |
const group = cache[groupName] | |
Object.keys(group).forEach(itemKey =>{ | |
if(!group[itemKey].localStorage){ | |
delete cache[groupName][itemKey] | |
} | |
}) | |
}) | |
}catch(error){} | |
export default class Cache{ | |
constructor () { | |
this._group = 'default' | |
this._localStorage = false | |
this._expires = 60 * 60 * 1000 // default expiry is 1 hour | |
} | |
persistAcrossPageReload(enabled = true){ | |
this._localStorage = enabled | |
return this | |
} | |
get(key, defaultValue = null){ | |
this._initGroupIfNoExist() | |
const cacheItem = cache[this._group][key] | |
if(cacheItem){ | |
if(!this._expired(cacheItem)) return cacheItem.value | |
// if it is expired go ahead and clean it out of the cache here | |
delete cache[this._group][key] | |
this._syncLocalStorage() | |
} | |
return typeof defaultValue === 'function' ? defaultValue() : defaultValue | |
} | |
put(key, value){ | |
this._initGroupIfNoExist() | |
cache[this._group][key] = { | |
expires: Date.now() + this._expires, | |
localStorage: this._localStorage, | |
value | |
} | |
this._syncLocalStorage() | |
return this | |
} | |
pull(key, defaultValue= null){ | |
const value = this.get(key, defaultValue) | |
this.forget(key) | |
this._syncLocalStorage() | |
return value | |
} | |
has(key){ | |
return !! this.get(key) | |
} | |
remove(key){ | |
this.forget(key) | |
} | |
forget(key){ | |
if(cache[this._group] && cache[this._group][key]){ | |
delete cache[this._group][key] | |
this._syncLocalStorage() | |
} | |
return this | |
} | |
flushGroup(group){ | |
cache[group] = {} | |
this._syncLocalStorage() | |
return this | |
} | |
flush(){ | |
cache = { 'default' : {} } | |
this._syncLocalStorage() | |
return this | |
} | |
group(name){ | |
this._group = name | |
return this | |
} | |
expires(milliseconds){ | |
if(milliseconds !== null){ | |
this._expires = milliseconds | |
} | |
return this | |
} | |
getGroup(group){ | |
return cache[group] || {} | |
} | |
all(){ | |
return cache | |
} | |
_expired(item){ | |
return Date.now() > item.expires | |
} | |
_initGroupIfNoExist(){ | |
cache[this._group] = cache[this._group] || {} | |
} | |
_syncLocalStorage(){ | |
localStorage.setItem( | |
'app_cache', | |
LZUTF8.compress(JSON.stringify(cache), {outputEncoding: 'StorageBinaryString'}) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment