{"id":13804468,"url":"https://github.com/e-Potek/method-cache","last_synced_at":"2025-05-13T17:32:23.385Z","repository":{"id":59837075,"uuid":"186988059","full_name":"e-Potek/method-cache","owner":"e-Potek","description":"Meteor method caching using DataLoader","archived":false,"fork":false,"pushed_at":"2023-12-15T02:57:15.000Z","size":49,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-05T01:11:05.504Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/e-Potek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-16T08:42:36.000Z","updated_at":"2024-06-25T01:29:33.000Z","dependencies_parsed_at":"2022-09-22T22:14:41.073Z","dependency_job_id":null,"html_url":"https://github.com/e-Potek/method-cache","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-Potek%2Fmethod-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-Potek%2Fmethod-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-Potek%2Fmethod-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/e-Potek%2Fmethod-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/e-Potek","download_url":"https://codeload.github.com/e-Potek/method-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225247884,"owners_count":17444134,"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-08-04T01:00:48.464Z","updated_at":"2024-11-18T20:32:01.750Z","avatar_url":"https://github.com/e-Potek.png","language":"JavaScript","funding_links":[],"categories":["Performance"],"sub_categories":[],"readme":"# Meteor method cache\n\nUse graphql's DataLoader library to speed up your heavy methods.\n\n*Warning: Probably don't use this in production, you will get errors with complex requests*\n\n\n## Usage\n\nInstall the package in your meteor app:\n\n```sh\nmeteor add epotek:method-cache\n```\n\nStart caching by initializing it on your server:\n\n```js\nimport { initializeMethodCache } from 'meteor/epotek:method-cache';\n\ninitializeMethodCache(options);\n```\n\nEnable caching in any of your methods:\n\n```js\nimport { MethodCache } from 'meteor/epotek:method-cache';\n\nMeteor.methods({\n    heavyMethod() {\n        MethodCache.enableCaching();\n\n        const todos = Todos.find({ userId: this.userId }).fetch();\n\n        const allowedTodos = todos.filter(({ listId, ...todo }) =\u003e {\n            // Each identical todo list will be fetched once\n            const permissions = TodoLists.findOne({ _id: listId });\n            // Will only be fetched once\n            const user = Users.findOne({ _id: this.userId });\n\n            return isAllowedToSeeTodos(permissions, user, todo);\n        });\n\n        return allowedTodos;\n    }\n})\n```\n\n### Options\n\nYou can pass the following options to `initializeMethodCache`:\n\n| Param           | Type    | Default value | Description                                       |\n| --------------- | ------- | ------------- | ------------------------------------------------- |\n| `enableCaching` | Boolean | `false`       | Enables caching on all methods by default         |\n| `log`           | Boolean | `true`        | Enables the initial logging on startup of caching |\n| `logStats`      | Boolean | `false`       | Logs cache hit ratios for all your methods        |\n\n\n### `MethodCache`\n\nThis package exports the `MethodCache` class, which can be used to enable or disable caching in your methods:\n\n* `MethodCache.enableCaching()`: Enables caching on this method, if `options.enableCaching` is set to `false`\n* `MethodCache.disableCaching()`: Disables caching on this method, if `options.enableCaching` is set to `true`\n* `MethodCache.clearCache()`: Can be run anywhere inside a method, clears the entire cache\n\n\n## What it does (and what it doesn't)\n\nThe cacher works by caching any `fetch` that only targets pure `_id`s. Meaning any selector that uses more than an `_id` will not be cached. It's meant to work for these selectors: `{ _id: 'someDocId' }` and `{ _id: { $in: ['someDocId', 'someOtherDocId'] } }`.\nThis is a limitation that can be adressed in the future.\n\nThe cacher adds a little bit of overhead, so if you're only ever fetching one or two duplicate documents in your method, it might be a bit slower.\n\nThe cacher currently only works for methods initiated by a client. Server-side initiated methods are not cached for the moment.\n\nIf you specify any `fields` in your selector, multiple caches will be created based on the `fields`.\n\nWhen you update a document, the cacher will try to identify which document it has to clear from the cache. It works if your update selector targets `_id`s only, just as the caching strategy does. If you use a more complex selector, it will fall back to clearing the entire cache (across collections). If you've used `fields` in your queries, they will not be invalidated.\n\n## Performance example\n\nYou can run the tests with `meteor npm t` in this repo to run some examples on your machine, but here's what it can do:\n\nFetch 1000 identical documents repeatedly (1-field documents) on a local machine (i.e. super fast DB) results in the following stats:\n\n* Without caching: **~350ms**\n* With caching: **~40ms**\n\nWhich is an almost **10x** increase in performance\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe-Potek%2Fmethod-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fe-Potek%2Fmethod-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fe-Potek%2Fmethod-cache/lists"}