Refactor: Clean up local caching code

This commit is contained in:
Armen Zambrano G 2018-03-26 13:11:18 -04:00 коммит произвёл Armen Zambrano
Родитель 71a5aa8aa0
Коммит 06d09b094b
4 изменённых файлов: 69 добавлений и 52 удалений

Просмотреть файл

@ -5,7 +5,7 @@ import ChangesetsViewerContainer from '../containers/summaryViewer';
import DiffViewerContainer from '../containers/diffViewer';
import FileViewerContainer from '../containers/fileViewer';
import settings from '../settings';
import clearLocalCache from '../utils/localCache';
import { clearCache } from '../utils/localCache';
import '../style.css';
import AppDisclaimer from './disclaimer';
import GitHubRibbon from './githubRibbon';
@ -42,7 +42,7 @@ export default () => (
<Route
path="/clear-cache"
render={() => {
if (clearLocalCache()) {
if (clearCache()) {
return (<p>The local database has been cleared.</p>);
}
return (<p>Failed to clear the local DB.</p>);

Просмотреть файл

@ -1,4 +1,4 @@
export default {
const settings = {
BZ_URL: 'http://bugzilla.mozilla.org',
CACHE_CONFIG: {
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))
.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 { JSON_HEADERS, PLAIN_HEADERS } from './fetch_data';
import { getFromCache, saveInCache } from './localCache';
export const HG_HOST = 'https://hg.mozilla.org';
@ -75,64 +75,34 @@ const pushesToCsets = async (pushes, hidden) => {
return filteredCsets;
};
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) => {
const getChangesets = async (repoName, hidden) => {
let csets = [];
if (settings.CACHE_CONFIG.ENABLED) {
try {
const lastCaching = await localForage.getItem('cachedTime');
const cachedCsets = await localForage.getItem('changesets');
if (!isExpiredCache(lastCaching) && cachedCsets) {
console.debug(`Retrieved cached changesets. We have ${cachedCsets.length} changesets.`);
csets = cachedCsets;
} else {
console.debug('The cache has expired');
}
csets = await getFromCache('changesets');
} 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.');
const pushes = await getJsonPushes(repoName);
const text = await (pushes).json();
const text = await (await getJsonPushes(repoName)).json();
csets = await pushesToCsets(text.pushes, hidden);
}
try {
const currentTime = (new Date()).getTime();
console.debug('Storing on local cache.');
await localForage.setItem('changesets', csets);
await localForage.setItem('cachedTime', currentTime);
saveInCache('changesets', csets);
} catch (e) {
console.info('We have failed to store to the local cache');
// We don't want to throw an error and abort code execution
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 {
console.debug('Local caching disabled');
const text = await (await getJsonPushes(repoName)).json();
csets = await pushesToCsets(text.pushes, hidden);
}
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 {
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;
} catch (e) {
console.log(e);