Skip to content

Commit

Permalink
Merge pull request eco-stake#72 from eco-stake/expire-image-cache
Browse files Browse the repository at this point in the history
Expire validator image cache after 3 days
  • Loading branch information
tombeynon authored Mar 7, 2022
2 parents 3d45faa + 54660dd commit fdeb614
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,32 @@ class App extends React.Component {
})
}

getValidatorImage(network, validatorAddress){
getValidatorImage(network, validatorAddress, expireCache){
const images = this.state.validatorImages[network.name] || {}
if(images[validatorAddress]){
return images[validatorAddress]
}
return localStorage.getItem(validatorAddress)
return this.getValidatorImageCache(validatorAddress, expireCache)
}

getValidatorImageCache(validatorAddress, expireCache){
const cache = localStorage.getItem(validatorAddress)
if(!cache) return

let cacheData = {}
try {
cacheData = JSON.parse(cache)
} catch {
cacheData.url = cache
}
if(!cacheData.url) return
if(!expireCache) return cacheData.url

const cacheTime = cacheData.time && new Date(cacheData.time)
if(!cacheData.time) return

const expiry = new Date() - 1000 * 60 * 60 * 24 * 3
if(cacheTime >= expiry) return cacheData.url
}

async loadValidatorImages(network, validators) {
Expand All @@ -137,7 +157,7 @@ class App extends React.Component {
}));
const calls = Object.values(validators).map(validator => {
return () => {
if(validator.description.identity && !this.getValidatorImage(network, validator.operator_address)){
if(validator.description.identity && !this.getValidatorImage(network, validator.operator_address, true)){
return fetch("https://keybase.io/_/api/1.0/user/lookup.json?fields=pictures&key_suffix=" + validator.description.identity)
.then((response) => {
return response.json();
Expand All @@ -147,7 +167,7 @@ class App extends React.Component {
this.setState((state, props) => ({
validatorImages: _.set(state.validatorImages, [network.name, validator.operator_address], imageUrl)
}));
localStorage.setItem(validator.operator_address, imageUrl)
localStorage.setItem(validator.operator_address, JSON.stringify({url: imageUrl, time: +new Date()}))
}
}, error => { })
}else{
Expand Down

0 comments on commit fdeb614

Please sign in to comment.