react-native-cache-data
v1.0.0
Published
```text npm install react-native-cache-data --save ``` or ```text yarn add react-native-cache-data ```
Downloads
1
Readme
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
});