On this page, you can find examples of using the memory-based driver.
This is the default diver of the storage-box
package, No further configuration is required.
import { Client } from 'storage-box';
const c = new Client();
client.set('key', 'value');
const value = client.get('key');
const objs = client.getall();
client.del('key');
client.clear();
const exists = client.exists('key');
const has = client.has('key'); // has is an alias for exists
Length of the storage
const size = client.size();
const keys = client.keys();
const values = client.values();
client.setex('key', 'value', 10); // 10 seconds
Returns the remaining time in seconds
const ttl = client.ttl('key');
const ttlMs = client.ttl('key', true); // Returns the remaining time in milliseconds
interface Vertex {
x: number;
y: number;
}
const hash = client.createHashMap<string, Vertex>();
import { expect } from 'chai';
import type { JsonObject } from 'storage-box';
interface CuteUser extends JsonObject {
first: string;
last: string;
}
class CuteMap extends HashMap<string, CuteUser> {
async addUser(user: CuteUser) {
const randId = Math.random().toString(36).slice(2);
await this.set(randId, user);
}
async initials(): string[] {
const all = await this.getall();
return Object.values(all).map((u) => `${u.first[0]}${u.last[0]}`);
}
}
const cuties = client.createHashMap(CuteMap);
await cuties.addUser({ first: 'Mary', last: 'Jane' });
await cuties.addUser({ first: 'Peter', last: 'Parker' });
const initials = await cuties.initials();
expect(initials).to.have.members(['MJ', 'PP']);
client.hset('key', 'field', 'value');
const value = client.hget('key', 'field');
const map = client.hgetall('key');
client.hsetex('key', 'field', 'value', 10); // 10 seconds
const keys = client.hkeys('key');
const values = client.hvalues('key');
client.hdel('key', 'field');
const exists = client.hexists('key', 'field');
const size = client.hsize('key');
client.hclear('key');