{"id":20466899,"url":"https://github.com/fwh1990/cache-bucket","last_synced_at":"2025-04-13T09:10:44.517Z","repository":{"id":57192414,"uuid":"153594260","full_name":"fwh1990/cache-bucket","owner":"fwh1990","description":"Light Cache for nodeJs and browserJs with TTL.","archived":false,"fork":false,"pushed_at":"2018-11-02T04:45:50.000Z","size":51,"stargazers_count":14,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-29T15:14:58.939Z","etag":null,"topics":["browser","cache","localstorage","memory","nodejs","sessionstorage"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/fwh1990.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":"2018-10-18T08:59:10.000Z","updated_at":"2022-08-20T16:52:10.000Z","dependencies_parsed_at":"2022-08-24T05:21:12.421Z","dependency_job_id":null,"html_url":"https://github.com/fwh1990/cache-bucket","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fwh1990%2Fcache-bucket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fwh1990%2Fcache-bucket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fwh1990%2Fcache-bucket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fwh1990%2Fcache-bucket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fwh1990","download_url":"https://codeload.github.com/fwh1990/cache-bucket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247888567,"owners_count":21013002,"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":["browser","cache","localstorage","memory","nodejs","sessionstorage"],"created_at":"2024-11-15T13:26:20.169Z","updated_at":"2025-04-13T09:10:44.493Z","avatar_url":"https://github.com/fwh1990.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction\nCache your data with TTL. Using this package in node.js and browser.\n\n# Installation\n\nBy npm\n```bash\nnpm install cache-bucket\n```\nOr by yarn\n```bash\nyarn add cache-bucket\n```\n\n# Support\n## Node.js\n**FileCache** and **MemoryCache** is enable. \n\nNotice that CacheFile will touch a file as storage. The default file path is: ./.filecache\n\n```js\n// Based on file\nimport {cache} from 'cache-bucket/file-cache';\n\n// Based on memory\nimport {cache} from 'cache-bucket/memory-cache';\n```\n## Browser\n**LocalCache** and **SessionCache** and **MemoryCache** is enable.\n\nNotice that LocalCache is based on localStorage, and SessionCache is based on sessionStorage.\n\n```js\n// Based on memory\nimport {cache} from 'cache-bucket/memory-cache';\n\n// Based on localStorage\nimport {cache} from 'cache-bucket/local-cache';\n\n// Based on sessionStorage\nimport {cache} from 'cache-bucket/session-cache';\n```\n\n# Methods\n### get(key: string, defaultValue?: any) =\u003e any\nGet your cache by key.\n\nIf cache is empty, defaultValue will be used. If parameter defaultValue is missing, method will respond `null`.\n\n```js\ncache.get('foo'); // null\ncache.get('foo', 'default-bar'); // default-bar\n```\n\n### set(key: string, value: any, duration?: number) =\u003e void\nSet cache data.\n\nThe parameter `value` type can be **string**, **number**, **object**, **array**. Cache data will expired after millSeconds when you provide duration.\n\n```js\ncache.set('foo', 'bar');\ncache.set('obj', {pkg: 'cache-bucket'});\n\n// Expired after 3 second.\ncache.set('array', ['cache', 'bucket'], 3000);\n```\n\n### getOrSet(key: string, onEmpty: () =\u003e any, duration?: number) =\u003e any\n\nGet cache data. \n\nWhen cache data is missing, method will set data immediately. And then return it.\n\n```js\ncache.getOrSet('foo', () =\u003e {\n  return 'bar';\n}); // bar\n```\n\n### add(key: string, value: any, duration?: number) =\u003e boolean\nSet cache data when key is not exist.\n\n```js\ncache.add('foo', 'bar'); // true\ncache.add('foo', 'new-bar'); // false\n```\n\n### remove(key: string) =\u003e void\nDelete a cache data.\n\n```js\ncache.remove('foo');\n```\n\n### clearExpired() =\u003e void\n\nClear all expired cache data\n\n```js\ncache.clearExpired();\n```\n\n### clearAll() =\u003e void\n\nClear all data.\n\n```js\ncache.clearAll();\n```\n\n# Advanced\n\n### Multiply instances.\n\n```js\nimport {MemoryCache} from 'cache-bucket/memory-cache';\n\nconst cache = new MemoryCache();\n\ncache.set('foo', 'bar');\ncache.get('foo'); // bar\n```\n\n```js\nimport {FileCache} from 'cache-bucket/file-cache';\n\nconst cache = new FileCache('./.new-cachefile');\n\ncache.set('foo', 'bar');\ncache.get('foo'); // bar\n```\n\n### Config\nYou can choose the file you want to put data when Using **FileCache**. Just creating config file `cache-bucekt.json` in project's root directory.\n```json\n{\n  \"defaultFilePath\": \"./.custom-cache-file\"\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffwh1990%2Fcache-bucket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffwh1990%2Fcache-bucket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffwh1990%2Fcache-bucket/lists"}