{"id":13787246,"url":"https://github.com/helnokaly/adonis-cache","last_synced_at":"2025-05-12T00:30:48.747Z","repository":{"id":57173552,"uuid":"70355856","full_name":"helnokaly/adonis-cache","owner":"helnokaly","description":"Cache provider for AdonisJS framework","archived":false,"fork":false,"pushed_at":"2019-02-17T01:33:41.000Z","size":97,"stargazers_count":69,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-20T15:52:08.795Z","etag":null,"topics":["adonisjs","cache","caching","nodejs","redis"],"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/helnokaly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-08T20:31:05.000Z","updated_at":"2024-12-31T07:05:58.000Z","dependencies_parsed_at":"2022-08-24T14:40:43.149Z","dependency_job_id":null,"html_url":"https://github.com/helnokaly/adonis-cache","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/helnokaly%2Fadonis-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helnokaly%2Fadonis-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helnokaly%2Fadonis-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helnokaly%2Fadonis-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/helnokaly","download_url":"https://codeload.github.com/helnokaly/adonis-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253655782,"owners_count":21943068,"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":["adonisjs","cache","caching","nodejs","redis"],"created_at":"2024-08-03T20:00:31.909Z","updated_at":"2025-05-12T00:30:48.192Z","avatar_url":"https://github.com/helnokaly.png","language":"JavaScript","funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"# AdonisCache\n\nThis is a cache service provider for AdonisJS framework\n\n- [Installation](#installation)\n- [Configuration](#configuration)\n    - [Driver Prerequisites](#driver-prerequisites)\n- [Cache Usage](#cache-usage)\n    - [Obtaining A Cache Instance](#obtaining-a-cache-instance)\n    - [Retrieving Items From The Cache](#retrieving-items-from-the-cache)\n    - [Storing Items In The Cache](#storing-items-in-the-cache)\n    - [Removing Items From The Cache](#removing-items-from-the-cache)\n- [Cache Tags](#cache-tags)\n    - [Storing Tagged Cache Items](#storing-tagged-cache-items)\n    - [Accessing Tagged Cache Items](#accessing-tagged-cache-items)\n    - [Removing Tagged Cache Items](#removing-tagged-cache-items)\n- [Events](#events)\n\n\u003ca name=\"installation\"\u003e\u003c/a\u003e\n## Installation\n\n```js\nnpm i adonis-cache --save\n```\n\nAfter installation, you need to register the providers inside `start/app.js` file.\n\n##### start/app.js\n```javascript\nconst providers = [\n  ...,\n  'adonis-cache/providers/CacheProvider'\n]\n```\n\nAlso, for registering commands.\n\n##### start/app.js\n```javascript\nconst aceProviders = [\n  ...,\n  'adonis-cache/providers/CommandsProvider'\n]\n```\n\nAlso, it is a good practice to setup an alias to avoid typing the complete namespace.\n\n##### start/app.js\n```javascript\nconst aliases = {\n  ...,\n  Cache: 'Adonis/Addons/Cache'\n}\n```\n\nThen, for generating a config file.\n```bash\nadonis cache:config\n```\n\n\u003ca name=\"configuration\"\u003e\u003c/a\u003e\n## Configuration\n\nAdonisCache provides an expressive, unified API for various caching backends. The cache configuration is located at `config/cache.js`. In this file you may specify which cache driver you would like used by default throughout your application. AdonisCache supports popular caching backends like [Redis](http://redis.io) out of the box.\n\nThe cache configuration file also contains various other options, which are documented within the file, so make sure to read over these options. By default, AdonisCache is configured to use the `object` cache driver, which stores cached objects in plain JavaScript object (use only for development). For larger applications, it is recommended that you use a more robust driver such as Redis. You may even configure multiple cache configurations for the same driver.\n\n\u003ca name=\"driver-prerequisites\"\u003e\u003c/a\u003e\n### Driver Prerequisites\n\n#### Database\n\nWhen using the `database` cache driver, you will need to setup a table to contain the cache items. You'll find an example `Schema` declaration for the table below:\n```javascript\nthis.create('cache', (table) =\u003e {\n  table.string('key').unique()\n  table.text('value')\n  table.integer('expiration')\n})\n```\n\n\u003e {tip} You may also use the `adonis cache:table` Ace command to generate a migration with the proper schema.\n\n#### Redis\n\nBefore using a Redis cache, you will need to have the Redis provider installed.\n\nFor more information on configuring Redis, consult its [AdonisJs documentation page](http://adonisjs.com/docs/redis).\n\n\u003ca name=\"cache-usage\"\u003e\u003c/a\u003e\n## Cache Usage\n\n\u003ca name=\"obtaining-a-cache-instance\"\u003e\u003c/a\u003e\n### Obtaining A Cache Instance\n\n```javascript\n'use strict'\n\nconst Cache = use('Cache')\n\nclass UserController {\n\n  async index(request, response) {\n    const value = await Cache.get('key')\n\n    //\n  }\n}\n```\n\n#### Accessing Multiple Cache Stores\n\nYou may access various cache stores via the `store` method. The key passed to the `store` method should correspond to one of the stores listed in the `stores` configuration object in your `cache` configuration file:\n\n```javascript\nvalue = await Cache.store('database').get('foo')\n\nawait Cache.store('redis').put('bar', 'baz', 10)\n```\n\u003ca name=\"retrieving-items-from-the-cache\"\u003e\u003c/a\u003e\n### Retrieving Items From The Cache\n\nThe `get` method is used to retrieve items from the cache. If the item does not exist in the cache, `null` will be returned. If you wish, you may pass a second argument to the `get` method specifying the default value you wish to be returned if the item doesn't exist:\n\n```javascript\nvalue = await Cache.get('key')\n\nvalue = await Cache.get('key', 'default')\n```\n\nYou may even pass a `Closure` as the default value. The result of the `Closure` will be returned if the specified item does not exist in the cache. Passing a Closure allows you to defer the retrieval of default values from a database or other external service:\n\n```javascript\nvalue = await Cache.get('key', async () =\u003e {\n  return await Database.table(...).where(...).first()\n})\n```\n\nRetrieving multiple items:\n\n```javascript\nvalues = await Cache.many(['key1', 'key2', 'key3'])\n//  values = {\n//    key1: value,\n//    key2: value,\n//    key3: value\n//  }\n```\n\n#### Checking For Item Existence\n\nThe `has` method may be used to determine if an item exists in the cache:\n\n```javascript\nif (await Cache.has('key')) {\n  //\n}\n```\n\n#### Incrementing / Decrementing Values\n\nThe `increment` and `decrement` methods may be used to adjust the value of integer items in the cache. Both of these methods accept an optional second argument indicating the amount by which to increment or decrement the item's value:\n\n```javascript\nawait Cache.increment('key')\nawait Cache.increment('key', amount)\nawait Cache.decrement('key')\nawait Cache.decrement('key', amount)\n```\n\n#### Retrieve \u0026 Store\n\nSometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. For example, you may wish to retrieve all users from the cache or, if they don't exist, retrieve them from the database and add them to the cache. You may do this using the `Cache.remember` method:\n\n```javascript\nvalue = await Cache.remember('key', minutes, async () =\u003e {\n  return await Database.table(...).where(...).first()\n})\n```\n\nIf the item does not exist in the cache, the `Closure` passed to the `remember` method will be executed and its result will be placed in the cache.\n\n#### Retrieve \u0026 Delete\n\nIf you need to retrieve an item from the cache and then delete the item, you may use the `pull` method. Like the `get` method, `null` will be returned if the item does not exist in the cache:\n\n```javascript\nvalue = await Cache.pull('key')\n```\n\n\u003ca name=\"storing-items-in-the-cache\"\u003e\u003c/a\u003e\n### Storing Items In The Cache\n\nYou may use the `put` method on the `Cache` to store items in the cache. When you place an item in the cache, you need to specify the number of minutes for which the value should be cached:\n\n```javascript\nawait Cache.put('key', 'value', minutes)\n```\n\nInstead of passing the number of minutes as an integer, you may also pass a `Date` instance representing the expiration time of the cached item:\n\n```javascript\nconst expiresAt = new Date(2016, 11, 1, 12, 0)\n\nawait Cache.put('key', 'value', expiresAt)\n```\n\nStoring multiple items:\n\n```javascript\nconst items = {\n  key1: 'value1',\n  key2: 'value2',\n  key3: 'value3'\n}\n\nawait Cache.putMany(items, minutes)\n```\n\n#### Store If Not Present\n\nThe `add` method will only add the item to the cache if it does not already exist in the cache store. The method will return `true` if the item is actually added to the cache. Otherwise, the method will return `false`:\n\n```javascript\nawait Cache.add('key', 'value', minutes)\n```\n\n#### Storing Items Forever\n\nThe `forever` method may be used to store an item in the cache permanently. Since these items will not expire, they must be manually removed from the cache using the `forget` method:\n\n```javascript\nawait Cache.forever('key', 'value')\n```\n\n\u003ca name=\"removing-items-from-the-cache\"\u003e\u003c/a\u003e\n### Removing Items From The Cache\n\nYou may remove items from the cache using the `forget` method:\n\n```javascript\nawait Cache.forget('key')\n```\n\nYou may clear the entire cache using the `flush` method:\n\n```javascript\nawait Cache.flush()\n```\n\n\u003e {note} Flushing the cache does not respect the cache prefix and will remove all entries from the cache. Consider this carefully when clearing a cache which is shared by other applications.\n\n\u003ca name=\"cache-tags\"\u003e\u003c/a\u003e\n## Cache Tags\n\n\u003e {note} Cache tags are not supported when using the `database` cache driver.\n\n\u003ca name=\"storing-tagged-cache-items\"\u003e\u003c/a\u003e\n### Storing Tagged Cache Items\n\nCache tags allow you to tag related items in the cache and then flush all cached values that have been assigned a given tag. You may access a tagged cache by passing in an ordered array of tag names. For example, let's access a tagged cache and `put` value in the cache:\n\n```javascript\nawait Cache.tags(['people', 'artists']).put('John', john, minutes)\n\nawait Cache.tags(['people', 'authors']).put('Anne', anne, minutes)\n```\n\n\u003ca name=\"accessing-tagged-cache-items\"\u003e\u003c/a\u003e\n### Accessing Tagged Cache Items\n\nTo retrieve a tagged cache item, pass the same ordered list of tags to the `tags` method and then call the `get` method with the key you wish to retrieve:\n\n```javascript\nconst john = await Cache.tags(['people', 'artists']).get('John')\n\nconst anne = await Cache.tags(['people', 'authors']).get('Anne')\n```\n\n\u003ca name=\"removing-tagged-cache-items\"\u003e\u003c/a\u003e\n### Removing Tagged Cache Items\n\nYou may flush all items that are assigned a tag or list of tags. For example, this statement would remove all caches tagged with either `people`, `authors`, or both. So, both `Anne` and `John` would be removed from the cache:\n\n```javascript\nawait Cache.tags(['people', 'authors']).flush()\n```\n\nIn contrast, this statement would remove only caches tagged with `authors`, so `Anne` would be removed, but not `John`:\n\n```javascript\nawait Cache.tags('authors').flush()\n```\n\n\u003ca name=\"events\"\u003e\u003c/a\u003e\n## Events\n\nTo execute code on every cache operation, you may listen for the [events](http://adonisjs.com/docs/events) fired by the cache. Typically, you should place these event listeners within your `start/events.js`:\n\n```\nCache.hit\nCache.missed\nCache.keyForgotten\nCache.keyWritten\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelnokaly%2Fadonis-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelnokaly%2Fadonis-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelnokaly%2Fadonis-cache/lists"}