diff --git a/src/components/app.jsx b/src/components/app.jsx index 1ce5825..4609d39 100644 --- a/src/components/app.jsx +++ b/src/components/app.jsx @@ -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 () => ( { - if (clearLocalCache()) { + if (clearCache()) { return (

The local database has been cleared.

); } return (

Failed to clear the local DB.

); diff --git a/src/settings.js b/src/settings.js index 186d9fc..f94a362 100644 --- a/src/settings.js +++ b/src/settings.js @@ -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; diff --git a/src/utils/hg.js b/src/utils/hg.js index 320c58f..af6e2fc 100644 --- a/src/utils/hg.js +++ b/src/utils/hg.js @@ -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 = []; - 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'); + if (settings.CACHE_CONFIG.ENABLED) { + try { + csets = await getFromCache('changesets'); + } catch (e) { + // We only log since we want to fetch the changesets from Hg + console.error(e); } - } catch (e) { - // We only log since we want to fetch the changesets from Hg - console.error(e); - } - if (csets.length === 0) { - console.debug('The local cache was not available.'); - const pushes = await getJsonPushes(repoName); - const text = await (pushes).json(); - csets = await pushesToCsets(text.pushes, hidden); + if (!csets || csets.length === 0) { + console.debug('The local cache was not available.'); + 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; diff --git a/src/utils/localCache.js b/src/utils/localCache.js index d108541..0d53204 100644 --- a/src/utils/localCache.js +++ b/src/utils/localCache.js @@ -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);