{"id":22733479,"url":"https://github.com/strikeentco/icache","last_synced_at":"2026-01-23T11:44:10.844Z","repository":{"id":57270731,"uuid":"65832857","full_name":"strikeentco/icache","owner":"strikeentco","description":":ledger: A simple queue cache","archived":false,"fork":false,"pushed_at":"2017-02-17T13:20:54.000Z","size":11,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T01:02:17.207Z","etag":null,"topics":["cache","fifo","javascript","nodejs","queue"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/strikeentco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-16T15:45:19.000Z","updated_at":"2020-10-29T04:03:52.000Z","dependencies_parsed_at":"2022-09-09T14:13:25.183Z","dependency_job_id":null,"html_url":"https://github.com/strikeentco/icache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strikeentco%2Ficache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strikeentco%2Ficache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strikeentco%2Ficache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strikeentco%2Ficache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/strikeentco","download_url":"https://codeload.github.com/strikeentco/icache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248618260,"owners_count":21134200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","fifo","javascript","nodejs","queue"],"created_at":"2024-12-10T20:14:45.179Z","updated_at":"2026-01-23T11:44:05.805Z","avatar_url":"https://github.com/strikeentco.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"icache [![License](https://img.shields.io/npm/l/icache.svg)](https://github.com/strikeentco/icache/blob/master/LICENSE)  [![npm](https://img.shields.io/npm/v/icache.svg)](https://www.npmjs.com/package/icache)\n==========\n[![Build Status](https://travis-ci.org/strikeentco/icache.svg)](https://travis-ci.org/strikeentco/icache) [![node](https://img.shields.io/node/v/icache.svg)](https://www.npmjs.com/package/icache) [![Test Coverage](https://codeclimate.com/github/strikeentco/icache/badges/coverage.svg)](https://codeclimate.com/github/strikeentco/icache/coverage) [![bitHound Score](https://www.bithound.io/github/strikeentco/icache/badges/score.svg)](https://www.bithound.io/github/strikeentco/icache)\n\n**Note:** *This module stores all data in memory - remember that.*\n\nA simple queue cache which follows FIFO strategy when size is specified.\n\n\u003e **FIFO** is an acronym for **first in, first out**, a method for organizing and manipulating a data buffer, where the oldest (first) entry, or 'head' of the queue, is processed first. [Wiki](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))\n\nWhen size isn't specified it's just a cache storage.\n\nQueue cache is perfectly fit when you need to store last `n` elements, and access to them from time to time.\n\n# Usage\n\n```sh\n$ npm install icache --save\n```\n\n```javascript\nconst Cache = require('icache');\n\nconst cache = new Cache(5);\n\ncache.put('user', 'strikeentco');\ncache.has('user'); // -\u003e true\ncache.put('email', 'strikeentco@gmail.com');\ncache.get('email'); // -\u003e 'strikeentco@gmail.com'\ncache.clear();\ncache.keys(); // -\u003e []\ncache.all(); // -\u003e {}\n\ncache.put('github', { user: 'strikeentco', email: 'strikeentco@gmail.com' });\ncache.keys(); // -\u003e ['github']\ncache.all(); // -\u003e { github: { user: 'strikeentco', email: 'strikeentco@gmail.com' } }\ncache.del('github');\ncache.keys(); // -\u003e []\ncache.all(); // -\u003e {}\n```\n\n# Methods\n\n## new Cache([size])\n\nConstructor.\n\n### Params:\n\n* **[size]** (*Integer*) - Maximum elements number (by default = 0, i.e. no limits)\n\n```javascript\nconst cache = new Cache();\n```\n\n## .length\n\nReturns the number of elements in a Cache.\n\n```javascript\nconst cache = new Cache();\ncache.length; // -\u003e 0\ncache.put('new', 'element');\ncache.length; // -\u003e 1\n```\n\n## .getSize()\n\nReturns Cache size.\n\n```javascript\nconst cache = new Cache();\ncache.getSize();\n```\n\n## .setSize([size])\n\nSets Cache size.\n\n### Params:\n\n* **[size]** (*Integer*) - Maximum elements number (by default = 0, i.e. no limits)\n\n```javascript\nconst cache = new Cache();\ncache.setSize(10);\n```\n\n## .put(key, value, [time])\n\nAdds a new element with a specified key and value to a Cache.\n\n### Params:\n\n* **key** (*String*) - The key of the element\n* **value** (*Mixed*) - The value of the element\n* **[time]** (*Integer|Float*) - Time in seconds after which the element will be removed\n\n```javascript\ncache.put('new', 'element');\ncache.put('another', { element: null });\n```\n\n## .get(key)\n\nReturns a specified element from a Cache.\n\n### Params:\n\n* **key** (*String*) - The key of the element\n\n```javascript\ncache.put('new', 'element');\ncache.get('new'); // -\u003e 'element'\n```\n\n## .has(key)\n\nReturns a boolean indicating whether an element with the specified key exists in a Cache or not.\n\n### Params:\n\n* **key** (*String*) - The key of the element.\n\n```javascript\ncache.put('new', 'element');\ncache.has('new'); // -\u003e true\ncache.has('old'); // -\u003e false\n```\n\n## .keys()\n\nReturns an array of elements keys from a Cache.\n\n```javascript\ncache.put('new', 'element');\ncache.put('newer', 'element');\ncache.keys(); // ['new', 'newer']\n```\n\n## .del(key)\n\nRemoves the specified element from a Cache.\n\n### Params:\n\n* **key** (*String*) - The key of the element.\n\n```javascript\ncache.put('new', 'element').has('new'); // -\u003e true\ncache.del('new').has('new'); // -\u003e false\n```\n\n## .all()\n\nReturns an object with all Cache elements.\n\nOrder is not guaranteed. For correct order, use in combination with [.keys()](#keys). [Example](#adding-new-method).\n\n```javascript\ncache.put('1', null).put('2', null);\ncache.all(); // -\u003e { 1: null, 2: null }\n```\n\n## .clear()\n\nRemoves all elements from a Cache.\n\n```javascript\ncache.put('1', null).put('2', null).all(); // -\u003e { 1: null, 2: null }\ncache.clear().all(); // -\u003e { }\n```\n\n## .expire(key, time)\n\nSets timeout after which element will be removed.\n\nTo remove timeout, set time to 0.\n\n### Params:\n\n* **key** (*String*) - The key of the element\n* **time** (*Integer|Float*) - Time in seconds after which the element will be removed\n\n```javascript\ncache.put('old', 'element');\ncache.expire('old', 1); // will be removed after 1 second\ncache.put('new', 'element', 5); // will be removed after 5 seconds\ncache.expire('new', 0); // will cancel timeout\n```\n\n# Examples\n\n## Adding new method\n\n```javascript\nconst Cache = require('icache');\n\nclass ExtCache extends Cache {\n  allInOrder() { // will return array with all Cache elements in accordance with this.keys() order\n    return this.keys().map(key =\u003e ({ [key]: this.get(key) }));\n  }\n}\n\nconst cache = new ExtCache();\ncache.put(2, 'element');\ncache.put(3, 'element');\ncache.put('1', 'element');\n\ncache.all(); // -\u003e { 1: 'element', 2: 'element', 3: 'element' }\ncache.allInOrder(); // -\u003e [{ 2: 'element' }, { 3: 'element' }, { 1: 'element' }]\n\ncache.setSize(2);\n\ncache.all(); // -\u003e { 1: 'element', 3: 'element' }\ncache.allInOrder(); // -\u003e [{ 3: 'element' }, { 1: 'element' }]\n```\n\n## Caching last 5 requests\n\n```javascript\nconst Cache = require('icache');\nconst app = require('express')();\nconst co = require('co');\nconst get = require('yarl').get;\n\nconst cache = new Cache(5);\n\napp.get('/user/:name', (req, res) =\u003e {\n  co(function* () {\n    const name = req.params.name;\n    if (cache.has(name)) {\n      return res.json(cache.get(name));\n    }\n    const data = (yield get(`https://api.github.com/users/${name}`, { json: true })).body;\n    cache.put(name, data);\n    res.json(data);\n  }).catch(e =\u003e res.status(500).json(e));\n});\n\napp.listen(3000);\n```\n\n## License\n\nThe MIT License (MIT)\u003cbr/\u003e\nCopyright (c) 2016 Alexey Bystrov\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrikeentco%2Ficache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrikeentco%2Ficache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrikeentco%2Ficache/lists"}