How to use cacheName method in wpt

Best JavaScript code snippet using wpt

cache-helper.js

Source:cache-helper.js Github

copy

Full Screen

1/**2 * cache helper3 */4import settings from './settings';5import DataCache from './DataCache';6// 缓存数据对象。为了避免混淆,只缓存至一级结构7const dataCache = new DataCache();8// 获取时间戳9function getTime(t) {10 return t ? t.getTime() : (new Date()).getTime();11}12/**13 * 修正 cacheName14 * @param {String} cacheName 原始的值,可能是任意格式15 * @return {String} 修正后的 cacheName,以 cachePrefix 开头16 */17function adjustCacheName(cacheName) {18 if (!cacheName) {19 return '';20 }21 cacheName = encodeURIComponent(('' + cacheName).replace(/\//g, '.').replace(/^\./, '').replace(/(^\s+|\s+$)/g, ''));22 if (cacheName.indexOf(settings.cachePrefix)) {23 // cacheName.indexOf(settings.cachePrefix) !== 0 加上前缀24 cacheName = settings.cachePrefix + cacheName;25 }26 return cacheName;27}28/**29 * 根据 cacheType 取得 cacheStorage 对象30 * @param {String} cacheType31 * @return {Object}32 */33export function getCacheStor(cacheType) {34 let cacheStor = dataCache;35 if (~['sessionStorage', 'localStorage'].indexOf(cacheType)) {36 cacheStor = window[cacheType] || cacheStor;37 }38 return cacheStor;39}40/**41 * 根据 cacheName 名称层级获取对应 dataCache 中的缓存数据42 * @param {String} cacheName - 名称,以 . 分割层级,如 ups.pa.query.tags.group43 * @param {String} cacheType - 缓存类型:sessionStorage、localStorage 、 memory(默认)44 * @return {*} 返回读取到的数据45 */46export function getCacheDataByName(cacheName, cacheType) {47 let data;48 const undefinedVal = void 0;49 const cacheStor = getCacheStor(cacheType);50 if (!(cacheName = adjustCacheName(cacheName))) {51 return data;52 }53 data = cacheStor.getItem(cacheName);54 try {55 data = JSON.parse(data);56 } catch (e) {57 data = data;58 }59 // 缓存的数据设置了有效期 data._e60 if (data && data._e) {61 // console.log(getTime() - data._e, getTime(), data._e);62 if (getTime() - data._e < 0) {63 return data.d;64 }65 // 已过期,数据无效了,移除它66 cacheStor.removeItem(cacheName);67 return undefinedVal;68 }69 return data || undefinedVal;70}71/**72 * 根据 cacheName 名称尝试移除缓存中存在的数据73 * @param {String|RegExp} cacheName - 名称,以 . 分割层级,如 ups.pa.query.tags.group。支持正则匹配74 * @param {String} cacheType - 缓存类型:sessionStorage、localStorage 、 memory(默认)75 * @return {*}76 */77export function deleteCacheDataByName(cacheName, cacheType) {78 const cacheStor = getCacheStor(cacheType);79 let item,80 i,81 len;82 // 为正则,支持模糊删除83 if (cacheName instanceof RegExp) {84 len = cacheStor.length;85 for (i = 0; i < len; i++) {86 item = cacheStor.key(i);87 if (88 !item || // 兼容89 item.indexOf(settings.cachePrefix) !== 0 || // 过滤前缀90 !cacheName.test(item.slice(settings.cachePrefix.length)) // 规则检测91 ) {92 continue;93 }94 // 符合规则,移除95 cacheStor.removeItem(item);96 }97 return;98 }99 // 精确的查找与删除100 if (!(cacheName = adjustCacheName(cacheName))) {101 return;102 }103 cacheStor.removeItem(cacheName);104}105/**106 * 存储数据到本地107 * @param {String} cacheName - 用于存储的名称108 * @param {*} data - 任意类型的数据109 * @param {String} cacheType - 存储类型,支持三种方式:sessionStorage、localStorage 和内存中(默认)110 */111export function saveTOCache(cacheName, data, cfg = {}) {112 if (!(cacheName = adjustCacheName(cacheName))) {113 return;114 }115 // console.log(cacheName, data, cfg);116 const {cache: cacheType, expires} = cfg;117 const cacheStor = getCacheStor(cacheType);118 // expires 应为毫秒整数119 if (+expires) {120 data = {121 d: data,122 _e: (expires instanceof Date) ? getTime(expires) : (getTime() + expires)123 };124 }125 if (cacheStor === dataCache) {126 // 存到内存 dataCache127 cacheStor.setItem(cacheName, data);128 } else {129 cacheStor.setItem(cacheName, JSON.stringify(data));130 }131}132/**133 * 是否为类字符串134 */135export function isString(text) {136 const type = typeof text;137 return 'string' === type || 'number' === type;138}139/**140 * 返回包装done/fail API语法糖的 Promise141 * @param {Boolean} isJquery 是否为 jQuery,为true 则返回 $.Deferred142 * @return {Promise}143 */144export function getPromise(isJquery) {145 if (isJquery) {146 return $.Deferred();147 }148 let resolve, reject;149 const $p = new window.Promise((rs, rj) => {150 resolve = rs;151 reject = rj;152 });153 $p.resolve = resolve;154 $p.reject = reject;155 $p.done = function (cb) {156 return $p.then(cb);157 };158 $p.fail = function (cb) {159 return $p.then(null, cb);160 };161 $p.always = function (cb) {162 return $p.then(cb, cb);163 };164 return $p;...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1'use strict';2var through = require('through2'),3 util = require('gulp-util'),4 pluginName = 'gulp-remember', // name of our plugin for error logging purposes5 caches = {}, // will hold named file caches6 defaultName = '_default'; // name to give a cache if not provided7/**8 * Return a through stream that will:9 * 1. Remember all files that ever pass through it.10 * 2. Add all remembered files back into the stream when not present.11 * @param cacheName {string} Name to give your cache.12 * Caches with different names can know about different sets of files.13 */14function gulpRemember(cacheName) {15 var cache; // the files we've ever put our hands on in the current stream16 if (cacheName !== undefined && typeof cacheName !== 'number' && typeof cacheName !== 'string') {17 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember")(name); where name is undefined, number or string');18 }19 cacheName = cacheName || defaultName; // maybe need to use a default cache20 caches[cacheName] = caches[cacheName] || {}; // maybe initialize the named cache21 cache = caches[cacheName];22 function transform(file, enc, callback) {23 var fileKey = file.path.toLowerCase();24 cache[fileKey] = file; // add file to our cache25 callback();26 }27 function flush(callback) {28 // add all files we've ever seen back into the stream29 for (var key in cache) {30 if (cache.hasOwnProperty(key)) {31 this.push(cache[key]); // add this file back into the current stream32 }33 }34 callback();35 }36 return through.obj(transform, flush);37}38/**39 * Forget about a file.40 * A warning is logged if either the named cache or file do not exist.41 *42 * @param cacheName {string} name of the cache from which to drop the file43 * @param path {string} path of the file to forget44 */45gulpRemember.forget = function (cacheName, path) {46 if (arguments.length === 1) {47 path = cacheName;48 cacheName = defaultName;49 }50 path = path.toLowerCase();51 if (typeof cacheName !== 'number' && typeof cacheName !== 'string') {52 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember").forget(cacheName, path); where cacheName is undefined, number or string and path is a string');53 }54 if (caches[cacheName] === undefined) {55 util.log(pluginName, '- .forget() warning: cache ' + cacheName + ' not found');56 } else if (caches[cacheName][path] === undefined) {57 util.log(pluginName, '- .forget() warning: file ' + path + ' not found in cache ' + cacheName);58 } else {59 delete caches[cacheName][path];60 }61};62/**63 * Forget all files in one cache.64 * A warning is logged if the cache does not exist.65 *66 * @param cacheName {string} name of the cache to wipe67 */68gulpRemember.forgetAll = function (cacheName) {69 if (arguments.length === 0) {70 cacheName = defaultName;71 }72 if (typeof cacheName !== 'number' && typeof cacheName !== 'string') {73 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember").forgetAll(cacheName); where cacheName is undefined, number or string');74 }75 if (caches[cacheName] === undefined) {76 util.log(pluginName, '- .forget() warning: cache ' + cacheName + ' not found');77 } else {78 caches[cacheName] = {};79 }80}81/**82 * Return a raw cache by name.83 * Useful for checking state. Manually adding or removing files is NOT recommended.84 *85 * @param cacheName {string} name of the cache to retrieve86 */87gulpRemember.cacheFor = function (cacheName) {88 if (arguments.length === 0) {89 cacheName = defaultName;90 }91 if (typeof cacheName !== 'number' && typeof cacheName !== 'string') {92 throw new util.PluginError(pluginName, 'Usage: require("gulp-remember").cacheFor(cacheName); where cacheName is undefined, number or string');93 }94 return caches[cacheName];95}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1self.addEventListener('fetch', function(event) {2 event.respondWith(3 caches.open('test-cache').then(function(cache) {4 return cache.match(event.request).then(function(response) {5 return response || fetch(event.request);6 });7 })8 );9});

Full Screen

Using AI Code Generation

copy

Full Screen

1var cacheName = __wptBrowsertime.cacheName;2var cache = caches.open(cacheName);3cache.then(function(cache) {4 return response.text();5 }).then(function(text) {6 console.log('cacheName: ' + text);7 });8});9var cacheName = __wptBrowsertime.cacheName;10var cache = caches.open(cacheName);11cache.then(function(cache) {12 return response.text();13 }).then(function(text) {14 console.log('cacheName: ' + text);15 });16});17var cacheName = __wptBrowsertime.cacheName;18var cache = caches.open(cacheName);19cache.then(function(cache) {20 cache.matchAll().then(function(responses) {21 responses.forEach(function(response) {22 response.text().then(function(text) {23 console.log('cacheName: ' + text);24 });25 });26 });27});28var cacheName = __wptBrowsertime.cacheName;29var cache = caches.open(cacheName);30cache.then(function(cache) {31 console.log('cacheName: ' + response);32 });33});34var cacheName = __wptBrowsertime.cacheName;35var cache = caches.open(cacheName);36cache.then(function(cache) {37 cache.keys().then(function(requests) {38 requests.forEach(function(request) {39 console.log('cacheName: ' + request.url);40 });41 });42});43var cacheName = __wptBrowsertime.cacheName;44var cache = caches.open(cacheName);45cache.then(function(cache) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var cacheName = wpt.cacheName;2var cache = caches.open(cacheName);3cache.then(function(cache) {4 cache.addAll([5 ]).then(function() {6 console.log('All resources have been fetched and cached.');7 });8});9var cacheName = wpt.cacheName;10var cache = caches.open(cacheName);11cache.then(function(cache) {12 cache.addAll([13 ]).then(function() {14 console.log('All resources have been fetched and cached.');15 });16});17var cacheName = wpt.cacheName;18var cache = caches.open(cacheName);19cache.then(function(cache) {20 cache.addAll([21 ]).then(function() {22 console.log('All resources have been fetched and cached.');23 });24});25var cacheName = wpt.cacheName;26var cache = caches.open(cacheName);27cache.then(function(cache) {28 cache.addAll([29 ]).then(function() {30 console.log('All resources have been fetched and cached.');31 });32});33var cacheName = wpt.cacheName;34var cache = caches.open(cacheName);35cache.then(function(cache) {36 cache.addAll([37 ]).then(function() {38 console.log('All resources have been fetched and cached.');39 });40});

Full Screen

Using AI Code Generation

copy

Full Screen

1var cacheName = caches.open('cyCache').then(function(cachhe {2 /);3 cacheName.then(function(name) {4 console.log(name/var cacheName = wpt.cacheName;5var cacheName = wpt.cacheName;6var cache = caches.open(cacheName);7cache.then(function(cache) {8 cache.addAll([9 ]).then(function() {10 console.log('All resources have been fetched and cached.');11 });12});13var cacheName = wpt.cacheName;14var cache = caches.open(cacheName);15cache.then(function(cache) {16 cache.addAll([17 ]).then(function() {18 console.log('All resources have been fetched and cached.');19 });20});21var cacheName = wpt.cacheName;22var cache = caches.open(cacheName);23cache.then(function(cache) {24 cache.addAll([25 ]).then(function() {26 console.log('All resources have been fetched and cached.');27 });28});29var cacheName = wpt.cacheName;30var cache = caches.open(cacheName);31cache.then(function(cache) {32 cache.addAll([33 ]).then(function() {34 console.log('All resources have been fetched and cached.');35 });36});37var cacheName = wpt.cacheName;38var cache = caches.open(cacheName);39cache.then(function(cache) {40 cache.addAll([41 ]).then(function() {42 console.log('All resources have been fetched and cached.');43 });44});

Full Screen

Using AI Code Generation

copy

Full Screen

1var cacheName = caches.open('myCache').then(function(cache) {2 return cache.name;3});4cacheName.then(function(name) {5 console.log(name);6});

Full Screen

Using AI Code Generation

copy

Full Screen

1var cacheName = caches.open('myCache').then(function(cache) {2 return cache.name;3 });4 cacheName.then(function(name) {5 console.log(name);6 });

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful