From 9c735624af983dbd4f0de05ef2a7c964b7606f81 Mon Sep 17 00:00:00 2001 From: dexagod Date: Tue, 6 Oct 2020 11:36:24 +0200 Subject: [PATCH] Added caching with TTL of 10 seconds --- package.json | 1 + src/Components/MarriageViewComponent.js | 3 ++- src/hooks/useNotifications.js | 4 ++-- src/util/Cache.js | 27 +++++++++++++++++++++++++ src/util/Util.js | 14 ++++++++++++- src/util/notifications.js | 2 +- yarn.lock | 12 +++++++++++ 7 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/util/Cache.js diff --git a/package.json b/package.json index a92a4f9..0cbb884 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Components/MarriageViewComponent.js b/src/Components/MarriageViewComponent.js index d040f6b..5bc81fb 100644 --- a/src/Components/MarriageViewComponent.js +++ b/src/Components/MarriageViewComponent.js @@ -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? @@ -55,7 +56,7 @@ const MarriageViewComponent = (props) => { refreshContacts() var interval = setInterval(() => { refreshContacts() - }, 7000); + }, TIMEOUT); return () => { mounted = false; clearInterval(interval); diff --git a/src/hooks/useNotifications.js b/src/hooks/useNotifications.js index fcc93da..e1df1b1 100644 --- a/src/hooks/useNotifications.js +++ b/src/hooks/useNotifications.js @@ -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'); @@ -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; diff --git a/src/util/Cache.js b/src/util/Cache.js new file mode 100644 index 0000000..ed536a5 --- /dev/null +++ b/src/util/Cache.js @@ -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) +} diff --git a/src/util/Util.js b/src/util/Util.js index db72ce3..1ef4bfe 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -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] @@ -65,7 +67,9 @@ 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 @@ -73,8 +77,16 @@ export async function getStore(URI){ 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) diff --git a/src/util/notifications.js b/src/util/notifications.js index 1067ca7..51fdfd4 100644 --- a/src/util/notifications.js +++ b/src/util/notifications.js @@ -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})}) diff --git a/yarn.lock b/yarn.lock index 2d08b76..1a9cc0d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -5904,6 +5911,11 @@ dotenv@8.2.0: 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"