WeHIT / wiki

Small Record In WeHIT Development
0 stars 0 forks source link

Storage 封装 #17

Open rccoder opened 7 years ago

rccoder commented 7 years ago

让其和 LocalStorage 非常像

import Storage from 'react-native-storage';
import { AsyncStorage } from 'react-native';

const storage = new Storage({
  // maximum capacity, default 1000
  size: 1000,

  // Use AsyncStorage for RN, or window.localStorage for web.
  // If not set, data would be lost after reload.
  storageBackend: AsyncStorage,

  // expire time, default 1 day(1000 * 3600 * 24 milliseconds).
  // can be null, which means never expire.
  defaultExpires: 1000 * 3600 * 24,

  // cache data in the memory. default is true.
  enableCache: true,

  // if data was not found in storage or expired,
  // the corresponding sync method will be invoked and return
  // the latest data.
  sync : {
    // we'll talk about the details later.
  }
});

/**
 * @desc 保存,强key value
 * @param data
 */
const save = data => new Promise((resolve, reject) => {
  storage.save({
    key: data.key,
    rawData: data.value
  });
  resolve();
});

/**
 * @desc 读取,按照 key
 * @param key
 * @return {Promise.<*>}
 */
const load = key => new Promise((resolve, reject) => {
  storage.load({
    key,
  }).then(val => resolve(val));
});

export default {
  save,
  load,
};

调用

import { Storage } from '@base';
Storage.save({
          key: 'token',
          value: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InNoYWJpIiwiaWF0IjoxNDkxODI1Njg5fQ.FD1_u1gGMUni2_eohxx6w_CgyV9vf0bbVSZkD4CjPqo'
        })