house-middleware-sdk
Version:
58 hosue rn native sdk
81 lines (71 loc) • 1.82 kB
JavaScript
import {
AsyncStorage
} from 'react-native';
import DeviceStorage from './DeviceStorage';
import { encryptionMD5 } from './md5Util';
/**
* 缓存类
*
* @author haojie
* @version 2019-09-27
*/
const CITY_ID ='cityId';
export default class CacheUtil {
static useCache = false;
/**
* 全局设置是否使用缓存
* @param flag
*/
static setUseCache(flag) {
console.log(`----设置是否使用缓存----${flag}`);
this.useCache = flag;
}
/**
* 是否改变cityid
* @param cityId
* @returns {Promise<boolean>}
*/
static hasChangeCity = async(cityId) => {
const mCityId = await DeviceStorage.get(CITY_ID);
console.log('cityId:' + cityId + '---oldCityId:' + mCityId);
return mCityId !== cityId;
};
/**
* 检测cityId 变了清除数据
* @param cityId
* @returns {Promise<void>}
*/
static checkCityId = async (cityId) => {
const flag = await this.hasChangeCity(cityId);
console.log(`----检测城市是否改变----${flag}`);
if (flag) {
console.log('----城市切换清除缓存----');
DeviceStorage.clear();
DeviceStorage.save('cityId', cityId);
}
};
/**
* 存储数据
* @param cacheKey
* @param value
*/
static saveCache = (cacheKey, value) => {
const key = encryptionMD5(cacheKey);
console.log('----存储缓存加密缓存key----' + key);
DeviceStorage.saveDataForJson(key, value);
};
/**
* 读取缓存
* @param cacheKey
* @returns {Promise<*>}
*/
static loadCache = async (cacheKey) => {
if (!this.useCache) {
return null;
}
console.log('----原始缓存key----' + cacheKey);
const key = encryptionMD5(cacheKey);
console.log('----加密缓存key----' + key);
return await DeviceStorage.getDataforJson(key);
};
}