flk-cache

raw JSON →
0.1.67 verified Sat Apr 25 auth: no javascript

Browser localStorage-based caching library, part of the Falak framework ecosystem. Current stable version: 0.1.67. Provides a simple API (set, get, has, remove, clear) with automatic serialization of objects/arrays, optional encryption of stored values, and configurable expiration using timestamps. Designed for use with DI container; supports ESM/CommonJS. Lightweight, no external dependencies, encrypts values by default via flk-crypto. Differentiates from general caching libraries by deep Falak integration and encryption-at-rest built-in.

error TypeError: DI.resolve is not a function
cause flk-core (DI container) is not installed or imported before flk-cache.
fix
Install and import flk-core first: import { DI } from 'flk-core'; import 'flk-cache';
error Error: Module "flk-cache" does not provide a default export
cause Attempting default import when the module only exports named exports.
fix
Use named import: import { getCache } from 'flk-cache';
error ReferenceError: localStorage is not defined
cause Running in environment without localStorage (e.g., Node.js, SSR).
fix
Use a polyfill like 'localstorage-polyfill' or avoid using cache server-side.
error Error: Cache value must be a valid timestamp for expiresAt
cause Passing a non-number (e.g., string) as expiresAt parameter.
fix
Provide a numeric timestamp: cache.set('key', value, Date.now() + 60000);
gotcha Cache values are encrypted by default if flk-crypto is installed. Encrypted values are not human-readable in localStorage.
fix Set 'encryptValues' to false in flk-config under 'cache' key to disable encryption.
gotcha The set() method's expiresAt parameter expects a timestamp in milliseconds (e.g., Date.now() + duration), not a duration string.
fix Use Cache.FOR_ONE_HOUR constant or calculate: Date.now() + 3600000.
deprecated Using require('flk-cache') is deprecated. Import as ESM module.
fix Use import { getCache } from 'flk-cache'.
gotcha The cache relies on browser localStorage; not available in Node.js without a polyfill.
fix Use only in browser environment or provide localStorage polyfill for Node.
npm install flk-core
yarn add flk-core
pnpm add flk-core

Demonstrates basic CRUD operations: set, get, has, remove, clear with object support and expiration.

import { DI } from 'flk-core';
import 'flk-cache';

// Ensure flk-cache is registered with DI
const cache = DI.resolve('cache');

// Set a value
cache.set('username', 'john_doe');

// Get a value
const username = cache.get('username');
console.log(username); // 'john_doe'

// Set with expiration (1 hour from now)
const oneHour = Date.now() + 3600000;
cache.set('session', { token: 'abc123' }, oneHour);

// Check existence
if (cache.has('username')) {
  console.log('Key exists');
}

// Remove a key
cache.remove('username');

// Clear all cache
cache.clear();