Skip to content

Commit

Permalink
fix: cache all facebook requests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentuso committed Mar 23, 2021
1 parent 5b742c0 commit ea21c88
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
51 changes: 51 additions & 0 deletions packages/@uppy/companion/src/server/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,54 @@ const retryWithDelay = (options) => {
}

module.exports.retryWithDelay = retryWithDelay

class TemporaryCache {
constructor () {
this.entries = {}
}

serialize () {
return JSON.stringify(this.entries)
}

/**
* @param key
* @param value
* @param ttl - time to live in ms, default 1 minute
*/
add (key, value, ttl = 60000) {
const time = Date.now()
for (const entryKey of Object.keys(this.entries)) {
const checkEntry = this.entries[entryKey]
if (time > checkEntry.exp) {
this.delete(entryKey)
}
}

this.entries[key] = {
exp: Date.now() + ttl,
value
}
}

get (key) {
const entry = this.entries[key]

if (!entry) {
return undefined
}

if (Date.now() > entry.exp) {
this.delete(key)
return undefined
}

return entry.value
}

delete (key) {
delete this.entries[key]
}
}

exports.TemporaryCache = TemporaryCache
65 changes: 55 additions & 10 deletions packages/@uppy/companion/src/server/provider/facebook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { getURLMeta } = require('../../helpers/request')
const logger = require('../../logger')
const adapter = require('./adapter')
const { ProviderApiError, ProviderAuthError } = require('../error')
const { TemporaryCache } = require('../../helpers/utils')

const cache = new TemporaryCache()

/**
* Adapter for API https://developers.facebook.com/docs/graph-api/using-graph-api/
Expand Down Expand Up @@ -47,13 +50,31 @@ class Facebook extends Provider {
return done(err)
} else {
this._getUsername(token, (err, username) => {
err ? done(err) : done(null, this.adaptData(body, username, directory, query))
if (err) {
return done(err)
}

const items = adapter.getItemSubList(body)
for (const item of items) {
if (!item.images || !item.images.length) {
continue
}
const urlKey = `${token}:${adapter.getItemId(item)}:mediaUrl`
cache.add(urlKey, this._getMediaUrl(item.images), 5 * 60 * 10000)
}
done(null, this.adaptData(body, username, directory, query))
})
}
})
}

_getUsername (token, done) {
const key = `${token}:username`
const cached = cache.get(key)
if (cached) {
return done(null, cached)
}

this.client
.get('me')
.qs({ fields: 'email' })
Expand All @@ -64,18 +85,26 @@ class Facebook extends Provider {
logger.error(err, 'provider.facebook.user.error')
return done(err)
} else {
cache.add(key, body.email, 5 * 60 * 1000)
done(null, body.email)
}
})
}

_getMediaUrl (body) {
const sortedImages = adapter.sortImages(body.images)
_getMediaUrl (images) {
const sortedImages = adapter.sortImages(images)
return sortedImages[sortedImages.length - 1].source
}

download ({ id, token }) {
return new Promise((resolve, reject) => {
const key = `${token}:${id}:mediaUrl`
const cached = cache.get(key)
if (cached) {
resolve(request(cached))
return
}

this.client
.get(`https://graph.facebook.com/${id}`)
.qs({ fields: 'images' })
Expand All @@ -88,7 +117,9 @@ class Facebook extends Provider {
return
}

resolve(request(this._getMediaUrl(body)))
const url = this._getMediaUrl(body.images)
cache.add(key, url)
resolve(request(url))
})
})
}
Expand All @@ -101,6 +132,25 @@ class Facebook extends Provider {
}

size ({ id, token }, done) {
const sizeKey = `${token}:${id}:size`
const cachedSize = cache.get(sizeKey)
if (cachedSize) {
return done(null, cachedSize)
}

const getSize = (url) => getURLMeta(url)
.then(({ size }) => done(null, size))
.catch((err) => {
logger.error(err, 'provider.facebook.size.error')
done()
})

const urlKey = `${token}:${id}:mediaUrl`
const cachedUrl = cache.get(urlKey)
if (cachedUrl) {
return getSize(cachedUrl)
}

return this.client
.get(`https://graph.facebook.com/${id}`)
.qs({ fields: 'images' })
Expand All @@ -112,12 +162,7 @@ class Facebook extends Provider {
return done(err)
}

getURLMeta(this._getMediaUrl(body))
.then(({ size }) => done(null, size))
.catch((err) => {
logger.error(err, 'provider.facebook.size.error')
done()
})
return getSize(this._getMediaUrl(body.images))
})
}

Expand Down

0 comments on commit ea21c88

Please sign in to comment.