Feat: Redis cache #129

Merged
KevinMidboe merged 14 commits from feat/redis-cache into master 2020-04-08 21:04:07 +00:00
2 changed files with 9 additions and 8 deletions
Showing only changes of commit 2b25397253 - Show all commits

View File

@@ -48,13 +48,13 @@ class Plex {
this.cache = cache || redisCache;
this.cacheTags = {
machineInfo: 'mi',
search: 's'
machineInfo: 'plex/mi',
search: 'plex/s'
}
}
fetchMachineIdentifier() {
const cacheKey = `plex/${this.cacheTags.machineInfo}`
const cacheKey = `${this.cacheTags.machineInfo}`
const url = `http://${this.plexIP}:${this.plexPort}/`
const options = {
timeout: 20000,
@@ -130,8 +130,9 @@ class Plex {
}
search(query) {
const cacheKey = `plex/${this.cacheTags.search}:${query}`
const url = `http://${this.plexIP}:${this.plexPort}/hubs/search?query=${query}`
const cacheKey = `${this.cacheTags.search}:${query}`
const url = `http://${this.plexIP}:${this.plexPort}/hubs/search?query=${encodeURIComponent(query)}`
const options = {
timeout: 20000,
headers: { 'Accept': 'application/json' }
@@ -141,8 +142,8 @@ class Plex {
.then(resolve) // if found in cache resolve
.catch(() => fetch(url, options)) // else fetch fresh data
.then(successfullResponse)
.then(results => this.cache.set(cacheKey, results, 21600))
.then(this.mapResults)
.then(results => this.cache.set(cacheKey, results, 600))
.then(resolve)
.catch(error => {
if (error != undefined && error.type === 'request-timeout') {

View File

@@ -205,7 +205,7 @@ class TMDB {
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query)
.then(response => this.cache.set(cacheKey, response, 10800))
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, 'movie'))
}
@@ -214,7 +214,7 @@ class TMDB {
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
.then(response => this.cache.set(cacheKey, response, 10800))
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, 'show'))
}