Skip to content

Commit

Permalink
Added caching with TTL of 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexagod committed Oct 6, 2020
1 parent c52a622 commit 9c73562
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@testing-library/user-event": "^7.1.2",
"blob-stream": "^0.1.3",
"bootstrap": "^4.5.2",
"cache": "^2.3.1",
"jquery": "^3.5.1",
"jspdf": "^2.1.1",
"n3": "^1.6.3",
Expand Down
3 changes: 2 additions & 1 deletion src/Components/MarriageViewComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const INVITATIONACCEPTED = ns.demo('accepted')
const INVITATIONREFUSED = ns.demo('refused')

const DEFAULTOFFICIAL = 'https://weddinator.inrupt.net/profile/card#me'
const TIMEOUT = 10 * 1000;

// TODO:: remove marriage button only for creator
// TODO:: If spouse if 2 times the same person it will only show once (same for witnesses) because of ldflex => update this to use N3 in usecontracts?
Expand Down Expand Up @@ -55,7 +56,7 @@ const MarriageViewComponent = (props) => {
refreshContacts()
var interval = setInterval(() => {
refreshContacts()
}, 7000);
}, TIMEOUT);
return () => {
mounted = false;
clearInterval(interval);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getNotificationMetadata, getNotification, checkNewNotifications } from
import { getNotificationTypes, getProfileContracts, getContractData, getCertificateData } from '../util/Util';
import ns from '../util/NameSpaces';
import { addContractPatch, patchProfileWithContract, setProposalValidatedBy, updateMarriageContractStatus } from '../util/MarriageController';
const POLLINGRATE = 20000
const TIMEOUT = 10 * 1000

// const { default: data } = require('@solid/query-ldflex');

Expand All @@ -18,7 +18,7 @@ const useNotifications = function(webId) {
updateNotifications(webId, notifications).then( newNotifications => {
if (mounted && newNotifications && newNotifications.length) setNotifications(notifications.concat(newNotifications))
})
}, POLLINGRATE);
}, TIMEOUT);
return () => {
clearInterval(interval);
mounted = false;
Expand Down
27 changes: 27 additions & 0 deletions src/util/Cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const TTLCache = require("cache");
const DEFAULTTTL = 8 * 1000
var Cache = (function () {
var instance;

function createInstance() {
return new TTLCache(DEFAULTTTL);
}

return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();

export function checkCache(key) {
return Cache.getInstance().get(key)
}

export function setCache(key, value, ttl) {
ttl = ttl || DEFAULTTTL;
return Cache.getInstance().put(key, value, ttl)
}
14 changes: 13 additions & 1 deletion src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import ExitToAppIcon from '@material-ui/icons/ExitToApp';
import ns from "../util/NameSpaces"
import SubmissionViewComponent from '../Components/SubmissionViewComponent'
import CertificateViewComponent from '../Components/CertificateViewComponent'
import {checkCache, setCache} from './Cache'

const { default: data } = require('@solid/query-ldflex');

export const validStatusCodes = [200, 201, 202]
Expand Down Expand Up @@ -65,16 +67,26 @@ export const activeDrawerItemMapping = {
help: "help",
}

export async function getStore(URI){
export async function getStore(URI, ttl){
const cached = checkCache(URI)
if (cached) return cached;
try {
const response = await getFile(URI)
const code = (await response).status
if (validStatusCodes.indexOf(code) === -1){
return null;
}
const responseData = await response.text()

// If concurrent requests already filled cache
const cached = checkCache(URI)
if (cached) return cached;

const store = new N3.Store()
store.addQuads(await new N3.Parser({ baseIRI: URI}).parse(responseData))
if (store) {
if (!checkCache(URI)) setCache(URI, store, ttl);
}
return store
} catch (e) {
console.error(e)
Expand Down
2 changes: 1 addition & 1 deletion src/util/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function checkNewNotifications(webId, currentNotificationIds) {
export async function getNotificationMetadata(webId) {
const inbox = await getInbox(webId)
if(!inbox) return []
const store = await getStore(inbox)
const store = await getStore(inbox, 0)
if (!store) return [];
const notificationsMetadata = await store.getQuads(inbox, ns.ldp('contains'))
const metadataObjects = notificationsMetadata.map(quad => { return ( {id: quad.object.value})})
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4565,6 +4565,13 @@ cache-base@^1.0.1:
union-value "^1.0.0"
unset-value "^1.0.0"

cache@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/cache/-/cache-2.3.1.tgz#78334a30b2c9daca2d10abbe8f9a0b610ddf91ba"
integrity sha512-FR/0U9GMDAq4ERSom2ThoiXz89BlhCSftDOBUgFSBTw0Kh+zBXbDan/DMzh2pP3lDqmOatruG0NqUkeXQqaNKQ==
dependencies:
ds "^1.4.2"

call-me-maybe@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b"
Expand Down Expand Up @@ -5904,6 +5911,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

ds@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/ds/-/ds-1.4.2.tgz#0857aa213790a4fb3abb365b9cec0e9ba8569393"
integrity sha1-CFeqITeQpPs6uzZbnOwOm6hWk5M=

duplexer@^0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6"
Expand Down

0 comments on commit 9c73562

Please sign in to comment.