Refactor: Clean up local caching code
This commit is contained in:
Родитель
71a5aa8aa0
Коммит
06d09b094b
|
@ -5,7 +5,7 @@ import ChangesetsViewerContainer from '../containers/summaryViewer';
|
||||||
import DiffViewerContainer from '../containers/diffViewer';
|
import DiffViewerContainer from '../containers/diffViewer';
|
||||||
import FileViewerContainer from '../containers/fileViewer';
|
import FileViewerContainer from '../containers/fileViewer';
|
||||||
import settings from '../settings';
|
import settings from '../settings';
|
||||||
import clearLocalCache from '../utils/localCache';
|
import { clearCache } from '../utils/localCache';
|
||||||
import '../style.css';
|
import '../style.css';
|
||||||
import AppDisclaimer from './disclaimer';
|
import AppDisclaimer from './disclaimer';
|
||||||
import GitHubRibbon from './githubRibbon';
|
import GitHubRibbon from './githubRibbon';
|
||||||
|
@ -42,7 +42,7 @@ export default () => (
|
||||||
<Route
|
<Route
|
||||||
path="/clear-cache"
|
path="/clear-cache"
|
||||||
render={() => {
|
render={() => {
|
||||||
if (clearLocalCache()) {
|
if (clearCache()) {
|
||||||
return (<p>The local database has been cleared.</p>);
|
return (<p>The local database has been cleared.</p>);
|
||||||
}
|
}
|
||||||
return (<p>Failed to clear the local DB.</p>);
|
return (<p>Failed to clear the local DB.</p>);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
export default {
|
const settings = {
|
||||||
BZ_URL: 'http://bugzilla.mozilla.org',
|
BZ_URL: 'http://bugzilla.mozilla.org',
|
||||||
CACHE_CONFIG: {
|
CACHE_CONFIG: {
|
||||||
SECONDS_TO_EXPIRE: 1 * 60 * 60, // 1 hour
|
SECONDS_TO_EXPIRE: 1 * 60 * 60, // 1 hour
|
||||||
|
@ -29,3 +29,11 @@ export default {
|
||||||
HG_DAYS_AGO: new Date((new Date()).getTime() - (2 * 24 * 60 * 60 * 1000))
|
HG_DAYS_AGO: new Date((new Date()).getTime() - (2 * 24 * 60 * 60 * 1000))
|
||||||
.toISOString().substring(0, 10),
|
.toISOString().substring(0, 10),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (settings.CACHE_CONFIG.ENABLED) {
|
||||||
|
console.debug('Local caching enabled');
|
||||||
|
} else {
|
||||||
|
console.debug('Local caching disabled');
|
||||||
|
}
|
||||||
|
|
||||||
|
export default settings;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as localForage from 'localforage';
|
|
||||||
import settings from '../settings';
|
import settings from '../settings';
|
||||||
import { JSON_HEADERS, PLAIN_HEADERS } from './fetch_data';
|
import { JSON_HEADERS, PLAIN_HEADERS } from './fetch_data';
|
||||||
|
import { getFromCache, saveInCache } from './localCache';
|
||||||
|
|
||||||
export const HG_HOST = 'https://hg.mozilla.org';
|
export const HG_HOST = 'https://hg.mozilla.org';
|
||||||
|
|
||||||
|
@ -75,64 +75,34 @@ const pushesToCsets = async (pushes, hidden) => {
|
||||||
return filteredCsets;
|
return filteredCsets;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getChangesets = async (repoName, hidden) => {
|
||||||
const isExpiredCache = (lastCaching) => {
|
|
||||||
const MSTOS = 1000;
|
|
||||||
const currentTime = (new Date()).getTime();
|
|
||||||
const secondsDifference = (currentTime - lastCaching) / MSTOS;
|
|
||||||
console.debug((currentTime - lastCaching) / MSTOS);
|
|
||||||
return (
|
|
||||||
(typeof lastCaching === 'number') &&
|
|
||||||
(secondsDifference > settings.CACHE_CONFIG.SECONDS_TO_EXPIRE))
|
|
||||||
|| false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getCsetsFromCache = async (repoName, hidden) => {
|
|
||||||
let csets = [];
|
let csets = [];
|
||||||
try {
|
if (settings.CACHE_CONFIG.ENABLED) {
|
||||||
const lastCaching = await localForage.getItem('cachedTime');
|
try {
|
||||||
const cachedCsets = await localForage.getItem('changesets');
|
csets = await getFromCache('changesets');
|
||||||
if (!isExpiredCache(lastCaching) && cachedCsets) {
|
} catch (e) {
|
||||||
console.debug(`Retrieved cached changesets. We have ${cachedCsets.length} changesets.`);
|
// We only log since we want to fetch the changesets from Hg
|
||||||
csets = cachedCsets;
|
console.error(e);
|
||||||
} else {
|
|
||||||
console.debug('The cache has expired');
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
// We only log since we want to fetch the changesets from Hg
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (csets.length === 0) {
|
if (!csets || csets.length === 0) {
|
||||||
console.debug('The local cache was not available.');
|
console.debug('The local cache was not available.');
|
||||||
const pushes = await getJsonPushes(repoName);
|
const text = await (await getJsonPushes(repoName)).json();
|
||||||
const text = await (pushes).json();
|
csets = await pushesToCsets(text.pushes, hidden);
|
||||||
csets = await pushesToCsets(text.pushes, hidden);
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const currentTime = (new Date()).getTime();
|
saveInCache('changesets', csets);
|
||||||
console.debug('Storing on local cache.');
|
|
||||||
await localForage.setItem('changesets', csets);
|
|
||||||
await localForage.setItem('cachedTime', currentTime);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.info('We have failed to store to the local cache');
|
console.info('We have failed to store to the local cache');
|
||||||
// We don't want to throw an error and abort code execution
|
// We don't want to throw an error and abort code execution
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return csets;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async (repoName, hidden) => {
|
|
||||||
let csets = [];
|
|
||||||
if (settings.CACHE_CONFIG.ENABLED) {
|
|
||||||
console.debug('Local caching enabled');
|
|
||||||
csets = await getCsetsFromCache(repoName, hidden);
|
|
||||||
} else {
|
} else {
|
||||||
console.debug('Local caching disabled');
|
|
||||||
const text = await (await getJsonPushes(repoName)).json();
|
const text = await (await getJsonPushes(repoName)).json();
|
||||||
csets = await pushesToCsets(text.pushes, hidden);
|
csets = await pushesToCsets(text.pushes, hidden);
|
||||||
}
|
}
|
||||||
return csets;
|
return csets;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default getChangesets;
|
||||||
|
|
|
@ -1,8 +1,47 @@
|
||||||
import { clear } from 'localforage';
|
import * as localForage from 'localforage';
|
||||||
|
import settings from '../settings';
|
||||||
|
|
||||||
export default () => {
|
const CACHING_KEY = 'cachedTime';
|
||||||
|
|
||||||
|
export const isExpiredCache = (lastCaching) => {
|
||||||
|
const MSTOS = 1000;
|
||||||
|
const currentTime = (new Date()).getTime();
|
||||||
|
const secondsDifference = (currentTime - lastCaching) / MSTOS;
|
||||||
|
console.debug((currentTime - lastCaching) / MSTOS);
|
||||||
|
return (
|
||||||
|
(typeof lastCaching === 'number') &&
|
||||||
|
(secondsDifference > settings.CACHE_CONFIG.SECONDS_TO_EXPIRE))
|
||||||
|
|| false;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getFromCache = async (key) => {
|
||||||
|
let data;
|
||||||
try {
|
try {
|
||||||
clear();
|
const lastCaching = await localForage.getItem(CACHING_KEY);
|
||||||
|
data = await localForage.getItem(key);
|
||||||
|
if (!isExpiredCache(lastCaching) && data) {
|
||||||
|
console.debug('Retrieved data from the local cache.');
|
||||||
|
} else {
|
||||||
|
console.debug('The cache has expired');
|
||||||
|
data = undefined;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// We only log since we want to fetch the changesets from Hg
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const saveInCache = async (key, data) => {
|
||||||
|
const currentTime = (new Date()).getTime();
|
||||||
|
console.debug('Storing on local cache.');
|
||||||
|
await localForage.setItem(key, data);
|
||||||
|
await localForage.setItem(CACHING_KEY, currentTime);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const clearCache = () => {
|
||||||
|
try {
|
||||||
|
localForage.clear();
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче