{"id":15616125,"url":"https://github.com/boblauer/cachegoose","last_synced_at":"2025-04-04T13:13:58.202Z","repository":{"id":21906515,"uuid":"25230556","full_name":"boblauer/cachegoose","owner":"boblauer","description":"Simple, integrated caching for Mongoose queries.","archived":false,"fork":false,"pushed_at":"2022-06-07T14:13:33.000Z","size":169,"stargazers_count":211,"open_issues_count":15,"forks_count":55,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-10-18T06:43:23.595Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/boblauer.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":"2014-10-14T23:16:45.000Z","updated_at":"2024-10-02T02:34:17.000Z","dependencies_parsed_at":"2022-07-27T02:47:29.216Z","dependency_job_id":null,"html_url":"https://github.com/boblauer/cachegoose","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boblauer%2Fcachegoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boblauer%2Fcachegoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boblauer%2Fcachegoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boblauer%2Fcachegoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boblauer","download_url":"https://codeload.github.com/boblauer/cachegoose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247178193,"owners_count":20896801,"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":[],"created_at":"2024-10-03T07:03:10.495Z","updated_at":"2025-04-04T13:13:58.159Z","avatar_url":"https://github.com/boblauer.png","language":"JavaScript","readme":"# :warning: Seeking New Maintainer #\n\nWhen I wrote this library I used Mongo at work and needed a good caching solution. I no longer use Mongo, and so supporting this library has become a hassle.\n\nIf you would like to take over maintaining this project, please open an issue. Thank you!\n\n# cachegoose #\n\n#### Mongoose caching that actually works. ####\n\n[![Build Status](https://travis-ci.org/boblauer/cachegoose.svg)](https://travis-ci.org/boblauer/cachegoose)\n\n## About ##\n\nA Mongoose caching module that works exactly how you would expect it to, with the latest version of Mongoose.\n\n```\nImportant:\n\nIf you are using Mongoose 4.x or below, you need to use version 4.0.1 of this library. \n\nhis is due to Mongoose 5.x no longer having a custom Promise solution(which previous\nversion of this library relied upon). - https://github.com/Automattic/mongoose/issues/2917\n```\n\n## Usage ##\n\n```javascript\nvar mongoose = require('mongoose');\nvar cachegoose = require('cachegoose');\n\ncachegoose(mongoose, {\n  engine: 'redis',    /* If you don't specify the redis engine,      */\n  port: 6379,         /* the query results will be cached in memory. */\n  host: 'localhost'\n});\n\nRecord\n  .find({ some_condition: true })\n  .cache(30) // The number of seconds to cache the query.  Defaults to 60 seconds.\n  .exec(function(err, records) {\n    ...\n  });\n\nRecord\n  .aggregate()\n  .group({ total: { $sum: '$some_field' } })\n  .cache(0) // Explicitly passing in 0 will cache the results indefinitely.\n  .exec(function(err, aggResults) {\n    ...\n  });\n```\n\nYou can also pass a custom key into the `.cache()` method, which you can then use later to clear the cached content.\n\n```javascript\nvar mongoose = require('mongoose');\nvar cachegoose = require('cachegoose');\n\ncachegoose(mongoose, {\n  engine: 'redis',\n  port: 6379,\n  host: 'localhost'\n});\n\nvar userId = '1234567890';\n\nChildren\n  .find({ parentId: userId })\n  .cache(0, userId + '-children') /* Will create a redis entry          */\n  .exec(function(err, records) {  /* with the key '1234567890-children' */\n    ...\n  });\n\nChildrenSchema.post('save', function(child) {\n  // Clear the parent's cache, since a new child has been added.\n  cachegoose.clearCache(child.parentId + '-children');\n});\n```\n\nInsert `.cache()` into the queries you want to cache, and they will be cached.  Works with `select`, `lean`, `sort`, and anything else that will modify the results of a query.\n\n## Clearing the cache ##\n\nIf you want to clear the cache for a specific query, you must specify the cache key yourself:\n\n```js\nfunction getChildrenByParentId(parentId, cb) {\n  Children\n    .find({ parentId })\n    .cache(0, `${parentId}_children`)\n    .exec(cb);\n}\n\nfunction clearChildrenByParentIdCache(parentId, cb) {\n  cachegoose.clearCache(`${parentId}_children`, cb);\n}\n```\n\nIf you call `cachegoose.clearCache(null, cb)` without passing a cache key as the first parameter, the entire cache will be cleared for all queries.\n\n## Caching populated documents ##\n\nWhen a document is returned from the cache, cachegoose will [hydrate](http://mongoosejs.com/docs/api.html#model_Model.hydrate) it, which initializes it's virtuals/methods. Hydrating a populated document will discard any populated fields (see [Automattic/mongoose#4727](https://github.com/Automattic/mongoose/issues/4727)). To cache populated documents without losing child documents, you must use `.lean()`, however if you do this you will not be able to use any virtuals/methods (it will be a plain object).\n\n## Test ##\nnpm test\n","funding_links":[],"categories":["📦 Legacy \u0026 Inactive Projects","⚡ Caching \u0026 Performance"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboblauer%2Fcachegoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboblauer%2Fcachegoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboblauer%2Fcachegoose/lists"}