Merge pull request #10 from Maathias/redis

feat: add redis for cache
This commit was merged in pull request #10.
This commit is contained in:
Sebastian Fudalej authored and GitHub committed 2021-09-26 22:16:05 +02:00
commit 390dac6dc1
5 files changed
+97 -14

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import dotenv from 'dotenv'
import redis from 'redis'
dotenv.config()
const port = process.env.CACHE_PORT
const hostName = process.env.CACHE_HOST
const client = redis.createClient({ host: hostName, port })
client.on('ready', function (error) {
console.error(`cache: connected`)
})
const cache = {
get: (key) => {
return new Promise((resolve, reject) => {
client.get(key, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
},
set: (key, value) => {
client.set(key, value)
},
}
export default cache
+26 -14
View File
@@ -1,4 +1,5 @@
import Jwt from 'jsonwebtoken'
import cache from './cache.js'
import {
getTrack,
@@ -45,22 +46,33 @@ const db = {
*/
get: (id) => {
return new Promise((resolve, reject) => {
// query db first
getTrack(id)
.then((tdata) => resolve(new Track(tdata)))
.catch((dbErr) => {
// track not found in DB, fetch online
byId(id)
.then((track) => {
putTrack(track)
resolve(track)
// try to find in redis
cache.get(id).then((data) => {
if (data) {
resolve(JSON.parse(data))
} else {
// if not found, query db
getTrack(id)
.then((tdata) => {
cache.set(id, JSON.stringify(tdata))
resolve(new Track(tdata))
})
.catch((spotifyErr) => {
reject(
new DbError('DbErrorNotFound', `Could not retrieve track`)
)
.catch((dbErr) => {
// track not found in DB, fetch online
byId(id)
.then((track) => {
cache.set(id, JSON.stringify(track))
putTrack(track)
resolve(track)
})
.catch((spotifyErr) => {
reject(
new DbError('DbErrorNotFound', `Could not retrieve track`)
)
})
})
})
}
})
})
},
},
+10
View File
@@ -13,6 +13,8 @@ services:
dbUser: root
dbPass: supersecurepassword123
portWs: 3020
CACHE_HOST: cache
CACHE_PORT: 6379
ports:
- 3010:3010
- 3020:3020
@@ -31,3 +33,11 @@ services:
- ./database:/data/db
ports:
- 27017:27017
cache:
image: redis:6.2-alpine
container_name: cache
restart: always
environment:
ALLOW_EMPTY_PASSWORD: yes
ports:
- 6379:6379
+29
View File
@@ -1572,6 +1572,35 @@
"picomatch": "^2.2.1"
}
},
"redis": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
"requires": {
"denque": "^1.5.0",
"redis-commands": "^1.7.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0"
}
},
"redis-commands": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
},
"redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60="
},
"redis-parser": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
"requires": {
"redis-errors": "^1.0.0"
}
},
"regexp-clone": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-1.0.0.tgz",
+1
View File
@@ -33,6 +33,7 @@
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.13.9",
"nodemon": "^2.0.13",
"redis": "^3.1.2",
"spotify-to-yt": "^1.0.5",
"spotify-web-api-node": "^5.0.2",
"ws": "^8.2.2"