UNPKG

leancloud-storage

Version:
39 lines (34 loc) 976 B
'use strict'; var storage = require('./localstorage'); var AV = require('./av'); var removeAsync = exports.removeAsync = storage.removeItemAsync.bind(storage); var getCacheData = function getCacheData(cacheData, key) { try { cacheData = JSON.parse(cacheData); } catch (e) { return null; } if (cacheData) { var expired = cacheData.expiredAt && cacheData.expiredAt < Date.now(); if (!expired) { return cacheData.value; } return removeAsync(key).then(function () { return null; }); } return null; }; exports.getAsync = function (key) { key = AV.applicationId + '/' + key; return storage.getItemAsync(key).then(function (cache) { return getCacheData(cache, key); }); }; exports.setAsync = function (key, value, ttl) { var cache = { value: value }; if (typeof ttl === 'number') { cache.expiredAt = Date.now() + ttl; } return storage.setItemAsync(AV.applicationId + '/' + key, JSON.stringify(cache)); };