Commit ba9b1188 authored by Anton's avatar Anton

Add caching to metadata store module

parent 6201bd8d
......@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Metadata store module now caches responses from the transcoder service until app reload
- 'Documents' source tab now available in MoSys (so far only shows images)
- Access control menu available for MoSys grids
......
......@@ -4,8 +4,15 @@ import { Assert } from 'mbjs-utils'
const metadata = {
namespaced: true,
state: {},
mutations: {},
state: {
cache: {}
},
mutations: {
setCache (state, args) {
if (args.length !== 2) throw new Error('metadata/setCache: Invalid arguments')
state.cache[args[0]] = args[1]
}
},
actions: {
async get (context, payload) {
Assert.ok(typeof payload === 'string' || typeof payload.body.source.id === 'string',
......@@ -16,13 +23,18 @@ const metadata = {
let metadataURL
if (typeof payload === 'string') metadataURL = `${process.env.TRANSCODER_HOST}/metadata/${payload}`
else metadataURL = `${process.env.TRANSCODER_HOST}/metadata/url?url=${encodeURIComponent(payload.body.source.id)}`
let metadata = {}
try {
let result = await axios.get(metadataURL, { headers })
metadata = result.data
}
catch (err) {
if (!err.response || err.response.status > 404) console.error(err.message)
let metadata
metadata = context.state.cache[metadataURL] || metadata
if (!metadata) {
metadata = {}
try {
let result = await axios.get(metadataURL, {headers})
metadata = result.data
context.commit('setCache', [metadataURL, metadata])
}
catch (err) {
if (!err.response || err.response.status > 404) console.error(err.message)
}
}
const titleQuery = {
'target.id': typeof payload === 'string' ? `${BASE_URI}/annotations/${payload}` : payload.id,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment