EasyTempStorage
is designed for temporary, in-memory data storage. It's ideal for data that needs to be accessible throughout the app's lifecycle but not persisted between sessions. This makes it perfect for session data, temporary caches, or any scenario where data persistence beyond the app's current session is unnecessary.
npm i @silver-zepp/easy-storage
import { EasyTempStorage } from "@silver-zepp/easy-storage";
const temp = new EasyTempStorage();
temp.setKey("session", { token: "abc123" });
console.log(temp.getKey("session"));
setKey(key, value)
: Temporarily stores a value for a key.getKey(key, defaultValue)
: Retrieves a value of a key.hasKey(key)
: Checks existence of a key.removeKey(key)
: Deletes a key and its value.deleteAll()
: Clears all keys and values.printAllKeys()
: Displays all keys and values.getAllKeys(stringify)
: Lists all keys in temporary storage.
Initializes the temporary storage instance.
const temp = new EasyTempStorage();
Stores a value associated with a key in memory. The value is kept until the application is closed or explicitly removed.
key
{string} - The key under which to store the value.value
{any} - The value to store.
temp.setKey('session', { user: 'John Doe', token: 'abc123' });
Retrieves the value associated with the specified key from the storage.
key
{string} - The key whose value to retrieve.
console.log(temp.getKey('session'));
any
- The value associated with the key, or undefined
if not found.
Checks if the specified key exists in the storage.
key
{string} - The key to check.
console.log(temp.hasKey('session')); // true or false
boolean
- True if the key exists, false otherwise.
Removes the specified key and its value from the storage.
key
{string} - The key to remove.
temp.removeKey('session');
Retrieves all keys stored in the storage, optionally stringified.
stringify
{boolean} - If true, returns the keys as a JSON string; otherwise, as an array.
console.log(temp.getAllKeys());
console.log(temp.getAllKeys(true));
string | array
- The keys as a JSON string if stringify
is true, or as an array if false.
Removes all keys and their values from the storage.
temp.deleteAll();
Prints all keys and their associated values to the console.
temp.printAllKeys();