Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@stetsonpierce/simple-cache
Advanced tools
A fast, simple and lightweight expiring key value store for modern browsers.
A fast, simple and lightweight expiring key value store for modern browsers.
Question or comments please feel free to open an issue or email me at me@stetson.io. You can also visit the simple-cache website.
NOTE: SimpleCache follows semver versioning and is under active development.
Built on top of the Window.localStorage and Window.sessionStorage APIs, SimpleCache.js
was a solution developed to provide an expiring semi-persistent data store for modern browsers. I've written an article explaining the how/why of SimpleCache if you're interested.
Feel free to head over to the GitHub page and submit comments, issues, pulls and whatever else you'd like. I plan on adding features as I need them for my own projects so if something isn't happening fast enough for you why not do it yourself? :wink:
npm i @stetsonpierce/simple-cache
<!-- Minified -->
<script src="https://cdn.jsdelivr.net/npm/@stetsonpierce/simple-cache@3/dist/simple-cache.min.js"></script>
<--! index.hmtl -->
<script src="https://cdn.jsdelivr.net/npm/@stetsonpierce/simple-cache@3/dist/simple-cache.min.js"></script>
<script>
const cache = new SimpleCache.default();
cache.set('someKey', 'someValue');
console.log(cache.get('someKey')); // Logs 'someValue'
cache.remove('someKey');
console.log(cache.get('someKey')); // Logs null
</script>
// Can be used as an ES6 Module
import SimpleCache from '@stetsonpierce/simple-cache';
// Or a CommonJS Module
const SimpleCache = require('@stetsonpierce/simple-cache');
const cache = new SimpleCache();
cache.set('someKey', 'someValue');
console.log(cache.get('someKey')); // Logs 'someValue'
cache.remove('someKey');
console.log(cache.get('someKey')); // Logs null
{ Object } [options]
{ number } ttl
{ string } namespace
{ boolean } debug
SimpleCache Instance
ttl
number
1000 * 60 * 60 * 24 | (24 Hours)
namespace
string
SC_
debug
boolean
false
const cache = new SimpleCache({
ttl: 1000 * 30, // 30 seconds
namespace: 'MYAPP_',
debug: true,
});
{ string } key
{ string | Object } value
{ boolean } [session]
cache.set
is used to store values in either local or session storage. The default storage location is local storage. To indicate session storage as the desired data store just pass true
for the session parameter. Values provided can be anything serializable via JSON.stringify, this includes strings
, booleans
, Objects
, etc.
NOTE: All operations provided by SimpleCache can be called in bulk, see bulk operations for more info.
cache.set('myString', 'myValue');
cache.set('myObject', { isCool: true });
cache.set('myBool', true);
{ string } key
{ string | boolean | Object } value
cache.get
provides a uniform way to retrieve any value that has been stored by SimpleCache. The values are stringified so they will be accessible as JavaScript objects after you retrieve them. cache.get
also provides waterfall logic for retrieval. Meaning session storage is checked first followed by local storage if nothing is found. Since session storage is more fragile it is respected as the highest priority for cached items.
NOTE: All operations provided by SimpleCache can be called in bulk, see bulk operations for more info.
const myString = cache.get('myString');
console.log(myString); // Prints 'myValue'
const myObject = cache.get('myObject');
console.log(myObject.isCool); // Prints true
const myBool = cache.get('myBool');
if (myBool) {
console.log(myBool); // Prints true
}
{ string } key
cache.remove
will remove a cached value from both session storage and local storage.
NOTE: All operations provided by SimpleCache can be called in bulk, see bulk operations for more info.
cache.remove('myString');
console.log(cache.get('myString')); // Prints null
cache.set
, cache.get
and cache.remove
have all been written to support bulk operations in addition to single input. This is accomplished by passing an array of values rather than a single input. It's worth noting that all bulk operations will return a promise that you can chain or await.
{ Array<Object> } values
{ string } key
{ string | boolean | Object } value
{ boolean } [session]
cache.set
requires an array of objects that contain the two properties key and value. The properties are fairly self-explanatory. Key is the string value to be used as the key and value will be the serializable value to be stored with the associated key.
await cache.set([
{ key: 'myString1', value: 'value1' },
{ key: 'myString2', value: 'value2' },
{ key: 'myString3', value: 'value3' },
]);
console.log(cache.get('myString2')); // Prints 'value2'
{ Array<string> } keys
{ Array<any> } values
cache.get
requires an array of strings that correlate to keys being requested. The bulk operations follow the same waterfall logic that the singular operation does.
await cache.set([
{ key: 'myString1', value: 'value1' },
{ key: 'myString2', value: 'value2' },
{ key: 'myString3', value: 'value3' },
]);
const values = await cache.get(['myString1', 'myString5', 'myString3']);
console.log(values)
// Prints ['value1', null, 'value3']
{ Array<string> } keys
cache.remove
requires an array of strings that correlate to keys being requested. It will then remove that cached value from both session storage and local storage.
await cache.set([
{ key: 'myString1', value: 'value1' },
{ key: 'myString2', value: 'value2' },
{ key: 'myString3', value: 'value3' },
]);
await cache.remove(['myString1', 'myString2', 'myString3']);
const values = await cache.get(['myString1', 'myString2', 'myString3']);
console.log(values)
// Prints [null, null, null]
Short for Time To Live, ttl specifies how long to respect a cached value before determining it has expired. While SimpleCache gives you the global ttl as a default for all items being cached, it also gives you the ability to specify a ttl on a per item basis. However, this only works if you pass an Object
into set that has a ttl
property. Currently you have to pass single item array if you want to set custom ttl for only one item.
Example:
const myObject = {
value: 'Some string goes here!',
ttl: 5000, // 5 Seconds
};
cache.set('myKey', myObject);
// After 2 Seconds the value will still be available
setTimeout(() => {
console.log(cache.get('myKey')) // Prints { value: 'Some string goes here!' }
}, 2000);
//
setTimeout(() => {
console.log(cache.get('myKey')) // Prints null
}, 6000);
When retrieving values SimpleCache uses waterfall logic when attempting to find stored values. Meaning session storage is checked first, followed by local storage if nothing is found. Since session storage is more fragile it is respected as the highest priority for cached items.
SimpleCache has very verbose logging if you need to figure out why something isn't working the way you'd expect. Just pass debug: true
to enable this. SimpleCache uses either the default namespace, or the one you provided to prefix all log messages.
Example:
import SimpleCache from '@stetsonpierce/simple-cache';
const cache = new SimpleCache({
debug: true,
});
cache.set('myKey', 'myValue');
# Example Output
15:18:03.550 SimpleCache:Storing in Local
15:18:03.550 SimpleCache:Setting ttl: 1534577434218
15:18:03.550 SimpleCache:Set Single
15:18:03.551 SimpleCache:"SimpleCache:test":{"value":true,"ttl":1534577434218}
Major Changes:
Minor Changes:
FAQs
A fast, simple and lightweight expiring key value store for modern browsers.
The npm package @stetsonpierce/simple-cache receives a total of 0 weekly downloads. As such, @stetsonpierce/simple-cache popularity was classified as not popular.
We found that @stetsonpierce/simple-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.