1.0.0 • Published 5 years ago

react-native-cache-data v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Install

npm install react-native-cache-data --save

or

yarn add react-native-cache-data

Install AsyncStorage

https://github.com/react-native-community/async-storage

Usage example

import cache from 'react-native-cache-data';

cache.set('test', 'simple value').then(() => {
    console.log('saved');
});

cache.get('test').then(result => {
    console.log(result);
});

cache.remove('test').then(() => {
    console.log('removed');
});

With middleware

import cache, {expire} from 'react-native-cache-data';

const data = {
    key1: 'value1',
    key2: 'value2',
};

cache.set('test', data, [JSON.stringify]).then(() => {
  console.log('saved');
});

cache.get('test', [expire(60), JSON.parse]).then(result => {
    console.log(result);
});

Custom middleware

import cache from 'react-native-cache-data';

const filterRedValueMiddleware = (value, date) => {
    if (value && value !== 'red') {
        return value;
    }
    return null;
};

cache.set('first', 'yellow').then(() => {
    console.log('saved');
});

cache.set('second', 'red').then(() => {
    console.log('saved');
});

cache.get('first', [filterRedValueMiddleware]).then(result => {
    console.log(result); // yellow
});

cache.get('second', [filterRedValueMiddleware]).then(result => {
    console.log(result); // null
});