{"id":18698163,"url":"https://github.com/wujjpp/node-cache-server","last_synced_at":"2025-11-08T17:30:29.455Z","repository":{"id":130020433,"uuid":"87402017","full_name":"wujjpp/node-cache-server","owner":"wujjpp","description":"nodejs多级缓存服务","archived":false,"fork":false,"pushed_at":"2017-04-06T09:34:26.000Z","size":32,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-28T04:24:56.711Z","etag":null,"topics":["cache","cache-objectscript","muti-cache","node"],"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/wujjpp.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-06T07:53:38.000Z","updated_at":"2018-03-07T06:21:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"9e9b0671-3d59-4b6d-890d-316198ea62f6","html_url":"https://github.com/wujjpp/node-cache-server","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/wujjpp%2Fnode-cache-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wujjpp%2Fnode-cache-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wujjpp%2Fnode-cache-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wujjpp%2Fnode-cache-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wujjpp","download_url":"https://codeload.github.com/wujjpp/node-cache-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239558927,"owners_count":19658929,"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","cache-objectscript","muti-cache","node"],"created_at":"2024-11-07T11:27:18.088Z","updated_at":"2025-11-08T17:30:29.419Z","avatar_url":"https://github.com/wujjpp.png","language":"JavaScript","readme":"# node cache server\n\nnode缓存服务器，支持多级缓存，允许自定义store, 内置store包含 memory, redis以及阿里云OTS\n\n## 安装\n```shell\n$ git clone https://github.com/wujjpp/node-cache-server.git\n```\n\n## 运行\n\n#### 服务端\n```shell\n$ cd server\n$ npm start\n```\n\n#### 运行测试客户端\n```shell\n$ cd client\n$ npm run benchmark\n```\n\n#### 客户端SDK使用\n```javascript\nvar cache = require('./client/cache');\n\nvar client = cache.create({\n  uri: 'http://127.0.0.1:9999', // 缓存服务器地址\n  apiKey: '123456789' // 必须与服务端的apiKey保持一致\n});\n\nclient\n  .get(\"category-name\", \"the-key\", function() {\n    return new Promise(function(resolve, reject) {\n      // Load data from database, for simple case, we just hardcode returned value\n      resolve({ id: 'foo', name: 'Jane' });      \n    });\n  })\n  .then(function(data) {\n    console.log('result:', data);\n  })\n```\n上面的代码执行流程： client先会尝试从缓存服务器的`category-name`目录取key为`the-key`值。存在的情况下，直接返回对应对象，假如不存在，会执行第三个参数（即回调函数），去慢速介质（比如：数据库）查询数据，框架会将resolve的结果自动填充到缓存服务器，以备下次使用。\n\n## 服务端配置\n___/server/configs/config.js___ 环境配置\n\n```javascript\n'use strict';\nvar env = 'dev'; //运行环境：dev, sit, uat, prod根据配置读取对应目录的cache.js配置文件\n\nvar path = require('path');\nvar _ = require('lodash');\nvar glob = require('glob');\n\nvar sharedConfig = require('./shared');\nvar config = {\n  env: env\n};\n\n_.extend(config, sharedConfig);\nvar configFiles = glob.sync(path.join(__dirname, env, '**', '*.js'));// sync operation is require\n\nmodule.exports = _.extend(config, _.chain(configFiles)\n  .map(function (o) {\n    return require(o);\n  })\n  .reduce(function (o, n) {\n    return _.extend(o, n);\n  }, config).value());\n```\n\n___/server/configs/(dev|sit|uat|prod)/cache.js___ 缓存store配置\n\n```javascript\n\nmodule.exports = {\n  caches: [\n    //一级缓存配置为内存\n    {\n      store: 'memory',\n      ttl: 60 * 5, //expire time in seconds, 5 mins\n      max: 600, //max key count\n    },\n\n    //二级缓存配置成redis\n    {\n      store: 'redis',\n      ttl: 60 * 10, //expire time in seconds, 10 mins\n      ignoreCacheErrors: false, //ignore storage errors\n\n      //下面是redis相关设置\n      host: \"127.0.0.1\",\n      port: 6379,\n      password: \"xxxxxxxxx\",\n      db: 0\n    },\n\n    //三级缓存配置成阿里云的OTS\n    {\n      //阿里云OTS\n      store: 'ots',\n      ttl: 60 * 20, //expire time in seconds, 20 minutes\n      ignoreCacheErrors: false, //ignore storage errors\n\n      //下面是OTS相关设置\n      accessKeyId: 'Mf7kHLKXXXXXXXXX',\n      secretAccessKey: 'o3Qv0NdgjpXXXXXXXXXXXXXX',\n      endpoint: 'http://cn-hangzhou.ots.aliyuncs.com',\n      apiVersion: '2014-08-08',\n      instance_name: 'cachetest',\n      table_name: 'dev'\n    }\n  ]\n};\n\n```\n\n根据实际情况进行调整，当然内存足够大的情况下，你可以直接使用memory，建议memory + redis, 这样缓存服务挂掉的情况下，可以使用redis作为backing store\n\n关于ttl，ttl用来控制缓存过期时间，单位为“秒”，越靠前的缓存存储ttl值应该是越小\n\n理论上越靠前的缓存存储器应该是读写速度应该越快，当然成本也是越高的。\n\n___/server/configs/shared.js___ 配置缓存服务器端口及apiKey\n```javascript\nmodule.exports = {\n  port: 9999, //缓存服务器访问端口\n  apiKey: '123456789' //API key\n};\n\n```\n\nMade with ♥ by Wu Jian Ping\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwujjpp%2Fnode-cache-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwujjpp%2Fnode-cache-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwujjpp%2Fnode-cache-server/lists"}