Skip to content

Commit

Permalink
Implement simple LRU cache for publisher info lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
emerick committed Jan 18, 2021
1 parent 675bc91 commit 0289262
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
38 changes: 38 additions & 0 deletions scripts/brave_rewards/publisher/common/lruCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

export class LruCache<T> {

private values: Map<string, T> = new Map<string, T>()
private maxEntries: number

constructor (maxEntries: number) {
this.maxEntries = maxEntries
}

public get (key: string): T | null {
if (!key) {
return null
}

const entry = this.values.get(key)
if (!entry) {
return null
}

this.values.delete(key)
this.values.set(key, entry)

return entry
}

public put (key: string, value: T) {
if (this.values.size >= this.maxEntries) {
const key = this.values.keys().next().value
this.values.delete(key)
}

this.values.set(key, value)
}
}
70 changes: 44 additions & 26 deletions scripts/brave_rewards/publisher/twitter/publisherInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import { LruCache } from '../common/lruCache'
import { getPort } from '../common/messaging'

import * as commonUtils from '../common/utils'
Expand All @@ -10,6 +11,41 @@ import * as api from './api'
import * as types from './types'
import * as utils from './utils'

const userCache = new LruCache<any>(128)

const savePublisherVisit = (screenName: string, userDetails: any) => {
if (!screenName || !userDetails) {
return
}

const userId = userDetails.id_str
const publisherKey =
commonUtils.buildPublisherKey(types.mediaType, userId)
const publisherName = screenName
const mediaKey = commonUtils.buildMediaKey(types.mediaType, screenName)
const favIconUrl =
userDetails.profile_image_url_https.replace('_normal', '')

const profileUrl = utils.buildProfileUrl(screenName, userId)

const port = getPort()
if (!port) {
return
}

port.postMessage({
type: 'SavePublisherVisit',
mediaType: types.mediaType,
data: {
url: profileUrl,
publisherKey,
publisherName,
mediaKey,
favIconUrl
}
})
}

const sendForExcludedPage = () => {
const url = `https://${types.mediaDomain}`
const publisherKey = types.mediaDomain
Expand Down Expand Up @@ -41,34 +77,16 @@ const sendForStandardPage = (url: URL) => {
return
}

const userDetails = userCache.get(screenName)
if (userDetails) {
savePublisherVisit(screenName, userDetails)
return
}

api.getUserDetails(screenName)
.then((userDetails: any) => {
const userId = userDetails.id_str
const publisherKey =
commonUtils.buildPublisherKey(types.mediaType, userId)
const publisherName = screenName
const mediaKey = commonUtils.buildMediaKey(types.mediaType, screenName)
const favIconUrl =
userDetails.profile_image_url_https.replace('_normal', '')

const profileUrl = utils.buildProfileUrl(screenName, userId)

const port = getPort()
if (!port) {
return
}

port.postMessage({
type: 'SavePublisherVisit',
mediaType: types.mediaType,
data: {
url: profileUrl,
publisherKey,
publisherName,
mediaKey,
favIconUrl
}
})
userCache.put(screenName, userDetails)
savePublisherVisit(screenName, userDetails)
})
.catch(error => {
console.error(`Failed to fetch user details for ${screenName}: ${error.message}`)
Expand Down

0 comments on commit 0289262

Please sign in to comment.