Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage 封装 #17

Open
rccoder opened this issue May 11, 2017 · 0 comments
Open

Storage 封装 #17

rccoder opened this issue May 11, 2017 · 0 comments

Comments

@rccoder
Copy link
Member

rccoder commented May 11, 2017

让其和 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'
        })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant