cache search results for 1 hour
This commit is contained in:
parent
780053a19e
commit
abc4923059
|
@ -210,7 +210,7 @@ window.addEventListener('showStyles:done', function _() {
|
||||||
if (searchResults.data.length === 0) {
|
if (searchResults.data.length === 0) {
|
||||||
throw 404;
|
throw 404;
|
||||||
}
|
}
|
||||||
unprocessedResults.push.apply(unprocessedResults, searchResults.data);
|
unprocessedResults.push(unprocessedResults, ...searchResults.data);
|
||||||
processNextResult();
|
processNextResult();
|
||||||
})
|
})
|
||||||
.catch(error);
|
.catch(error);
|
||||||
|
@ -288,8 +288,9 @@ window.addEventListener('showStyles:done', function _() {
|
||||||
id: ENTRY_ID_PREFIX + result.id,
|
id: ENTRY_ID_PREFIX + result.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const displayedName = result.name.length < 300 ? result.name : result.name.slice(0, 300) + '...';
|
||||||
Object.assign($('.search-result-title', entry), {
|
Object.assign($('.search-result-title', entry), {
|
||||||
textContent: result.name,
|
textContent: tWordBreak(displayedName),
|
||||||
onclick: handleEvent.openURLandHide,
|
onclick: handleEvent.openURLandHide,
|
||||||
href: searchAPI.BASE_URL + result.url
|
href: searchAPI.BASE_URL + result.url
|
||||||
});
|
});
|
||||||
|
@ -305,7 +306,7 @@ window.addEventListener('showStyles:done', function _() {
|
||||||
screenshot.src = screenshotUrl;
|
screenshot.src = screenshotUrl;
|
||||||
|
|
||||||
const description = result.description
|
const description = result.description
|
||||||
.replace(/<.*?>/g, '')
|
.replace(/<[^>]*>/g, '')
|
||||||
.replace(/[\r\n]{3,}/g, '\n\n');
|
.replace(/[\r\n]{3,}/g, '\n\n');
|
||||||
Object.assign($('.search-result-description', entry), {
|
Object.assign($('.search-result-description', entry), {
|
||||||
textContent: description,
|
textContent: description,
|
||||||
|
@ -435,6 +436,8 @@ window.addEventListener('showStyles:done', function _() {
|
||||||
*/
|
*/
|
||||||
function searchUserstyles() {
|
function searchUserstyles() {
|
||||||
const BASE_URL = 'https://userstyles.org';
|
const BASE_URL = 'https://userstyles.org';
|
||||||
|
const CACHE_PREFIX = 'usoSearchCache';
|
||||||
|
const CACHE_DURATION = 1 * 3600e3;
|
||||||
let totalPages;
|
let totalPages;
|
||||||
let currentPage = 1;
|
let currentPage = 1;
|
||||||
let exhausted = false;
|
let exhausted = false;
|
||||||
|
@ -511,18 +514,17 @@ function searchUserstyles() {
|
||||||
* @returns {Promise<Object>} An object containing info about the style, e.g. name, author, etc.
|
* @returns {Promise<Object>} An object containing info about the style, e.g. name, author, etc.
|
||||||
*/
|
*/
|
||||||
function fetchStyle(userstylesId) {
|
function fetchStyle(userstylesId) {
|
||||||
return new Promise((resolve, reject) => {
|
return readCache(userstylesId)
|
||||||
|
.then(json => json ||
|
||||||
download(BASE_URL + '/api/v1/styles/' + userstylesId, {
|
download(BASE_URL + '/api/v1/styles/' + userstylesId, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-type': 'application/json',
|
'Content-type': 'application/json',
|
||||||
'Accept': '*/*'
|
'Accept': '*/*'
|
||||||
},
|
},
|
||||||
|
responseType: 'json',
|
||||||
body: null
|
body: null
|
||||||
}).then(responseText => {
|
}).then(writeCache));
|
||||||
resolve(tryJSONparse(responseText));
|
|
||||||
}).catch(reject);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -532,9 +534,8 @@ function searchUserstyles() {
|
||||||
* @return {Object} Response object from userstyles.org
|
* @return {Object} Response object from userstyles.org
|
||||||
*/
|
*/
|
||||||
function search(category) {
|
function search(category) {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (totalPages !== undefined && currentPage > totalPages) {
|
if (totalPages !== undefined && currentPage > totalPages) {
|
||||||
resolve({'data':[]});
|
return Promise.resolve({'data':[]});
|
||||||
}
|
}
|
||||||
|
|
||||||
const searchURL = BASE_URL +
|
const searchURL = BASE_URL +
|
||||||
|
@ -543,23 +544,73 @@ function searchUserstyles() {
|
||||||
'&page=' + currentPage +
|
'&page=' + currentPage +
|
||||||
'&country=NA';
|
'&country=NA';
|
||||||
|
|
||||||
|
const cacheKey = category + '/' + currentPage;
|
||||||
|
|
||||||
|
return readCache(cacheKey)
|
||||||
|
.then(json => json ||
|
||||||
download(searchURL, {
|
download(searchURL, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-type': 'application/json',
|
'Content-type': 'application/json',
|
||||||
'Accept': '*/*'
|
'Accept': '*/*'
|
||||||
},
|
},
|
||||||
|
responseType: 'json',
|
||||||
body: null
|
body: null
|
||||||
}).then(responseText => {
|
}).then(json => {
|
||||||
const responseJson = tryJSONparse(responseText);
|
json.id = cacheKey;
|
||||||
currentPage = responseJson.current_page + 1;
|
writeCache(json);
|
||||||
totalPages = responseJson.total_pages;
|
return json;
|
||||||
|
}))
|
||||||
|
.then(json => {
|
||||||
|
currentPage = json.current_page + 1;
|
||||||
|
totalPages = json.total_pages;
|
||||||
exhausted = (currentPage > totalPages);
|
exhausted = (currentPage > totalPages);
|
||||||
resolve(responseJson);
|
return json;
|
||||||
}).catch(reason => {
|
}).catch(reason => {
|
||||||
exhausted = true;
|
exhausted = true;
|
||||||
reject(reason);
|
return Promise.reject(reason);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function readCache(id) {
|
||||||
|
return BG.chromeLocal.getLZValue(CACHE_PREFIX + id).then(data => {
|
||||||
|
if (!data || Date.now() - data.cacheWriteDate < CACHE_DURATION) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
BG.chromeLocal.remove(CACHE_PREFIX + id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeCache(data) {
|
||||||
|
debounce(cleanupCache, 10e3);
|
||||||
|
data.cacheWriteDate = Date.now();
|
||||||
|
return BG.chromeLocal.setLZValue(CACHE_PREFIX + data.id, data)
|
||||||
|
.then(() => data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanupCache() {
|
||||||
|
new Promise(resolve =>
|
||||||
|
chrome.storage.local.getBytesInUse &&
|
||||||
|
chrome.storage.local.getBytesInUse(null, resolve) ||
|
||||||
|
1e9
|
||||||
|
)
|
||||||
|
.then(size => size > 1e6 || Promise.reject())
|
||||||
|
.then(() => BG.chromeLocal.getValue(CACHE_PREFIX + 'Cleanup'))
|
||||||
|
.then((lastCleanup = 0) =>
|
||||||
|
Date.now() - lastCleanup > CACHE_DURATION &&
|
||||||
|
chrome.storage.local.get(null, storage => {
|
||||||
|
const expired = [];
|
||||||
|
for (const key in storage) {
|
||||||
|
if (key.startsWith(CACHE_PREFIX) &&
|
||||||
|
Date.now() - storage[key].cacheWriteDate > CACHE_DURATION) {
|
||||||
|
expired.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (expired.length) {
|
||||||
|
chrome.storage.local.remove(expired);
|
||||||
|
}
|
||||||
|
BG.chromeLocal.setValue(CACHE_PREFIX + 'Cleanup', Date.now());
|
||||||
|
}))
|
||||||
|
.catch(ignoreChromeError);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user