{"_id":"@anasakil/storage-cache-anas","name":"@anasakil/storage-cache-anas","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@anasakil/storage-cache-anas","version":"1.0.0","description":"A lightweight key-value cache with optional disk persistence for any data type in Node.js","main":"src/index.js","scripts":{"start":"node src/index.js","test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git+https://github.com/anasakil/storage-cache-anas.git"},"author":{"name":"anas akil"},"license":"ISC","bugs":{"url":"https://github.com/anasakil/storage-cache-anas/issues"},"homepage":"https://github.com/anasakil/storage-cache-anas#readme","_id":"@anasakil/storage-cache-anas@1.0.0","gitHead":"f0d1be34dc7d1fdeef210d67fa5be9ae3b28f2f0","_nodeVersion":"22.12.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-8RiwHySQzwyNO7DN1B71Rm80gDHF2cpia25QnOjjmBAJ6/Agmt6ke6WL2t6L3ifF1cWp2ebmW5HIv6YPFsuINQ==","shasum":"9ae53e9361b8eab874fd470b842ad29afba2db2e","tarball":"https://registry.npmjs.org/@anasakil/storage-cache-anas/-/storage-cache-anas-1.0.0.tgz","fileCount":6,"unpackedSize":9271,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDROXEyKvnFODetrD0zKXadiVTu0sJRQ7LKACRFhyhCuwIgIUxLZwTm0VQb+X4CD9UWcnvexWPcU3SPOGiRGt40cKc="}]},"_npmUser":{"name":"anasakil","email":"farawiakil@gmail.com"},"directories":{},"maintainers":[{"name":"anasakil","email":"farawiakil@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/storage-cache-anas_1.0.0_1740591555263_0.84530351469122"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-26T17:39:15.167Z","1.0.0":"2025-02-26T17:39:15.460Z","modified":"2025-02-26T17:39:15.724Z"},"maintainers":[{"name":"anasakil","email":"farawiakil@gmail.com"}],"description":"A lightweight key-value cache with optional disk persistence for any data type in Node.js","homepage":"https://github.com/anasakil/storage-cache-anas#readme","repository":{"type":"git","url":"git+https://github.com/anasakil/storage-cache-anas.git"},"author":{"name":"anas akil"},"bugs":{"url":"https://github.com/anasakil/storage-cache-anas/issues"},"license":"ISC","readme":"# @anasakil/storage-cache-anas\r\n\r\nA lightweight key-value storage cache for Node.js with optional disk persistence, supporting any JavaScript data type.\r\n\r\n## Installation\r\n```bash\r\nnpm install @anasakil/storage-cache-anas\r\n\r\n* ## Features\r\n * - Store any data type (strings, numbers, booleans, objects, arrays, etc.) in memory.\r\n * - Optional JSON file persistence for JSON-compatible types.\r\n * - TTL (time-to-live) support for expiring entries.\r\n * - Automatic cleanup of expired entries.\r\n * - No dependencies.\r\n *\r\n * ## Usage\r\n *\r\n * ### Storing Different Data Types\r\n * ```javascript\r\n * const StorageCache = require('@anasakil/storage-cache-anas');\r\n * const cache = new StorageCache({ persist: './data/cache.json' });\r\n *\r\n * (async () => {\r\n *   // String\r\n *   await cache.set('name', 'Anas', 5000);\r\n *   console.log(cache.get('name')); // 'Anas'\r\n *\r\n *   // Number\r\n *   await cache.set('age', 30);\r\n *   console.log(cache.get('age')); // 30\r\n *\r\n *   // Object\r\n *   await cache.set('user', { id: 1, name: 'Anas' }, 10000);\r\n *   console.log(cache.get('user')); // { id: 1, name: 'Anas' }\r\n *\r\n *   // Array\r\n *   await cache.set('items', [1, 2, 3]);\r\n *   console.log(cache.get('items')); // [1, 2, 3]\r\n *\r\n *   // Boolean\r\n *   await cache.set('active', true);\r\n *   console.log(cache.get('active')); // true\r\n *\r\n *   // Non-JSON type (memory-only with warning)\r\n *   await cache.set('func', () => console.log('test'));\r\n *   console.log(cache.get('func')); // [Function] (won’t persist to disk)\r\n * })();\r\n * ```\r\n *\r\n * ### Persistence Across Restarts\r\n * ```javascript\r\n * const StorageCache = require('@anasakil/storage-cache-anas');\r\n *\r\n * (async () => {\r\n *   const cache = new StorageCache({ persist: './data/cache.json' });\r\n *   await cache.set('config', { host: 'localhost', port: 3000 }, 60000);\r\n *   console.log(cache.get('config')); // { host: 'localhost', port: 3000 }\r\n *\r\n *   // Simulate restart\r\n *   const newCache = new StorageCache({ persist: './data/cache.json' });\r\n *   await newCache.init();\r\n *   console.log(newCache.get('config')); // { host: 'localhost', port: 3000 } (if not expired)\r\n * })();\r\n * ```\r\n *\r\n * ### Clear Cache\r\n * ```javascript\r\n * const cache = new StorageCache();\r\n * await cache.set('key', [1, 2, 3]);\r\n * await cache.clear();\r\n * console.log(cache.get('key')); // null\r\n * ```\r\n *\r\n * ## Methods\r\n * - **`.set(key, value, ttlMs)`**: Set any value with an optional TTL (in milliseconds).\r\n *   - `key`: A string identifier for the value.\r\n *   - `value`: Any JavaScript data type (string, number, object, array, etc.).\r\n *   - `ttlMs`: Optional expiration time in milliseconds (e.g., `5000` for 5 seconds); if omitted, the value persists until manually deleted or cleared.\r\n * - **`.get(key)`**: Get a value by key; returns `null` if expired or not found.\r\n *   - `key`: The string identifier to retrieve.\r\n * - **`.delete(key)`**: Delete a key and its associated value.\r\n *   - `key`: The string identifier to remove.\r\n * - **`.has(key)`**: Check if a key exists and isn’t expired; returns a boolean.\r\n *   - `key`: The string identifier to check.\r\n * - **`.clear()`**: Clear all entries from both memory and disk (if persistence is enabled).\r\n *\r\n * ## Configuration\r\n * - **`persist`**: Path to a JSON file for persistence (e.g., `'./cache.json'`) or `false` for memory-only storage.\r\n *   - Default: `false`.\r\n *   - Example: `{ persist: './data/cache.json' }` saves data to `data/cache.json`.\r\n *\r\n * ## Notes\r\n * - **Supported Types:** All JSON-compatible types (strings, numbers, booleans, objects, arrays, `null`) are fully supported in memory and on disk.\r\n * - **Non-JSON Types:** Functions, `undefined`, `Map`, `Set`, etc., are stored in memory but not persisted to disk (a warning is logged when attempting to persist these).\r\n * - **TTL:** Expired entries are automatically removed from both memory and disk.\r\n *\r\n * ## License\r\n * MIT\r\n */","readmeFilename":"README.md"}