[React Native Synchronous acquisition of a local cache key

  Prior wrote an asynchronous acquired, but can not meet the development demand synchronous read. Links: https://www.cnblogs.com/xjf125/p/10456720.html

  Today encapsulates a synchronization acquiring local pairs.

import React from "react";
import {
    AsyncStorage
} from 'react-native';
export default class SyncStorage {

    static cache: { [key: string]: string } = {}


    static async init() {
        let keys = await AsyncStorage.getAllKeys()
        let items = await AsyncStorage.multiGet(keys).then()
        items.map(([key, value]) => {
            this.cache[key] = value
        })
    }

    static getValue(key: string) {
        return this.cache[key]
    }

    static setValue(key: string, value: string) {
        if (this.cache[key] === value) return
        this.cache[key] = value
        AsyncStorage.setItem(key, value)
    }

    static removeKey(key: string) {
        delete this.cache[key]
        AsyncStorage.removeItem(key)
    }
}

  use:

SyncStorage.setValue(key,digest);  //写入

lastText = SyncStorage.getValue(key); //读取

 

Guess you like

Origin www.cnblogs.com/xjf125/p/12324409.html