Skip to content

Commit

Permalink
Add 24h cache for metadata requests
Browse files Browse the repository at this point in the history
This replaces zotero-api-client plugin used previously (before
citeproc-rs conversion).
  • Loading branch information
tnajdek committed Jun 8, 2021
1 parent 5562906 commit c3e3d6a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
74 changes: 74 additions & 0 deletions src/js/api-utils.js
@@ -0,0 +1,74 @@
import api from 'zotero-api-client';

// Zbib only uses API for meta requests, these rarely, if ever, change hence are cached for 24 hours
// to improve performance. localStorage is used to record time of the last request, then, if cache
// is fresh, we use ask browser to use `force-cache` caching strategy which will go to the disk
// cache before making requests

const apiCheckCache = key => {
var cacheTimes = {}, okToUseCache = false;
try {
cacheTimes = JSON.parse(localStorage.getItem('zotero-bib-api-cache')) || {};
} catch(_) {
// ignore
}

if(key in cacheTimes) {
okToUseCache = (Date.now() - cacheTimes[key]) < 24 * 60 * 60 * 1000;
}

if(!okToUseCache) {
cacheTimes[key] = Date.now();
localStorage.setItem('zotero-bib-api-cache', JSON.stringify(cacheTimes));
}

return okToUseCache;
}

const getItemTypes = async (retryOnFailure = true) => {
try {
return (await api()
.itemTypes()
.get({ cache: apiCheckCache('itemTypes') ? 'force-cache' : 'default'})
).getData();
} catch(e) {
localStorage.removeItem('zotero-bib-api-cache');
if(retryOnFailure) {
return getItemTypes(false);
} else {
throw e;
}
}
};

const getItemTypeMeta = async (itemType, retryOnFailure = true) => {
try {
var [itemTypeR, itemTypeFieldsR, creatorTypesR] = await Promise.all([
api()
.itemTypes()
.get({ cache: apiCheckCache('itemTypes') ? 'force-cache' : 'default'}),
api()
.itemTypeFields(itemType)
.get({ cache: apiCheckCache(`itemTypeFields-${itemType}`) ? 'force-cache' : 'default'}),
api()
.itemTypeCreatorTypes(itemType)
.get({ cache: apiCheckCache(`itemTypeCreatorTypes-${itemType}`) ? 'force-cache' : 'default'})
]);
} catch(e) {
localStorage.removeItem('zotero-bib-api-cache');
if(retryOnFailure) {
return getItemTypeMeta(itemType, false);
} else {
throw e;
}
}

return {
itemTypes: itemTypeR.getData(),
itemTypeFields: itemTypeFieldsR.getData(),
itemTypeCreatorTypes: creatorTypesR.getData()
};
};


export { getItemTypes, getItemTypeMeta }
2 changes: 1 addition & 1 deletion src/js/components/editor.jsx
Expand Up @@ -11,7 +11,7 @@ import baseMappings from 'zotero-base-mappings';
import Button from './ui/button';
import ItemBox from './itembox';
import Spinner from './ui/spinner';
import { getItemTypeMeta } from '../utils';
import { getItemTypeMeta } from '../api-utils';
import { hideFields, noEditFields } from '../constants/item';
import { reverseMap } from '../utils';
import Modal from './modal';
Expand Down
25 changes: 3 additions & 22 deletions src/js/utils.js
@@ -1,10 +1,10 @@
import api from 'zotero-api-client';
import balanced from 'balanced-match';
import baseMappings from 'zotero-base-mappings';
import CiteprocWrapper from './citeproc-wrapper';
import ZoteroBib from 'zotero-translation-client';

const stylesCache = {};
import CiteprocWrapper from './citeproc-wrapper';
import { getItemTypes, getItemTypeMeta } from './api-utils';


const ensureNoBlankItems = itemsCSL => itemsCSL.map(item => {
if(!('author' in item) && !('title' in item) && !('issued' in item)) {
Expand Down Expand Up @@ -70,24 +70,6 @@ const fetchWithCachedFallback = async url => {
}
}

const getItemTypes = async () => {
return (await api().itemTypes().get()).getData();
};

const getItemTypeMeta = async (itemType) => {
var [itemTypeR, itemTypeFieldsR, creatorTypesR] = await Promise.all([
api().itemTypes().get(),
api().itemTypeFields(itemType).get(),
api().itemTypeCreatorTypes(itemType).get()
]);

return {
itemTypes: itemTypeR.getData(),
itemTypeFields: itemTypeFieldsR.getData(),
itemTypeCreatorTypes: creatorTypesR.getData()
};
};

const validateItem = async item => {
const { itemTypeFields, itemTypeCreatorTypes } = await getItemTypeMeta(item.itemType);

Expand Down Expand Up @@ -438,7 +420,6 @@ export {
getCitation,
getExpandedCitationStyles,
getItemsCSL,
getItemTypeMeta,
getItemTypes,
getOneTimeBibliographyOrFallback,
isLikeUrl,
Expand Down

0 comments on commit c3e3d6a

Please sign in to comment.