Commit cf006ea6 authored by A. Koch's avatar A. Koch

added memcached to metadata route, close #3

parent c6ac0b7f
......@@ -17,6 +17,10 @@
"dbName": "MONGODB_PROFILES_DBNAME"
}
},
"metadata": {
"memcachedURL": "MEMCACHED_URL",
"lifetime": "METADATA_CACHE_LIFETIME"
},
"apiKeys": {
"youtube": "YOUTUBE_API_KEY",
"vimeo": "VIMEO_ACCESS_TOKEN",
......
......@@ -27,6 +27,10 @@
"dbName": "motionbank-api"
}
},
"metadata": {
"memcachedURL": null,
"lifetime": 86400
},
"auth": {
"jwks": {
"cache": true,
......
......@@ -871,6 +871,11 @@
"resolved": "https://registry.npmjs.org/connected/-/connected-0.0.2.tgz",
"integrity": "sha1-e1dVshbOMf+rzMOOn04d/Bw7fG0="
},
"connection-parse": {
"version": "0.0.7",
"resolved": "https://registry.npmjs.org/connection-parse/-/connection-parse-0.0.7.tgz",
"integrity": "sha1-GOcxiqsGppkmc3KxDFIm0locmmk="
},
"contains-path": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/contains-path/-/contains-path-0.1.0.tgz",
......@@ -1668,6 +1673,15 @@
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
"dev": true
},
"hashring": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/hashring/-/hashring-3.2.0.tgz",
"integrity": "sha1-/aTv3oqiLNuX+x0qZeiEAeHBRM4=",
"requires": {
"connection-parse": "0.0.x",
"simple-lru-cache": "0.0.x"
}
},
"hosted-git-info": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.6.0.tgz",
......@@ -1873,6 +1887,14 @@
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
},
"jackpot": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/jackpot/-/jackpot-0.0.6.tgz",
"integrity": "sha1-PP8GQoXL9m9OqyWTyQvOgWqCGEk=",
"requires": {
"retry": "0.6.0"
}
},
"js-tokens": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz",
......@@ -2244,9 +2266,9 @@
}
},
"mbjs-media": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/mbjs-media/-/mbjs-media-0.0.10.tgz",
"integrity": "sha512-ryabEdi9G+lrdQiFg3WF3G/5Rn+Qs71RcMbgQ1+HekpUvunoirdVngCpUtygv/IW5/RxqLfBEkDNfIiQeeL2aw==",
"version": "0.0.11",
"resolved": "https://registry.npmjs.org/mbjs-media/-/mbjs-media-0.0.11.tgz",
"integrity": "sha512-+BNGm1jsuzZ0dznnnfkDn4dgE14xGqaFw7q2zo9lhU+z+971YEuYHhj2rqsQctzpu+ALsL7zB/4pZ/QrigZJsg==",
"requires": {
"axios": "^0.18.0",
"bluebird": "^3.5.1",
......@@ -2322,6 +2344,15 @@
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
},
"memcached": {
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/memcached/-/memcached-2.2.2.tgz",
"integrity": "sha1-aPhsz9hLz5PMJe1G1tf8DHUhydU=",
"requires": {
"hashring": "3.2.x",
"jackpot": ">=0.0.6"
}
},
"metapak": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/metapak/-/metapak-1.0.3.tgz",
......@@ -2973,6 +3004,11 @@
"signal-exit": "^3.0.2"
}
},
"retry": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/retry/-/retry-0.6.0.tgz",
"integrity": "sha1-HAEHEyeab9Ho3vKK8MP/GHHKpTc="
},
"rimraf": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.2.tgz",
......@@ -3160,6 +3196,11 @@
"integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=",
"dev": true
},
"simple-lru-cache": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/simple-lru-cache/-/simple-lru-cache-0.0.2.tgz",
"integrity": "sha1-1ZzDoZPBpdAyD4Tucy9uRxPlEd0="
},
"slice-ansi": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-1.0.0.tgz",
......
......@@ -2,6 +2,8 @@ const
config = require('config'),
send = require('@polka/send-type'),
TinyEmitter = require('tiny-emitter'),
Memcached = require('memcached'),
{ ObjectUtil } = require('mbjs-utils'),
{ getMetaData } = require('mbjs-media/src/util/metadata')
const fetchMetaData = async (annotation, user, annotationsService) => {
......@@ -26,14 +28,36 @@ class Metadata extends TinyEmitter {
super()
this._annotations = annotationsService
if (config.metadata.memcachedURL) this._memcached = new Memcached(config.metadata.memcachedURL)
const _this = this
app.get('/metadata/:id', async (req, res) => {
const result = await _this._annotations.getHandler(req)
const annotation = result.data
const key = `metadata_${ObjectUtil.slug(annotation.body.source.id)}`
if (!annotation) return _this._errorResponse(res, 404)
const metadata = await fetchMetaData(annotation, req.user, _this._annotations)
let metadata
if (_this._memcached) {
metadata = await new Promise((resolve, reject) => {
console.log('memcached key', key)
_this._memcached.get(key, function (err, data) {
if (err) console.error('failed to get metadata from cache', err.message)
resolve(data)
})
})
}
if (!metadata) {
metadata = await fetchMetaData(annotation, req.user, _this._annotations)
if (_this._memcached && metadata) {
await new Promise((resolve, reject) => {
_this._memcached.set(key, metadata, parseInt(config.metadata.lifetime.toString()), err => {
if (err) console.error('failed to store metadata in cache', err.message)
resolve()
})
})
}
}
if (!metadata) return _this._errorResponse(res, 404)
_this._response(req, res, metadata)
})
......
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