add 2 min request cache, remove a few unneeded semicolons

This commit is contained in:
Daniel Waxweiler 2021-04-04 18:03:05 +02:00
parent 214fc50d6d
commit 28245574d8
11 changed files with 106 additions and 40 deletions

16
package-lock.json generated
View File

@ -11,7 +11,8 @@
"dependencies": { "dependencies": {
"graphql": "^15.5.0", "graphql": "^15.5.0",
"graphql-request": "^3.4.0", "graphql-request": "^3.4.0",
"luxon": "^1.26.0" "luxon": "^1.26.0",
"object-hash": "^2.1.1"
}, },
"devDependencies": { "devDependencies": {
"ava": "^3.15.0", "ava": "^3.15.0",
@ -6670,6 +6671,14 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/object-hash": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/object-hash/-/object-hash-2.1.1.tgz",
"integrity": "sha512-VOJmgmS+7wvXf8CjbQmimtCnEx3IAoLxI3fp2fbWehxrWBcAQFbk+vcwb6vzR0VZv/eNCJ/27j151ZTwqW/JeQ==",
"engines": {
"node": ">= 6"
}
},
"node_modules/object-keys": { "node_modules/object-keys": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
@ -16445,6 +16454,11 @@
} }
} }
}, },
"object-hash": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/object-hash/-/object-hash-2.1.1.tgz",
"integrity": "sha512-VOJmgmS+7wvXf8CjbQmimtCnEx3IAoLxI3fp2fbWehxrWBcAQFbk+vcwb6vzR0VZv/eNCJ/27j151ZTwqW/JeQ=="
},
"object-keys": { "object-keys": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",

View File

@ -20,7 +20,8 @@
"dependencies": { "dependencies": {
"graphql": "^15.5.0", "graphql": "^15.5.0",
"graphql-request": "^3.4.0", "graphql-request": "^3.4.0",
"luxon": "^1.26.0" "luxon": "^1.26.0",
"object-hash": "^2.1.1"
}, },
"devDependencies": { "devDependencies": {
"ava": "^3.15.0", "ava": "^3.15.0",

View File

@ -1,6 +1,7 @@
### [Unreleased] ### [Unreleased]
#### Added #### Added
- Donation link to WordPress Plugin Directory sidebar and to `package.json` - Donation link to WordPress Plugin Directory sidebar and to `package.json`
- Cache requests for 2 minutes
#### Changed #### Changed
- Update luxon dependency - Update luxon dependency
- Update dev dependencies jsdom, webpack, webpack-cli - Update dev dependencies jsdom, webpack, webpack-cli

View File

@ -1,5 +1,5 @@
import test from 'ava'; import test from 'ava'
import { DateTimeWrapper } from './date-time-wrapper'; import { DateTimeWrapper } from './date-time-wrapper'
test('#getShortDate usual date', t => { test('#getShortDate usual date', t => {
const d = new DateTimeWrapper('2020-12-24T16:45:00Z') const d = new DateTimeWrapper('2020-12-24T16:45:00Z')

View File

@ -1,4 +1,4 @@
import { DateTime } from 'luxon'; import { DateTime } from 'luxon'
export class DateTimeWrapper { export class DateTimeWrapper {
@ -23,6 +23,6 @@ export class DateTimeWrapper {
} }
static getCurrentDatetimeAsString() { static getCurrentDatetimeAsString() {
return DateTime.local().toString(); return DateTime.local().toString()
} }
} }

View File

@ -1,8 +1,8 @@
import { DateTimeWrapper } from './date-time-wrapper'; import { DateTimeWrapper } from './date-time-wrapper'
import * as GraphqlWrapper from './graphql-wrapper' import * as GraphqlWrapper from './graphql-wrapper'
import * as HtmlCreator from './html-creator' import * as HtmlCreator from './html-creator'
const NAME = '<wordpress-name>'; const NAME = '<wordpress-name>'
function displayEvents(data, list) { function displayEvents(data, list) {
const maxEventsCount = list.getAttribute('data-maximum') const maxEventsCount = list.getAttribute('data-maximum')
@ -11,7 +11,7 @@ function displayEvents(data, list) {
for (let i = 0; i < eventsCount; i++) { for (let i = 0; i < eventsCount; i++) {
const li = document.createElement('li') const li = document.createElement('li')
const a = HtmlCreator.createAnchorElement({ text: events[i].title, url: events[i].url }); const a = HtmlCreator.createAnchorElement({ text: events[i].title, url: events[i].url })
li.appendChild(a) li.appendChild(a)
const br = document.createElement('br') const br = document.createElement('br')
@ -26,7 +26,7 @@ function displayEvents(data, list) {
dateText += endsOn.getShortDate() + ' ' dateText += endsOn.getShortDate() + ' '
} }
dateText += endsOn.get24Time() dateText += endsOn.get24Time()
const textnode = document.createTextNode(dateText); const textnode = document.createTextNode(dateText)
li.appendChild(textnode) li.appendChild(textnode)
list.appendChild(li) list.appendChild(li)
@ -44,7 +44,7 @@ document.addEventListener('DOMContentLoaded', () => {
const eventLists = document.getElementsByClassName(NAME + '_events-list') const eventLists = document.getElementsByClassName(NAME + '_events-list')
for (let list of eventLists) { for (let list of eventLists) {
const url = list.getAttribute('data-url') + '/api' const url = list.getAttribute('data-url') + '/api'
const limit = list.getAttribute('data-maximum') const limit = parseInt(list.getAttribute('data-maximum'))
const groupName = list.getAttribute('data-group-name') const groupName = list.getAttribute('data-group-name')
if (groupName) { if (groupName) {
GraphqlWrapper.getUpcomingEventsByGroupName({ url, limit, groupName }) GraphqlWrapper.getUpcomingEventsByGroupName({ url, limit, groupName })

View File

@ -1,30 +1,11 @@
import { request, gql } from 'graphql-request' import { SessionCache } from './session-cache'
import { request } from 'graphql-request'
import { DateTimeWrapper } from './date-time-wrapper' import { DateTimeWrapper } from './date-time-wrapper'
export function getUpcomingEvents({ url, limit }) { export function getUpcomingEvents({ url, limit }) {
const query = gql` const query = `
query { query ($limit: Int) {
events(limit:${limit}) { events(limit: $limit) {
elements {
id,
title,
url,
beginsOn,
endsOn
},
total
}
}
`
return request(url, query)
}
export function getUpcomingEventsByGroupName({ url, limit, groupName }) {
const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString();
const query = gql`
query {
group(preferredUsername:"${groupName}") {
organizedEvents(afterDatetime:"${afterDatetime}", limit:${limit}) {
elements { elements {
id, id,
title, title,
@ -35,7 +16,41 @@ export function getUpcomingEventsByGroupName({ url, limit, groupName }) {
total total
} }
} }
}
` `
return request(url, query) const dataInCache = SessionCache.get({ url, query, variables: { limit }})
if (dataInCache !== null)
return Promise.resolve(dataInCache)
return request(url, query, { limit })
.then((data) => {
SessionCache.add({ url, query, variables: { limit }}, data)
return Promise.resolve(data)
})
}
export function getUpcomingEventsByGroupName({ url, limit, groupName }) {
const query = `
query ($afterDatetime: DateTime, $groupName: String, $limit: Int) {
group(preferredUsername: $groupName) {
organizedEvents(afterDatetime: $afterDatetime, limit: $limit) {
elements {
id,
title,
url,
beginsOn,
endsOn
},
total
}
}
}
`
const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
const dataInCache = SessionCache.get({ url, query, variables: { afterDatetime, groupName, limit }})
if (dataInCache !== null)
return Promise.resolve(dataInCache)
return request(url, query, { afterDatetime, groupName, limit })
.then((data) => {
SessionCache.add({ url, query, variables: { afterDatetime, groupName, limit }}, data)
return Promise.resolve(data)
})
} }

View File

@ -1,7 +1,7 @@
import test from 'ava'; import test from 'ava'
import { JSDOM } from 'jsdom'; import { JSDOM } from 'jsdom'
import * as HtmlCreator from './html-creator'; import * as HtmlCreator from './html-creator'
test.beforeEach(() => { test.beforeEach(() => {
global.document = new JSDOM().window.document global.document = new JSDOM().window.document

View File

@ -0,0 +1,6 @@
import test from 'ava'
import { hash } from './object-hash-wrapper'
test('#hash object', t => {
t.is(hash({foo: 'bar'}), 'a75c05bdca7d704bdfcd761913e5a4e4636e956b')
})

View File

@ -0,0 +1,5 @@
import objectHash from 'object-hash'
export default function hash(object) {
return objectHash(object)
}

View File

@ -0,0 +1,24 @@
import hash from './object-hash-wrapper'
const MAX_AGE_IN_MS = 120000
export class SessionCache {
static add(parameters, data) {
const key = hash(parameters)
const timestamp = Date.now()
const value = {
data,
timestamp,
}
sessionStorage.setItem(key, JSON.stringify(value))
}
static get(parameters) {
const key = hash(parameters)
const value = JSON.parse(sessionStorage.getItem(key))
if (value.timestamp && value.timestamp > Date.now() - MAX_AGE_IN_MS)
return value.data
return null
}
}