Cache - JavaScript - AWS Amplify Gen 1 Documentation (original) (raw)

The Amplify Cache module provides a generic LRU cache for JavaScript developers to store data with priority and expiration settings.

It is a key/value structure where expiration values can be configured globally or on a per-key basis. For instance, you may wish to cache all JSON responses from the API module for the next 10 minutes, but like to store user input values or preferences for a month.

Installation

Install aws-amplify.

Working with the API

First, import the library:

After the import, you can invoke the appropriate methods within your application.

setItem()

You can set number, string, boolean, array or object values to the cache. You can also specify options along with the call such as the priority or expiration time.

When using priority setting, the cached item with the higher number will be expired first. The Cache module decides expiration based on the memory available to the cache. In the following example,breakfastFoodOrder will be expired before mothersBirthday.

getItem()

Retrieves an item from the cache. It will return null if the item doesn’t exist or it has expired.

removeItem()

Removes item from cache.

clear()

Removes all of the items in the cache.

getAllKeys()

Returns all of the keys available in the cache.

getCacheCurSize()

Returns the current size of the cache in bytes.

configure()

Configures default settings for setItem functionality. You can see all available options in Configuration section.

createInstance()

Creates a new instance of Cache with custom configuration.

You can keep default values and only override the values you need as well.

API Reference

For the complete API documentation for Cache module, visit our API Reference

Configuration

Configuration Parameters

Here is the list of configuration parameters for the Cache module:

Configuration Parameters for Items

Here is the list of configuration parameters for the items in the cache :