react-native-cache-data

1.0.0 • Public • Published

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
});

Readme

Keywords

Package Sidebar

Install

npm i react-native-cache-data

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

5.18 kB

Total Files

4

Last publish

Collaborators

  • anvilapp