{"id":21944051,"url":"https://github.com/rfrench/simple-cluster-cache","last_synced_at":"2025-04-22T21:05:21.855Z","repository":{"id":34133474,"uuid":"37966161","full_name":"rfrench/simple-cluster-cache","owner":"rfrench","description":"Dead simple cluster based in-memory cache that allows the master process and the worker processes to share the same object cache.","archived":false,"fork":false,"pushed_at":"2015-07-11T16:42:15.000Z","size":184,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-22T21:05:14.888Z","etag":null,"topics":["cache","cluster","master","node","nodejs","npm","worker"],"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/rfrench.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":"2015-06-24T06:16:21.000Z","updated_at":"2019-06-28T01:31:59.000Z","dependencies_parsed_at":"2022-08-17T23:10:20.475Z","dependency_job_id":null,"html_url":"https://github.com/rfrench/simple-cluster-cache","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/rfrench%2Fsimple-cluster-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rfrench%2Fsimple-cluster-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rfrench%2Fsimple-cluster-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rfrench%2Fsimple-cluster-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rfrench","download_url":"https://codeload.github.com/rfrench/simple-cluster-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250324689,"owners_count":21411945,"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","cluster","master","node","nodejs","npm","worker"],"created_at":"2024-11-29T04:13:09.015Z","updated_at":"2025-04-22T21:05:21.833Z","avatar_url":"https://github.com/rfrench.png","language":"JavaScript","readme":"# simple-cluster-cache [![Build Status](https://api.travis-ci.org/rfrench/simple-cluster-cache.svg?branch=master)](https://travis-ci.org/rfrench/simple-cluster-cache) [![NPM version](https://badge.fury.io/js/simple-cluster-cache.png)](http://badge.fury.io/js/simple-cluster-cache)\nDead simple cluster based in-memory cache that allows the master process and the worker processes to share the same object cache.\n\n## Install\n```bash\n$ npm install simple-cluster-cache\n```\n\n## Methods\n- `get(key, callback)` - Gets the value from cache. Returns undefined if the value does not exist.\n    - `key`: The unique cache object key or an array of keys.\n    - `callback`: The optional callback function.\n- `set(key, value, ttl, callback)` - Sets a cache object for the specified key.\n    - `key`: The unique cache object key.\n    - `value`: The cache object value.\n    - `ttl`: The optional time to live on the cache object. (milliseconds)\n    - `callback`: The optional callback function. \n- `del(key, callback)` - Deletes a key from cache.\n    - `key`: The unique cache object key or an array of keys.\n    - `callback`: The optional callback function.\n- `clear(callback)` - Clears the cache.\n    - `callback`: The optional callback function.\n\n## Usage\n``` js\ncache.set('hello', 'world', function(err, value) {\n  console.log(value);\n});\n\ncache.set('world', 'hello', function(err, value) {\n  console.log(value);\n});\n\n// single get\ncache.get('hello', function(err, value) {\n  // prints 'world'\n  console.log(value);\n});\n\n// multi get\ncache.get(['hello', 'world'], function(err, value) {\n  // prints '{ 'hello': 'world', 'world': 'hello' }'\n  console.log(value);\n});\n\n// single delete\ncache.del('hello', function(err) {\n  console.log('deleted');  \n});\n\n// multi delete\ncache.del(['hello', 'world'], function(err) {\n  console.log('deleted');  \n});\n\n// clears the entire cache\ncache.clear(function() {\n  console.log('cache cleared!');\n});\n```\n\n## Example\n``` js\n'use strict';\n\nvar cluster = require('cluster');\nvar cache = require('simple-cluster-cache');\nvar express = require('express');\nvar os = require('os');\n\nif (cluster.isMaster) {\n\n  var cpus = os.cpus().length;\n  for (var i = 0; i \u003c cpus; i++) {\n    cluster.fork();\n  }\n\n  cluster.on('exit', function(worker, code, signal) {\n    cluster.fork();\n  });\n\n  // preload some keys just to demonstrate it works from the master thread\n  cache.clear();\n  cache.set('hello', 'world', 5000);\n  cache.set('object', { 'hello': 'world' });\n  cache.set('array', [ 1, 2, 3 ]);\n  cache.set('callback', true, function(err, value) {\n    console.log('successfully set \"callback\" to %s', value);\n  });\n}\nelse {\n  var app = express();\n\n  // gets the key from cache\n  app.get('/:key', function(req, res, next) {\n    var key = req.params.key;\n    cache.get(key, function(err, value) {\n      res.json(value);\n    });\n  });\n\n  // sets the cache value\n  app.post('/:key/:value/:ttl?', function(req, res, next) {\n    var key = req.params.key;\n    var value = req.params.value;\n    var ttl = req.params.ttl;\n\n    cache.set(key, value, ttl, function(err, value) {\n      res.json(value);\n    });\n  });\n\n  // deletes the cache key\n  app.delete('/:key', function(req, res, next) {\n    var key = req.params.key;\n    \n    cache.del(key, function(err) {\n      res.json(key + ' was deleted.');\n    });\n  });\n\n  // clears the cache\n  app.purge('/clear', function(req, res, next) {\n    console.log('clear')\n    cache.clear(function() {\n      res.json('cache cleared!');\n    });\n  });\n\n  app.listen(process.env.PORT || 8000);\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frfrench%2Fsimple-cluster-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frfrench%2Fsimple-cluster-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frfrench%2Fsimple-cluster-cache/lists"}