UNPKG

@taro-hooks/use-request

Version:
40 lines (39 loc) 1.28 kB
"use strict"; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } Object.defineProperty(exports, "__esModule", { value: true }); exports.clearCache = exports.setCache = exports.getCache = void 0; var cache = new Map(); var setCache = function setCache(key, cacheTime, cachedData) { var currentCache = cache.get(key); if (currentCache != null && currentCache.timer) { clearTimeout(currentCache.timer); } var timer = undefined; if (cacheTime > -1) { // if cache out, clear it timer = setTimeout(function () { cache["delete"](key); }, cacheTime); } cache.set(key, _extends({}, cachedData, { timer: timer })); }; exports.setCache = setCache; var getCache = function getCache(key) { return cache.get(key); }; exports.getCache = getCache; var clearCache = function clearCache(key) { if (key) { var cacheKeys = Array.isArray(key) ? key : [key]; cacheKeys.forEach(function (cacheKey) { return cache["delete"](cacheKey); }); } else { cache.clear(); } }; exports.clearCache = clearCache;