{"id":22514979,"url":"https://github.com/tiagoguirra/cache-on-function","last_synced_at":"2025-03-28T01:53:07.091Z","repository":{"id":57192475,"uuid":"310024581","full_name":"tiagoguirra/cache-on-function","owner":"tiagoguirra","description":"cache and optimize function calls without modification","archived":false,"fork":false,"pushed_at":"2022-01-04T19:41:59.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-04T07:47:07.179Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/tiagoguirra.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":"2020-11-04T14:20:54.000Z","updated_at":"2022-01-04T19:42:02.000Z","dependencies_parsed_at":"2022-08-24T05:21:14.805Z","dependency_job_id":null,"html_url":"https://github.com/tiagoguirra/cache-on-function","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/tiagoguirra%2Fcache-on-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagoguirra%2Fcache-on-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagoguirra%2Fcache-on-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiagoguirra%2Fcache-on-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiagoguirra","download_url":"https://codeload.github.com/tiagoguirra/cache-on-function/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245955201,"owners_count":20699891,"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-12-07T03:27:27.818Z","updated_at":"2025-03-28T01:53:07.067Z","avatar_url":"https://github.com/tiagoguirra.png","language":"TypeScript","readme":"# cache-on-function\n\nCache on function is a simple library to implement caching over a function easily and without changes to the original function.\n\n\n## Install\n\n```bash\n$ npm install cache-on-function\n```\n\n## Usage\n\nThen create a cache-on-function instance passing redis and cache options:\n\n```javascript\nconst { CacheOnFunction } = require('cache-on-function')\nconst cacheEngine = new CacheOnFunction({\n  host: 'localhost',\n  port: 6379,\n  connect_timeout: 3600000,\n  retry_strategy: (options) =\u003e {\n    return 2000\n  },\n},{\n    cache_default: true,\n    expire_time: 6000,\n    key_prefix: 'cache-on-function',\n})\n```\n\n##### create cache over async function\n\nCreate asynchronous function with you implementation;\n\u003e The only requirement is that the function only takes one argument, it is necessary to pass an object if it needs multiple arguments.\n\n```javascript\nconst myAsyncFunction = ({arga,argb})=\u003e{\n  return new Promise((resolve,reject)=\u003e {\n    resolve('Resolved')\n  })\n}\n```\nCreates a layer surrounding the function\n```javascript\nconst myFunctionWithCache = cacheEngine.async(myAsyncFunction)\n```\n\nCall a function with cache the same way called original function\n\n```javascript\nawait myFunctionWithCache({arga:'Argment a',argb: 'Argument b'})\n```\n\nIf you define default cache as True, the simple call will usage cache by default, but is possible call function without cache\n\n```javascript\nawait myFunctionWithCache.nocache({arga:'Argment a',argb: 'Argument b'})\n```\n\nIf you define default cache as False, the simple call will not use cache by default, but is possible call function with cache\n\n```javascript\nawait myFunctionWithCache.withcache({arga:'Argment a',argb: 'Argument b'})\n```\n\n##### create cache over sync function\n\nCreate synchronous function with you implementation\n\u003e The function only takes one argument, it is necessary to pass an object if it needs multiple arguments.\n\u003e The function with cache will return a promise, then need resolve promise to receive value\n\n\n```javascript\nconst mySyncFunction = ({arga,argb})=\u003e{\n  return 'My sync response'\n}\n```\nCreates a layer surrounding the function\n```javascript\nconst myFunctionWithCache = cacheEngine.sync(mySyncFunction)\n```\n\nIf you define default cache as True, simple call will return cache function as promise, else return original function synchronous\n\n```javascript\nawait myFunctionWithCache({arga:'Argment a',argb: 'Argument b'})\n```\n\nIf you define default cache as True, the simple call will usage cache by default, but is possible call function without cache\n\u003e Note that in this case the original function will be called so it will return the synchronous response\n\n```javascript\nmyFunctionWithCache.nocache({arga:'Argment a',argb: 'Argument b'})\n```\n\nIf you define default cache as False, the simple call will not use cache by default, but is possible call function with cache\n\n```javascript\nawait myFunctionWithCache.withcache({arga:'Argment a',argb: 'Argument b'})\n```\n\n##### create cache over callback function\n\nCreate callback function with you implementation\n\u003e The function only takes one argument, it is necessary to pass an object if it needs multiple arguments.\n\u003e The function cache and no cache will response using callback.\n\n\n```javascript\nconst myCbFunction = ({arga,argb},callback = (err,data)=\u003e{})=\u003e{\n  callback(null,'My sync response')\n}\n```\nCreates a layer surrounding the function\n```javascript\nconst myCbFunctionWithCache = cacheEngine.callback(myCbFunction)\n```\n\nCall a function with cache the same way called a original function\n\n```javascript\nmyCbFunctionWithCache({arga:'Argment a',argb: 'Argument b'},(err,data)=\u003e{\n  if (err) console.log('Error',err)\n  else console.log('Response',data)\n})\n```\n\nIf you define default cache as True, the simple call will usage cache by default, but is possible call function without cache\n\n```javascript\nmyCbFunctionWithCache.nocache({arga:'Argment a',argb: 'Argument b'},(err,data)=\u003e{\n  if (err) console.log('Error',err)\n  else console.log('Response',data)\n})\n```\n\nIf you define default cache as False, the simple call will not use cache by default, but is possible call function with cache\n\n```javascript\nmyCbFunctionWithCache.withcache({arga:'Argment a',argb: 'Argument b'},(err,data)=\u003e{\n  if (err) console.log('Error',err)\n  else console.log('Response',data)\n})\n```\n\n\n##### Invalidate function cache\n\nBy default caches will expire after time, but you can invalidate using a combination with function name and parameters, predefined key on surrounding function or using patterns\n\nInvalidate using name function and paramters\n\u003e Use the original name of function before surrounding cache\n\n```javascript\nawait cacheEngine.invalidateCache('mySyncFunction',{arga:'Argment a',argb: 'Argument b'})\n```\n\nInvalidate using key predefined on surrounding function\n\nOn surrounding function define key\n\u003e Note you can use arguments name to composite a cache key, use 'mey_custom_key_{{name_of_argument}}'\n```javascript\nconst myFunctionWithCache = cacheEngine.async(myAsyncFunction, {key: 'mey_custom_key'})\n```\nThen invalidate cache using custom key\n\n```javascript\nawait cacheEngine.invalidateCache('mey_custom_key')\n```\n\nInvalidate using patters\n\u003e This way can be used for function original name or custom key\n\n```javascript\nawait cacheEngine.invalidateCache('mySync*',{},true)\n```\n\n#### `options` object properties\n\nThe cache instance accepts argument options :\n\n- `new CacheOnRedis(redisOptions[,options])`\n\n| Property     | Default | Description                                                    |\n| ------------ | ------- | -------------------------------------------------------------- |\n| cache_default| true    | Define if default response will usage cache or not             |\n| key_prefix   | cache   | Cache key prefix, every cache storage will contain this prefix |\n| expire_time  | 3600    | Cache expiration time in seconds                              |\n\n\nThe cache mathods accepts argument options:\n\u003e The same options is valid to async, sync and callback methods\n\n- `cacheEngine.async(myFunction[,options])`\n\n| Property     | Default | Description                                                    |\n| ------------ | ------- | -------------------------------------------------------------- |\n| key          |         | Define a custom key to use on redis cache key and to invalidate|\n| expireAs     | 3600    | Cache expiration time in seconds                               |\n\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagoguirra%2Fcache-on-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiagoguirra%2Fcache-on-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiagoguirra%2Fcache-on-function/lists"}