Skip to content

Commit

Permalink
Add force cache update mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcorbalan committed Jun 2, 2020
1 parent 8fcca1b commit ff7adc4
Show file tree
Hide file tree
Showing 5 changed files with 436,055 additions and 233,685 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ REACT_APP_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/makerdao
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_GOV_DB_HTTP='/api/v1'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_LAST_CACHE_UPDATE='1591122639'
REACT_APP_CACHE_FORCE_UPDATE='1591122639'
REACT_APP_HOME_DATA_TTL='5'
9 changes: 6 additions & 3 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_GOV_DB_HTTP='/api/v1'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_LAST_CACHE_UPDATE='1591122639'
REACT_APP_CACHE_FORCE_UPDATE='1591122639'
REACT_APP_HOME_DATA_TTL='5'

[context.branch-deploy.environment]
Expand All @@ -27,7 +28,8 @@
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_GOV_DB_HTTP='/api/v1'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_LAST_CACHE_UPDATE='1591122639'
REACT_APP_CACHE_FORCE_UPDATE='1591122639'
REACT_APP_HOME_DATA_TTL='5'

[context.deploy-preview.environment]
Expand All @@ -36,5 +38,6 @@
REACT_APP_MKR_GRAPH_HTTP='https://api.thegraph.com/subgraphs/name/protofire/mkr-registry'
REACT_APP_GOV_DB_HTTP='/api/v1'
REACT_APP_ETHERSCAN_API_KEY='XQ2QTEM7H4KX7AQTE9JWXD3HWTTZ46TTU9'
REACT_APP_LAST_CACHE_UPDATE='1587733935'
REACT_APP_LAST_CACHE_UPDATE='1591122639'
REACT_APP_CACHE_FORCE_UPDATE='1591122639'
REACT_APP_HOME_DATA_TTL='5'
11 changes: 9 additions & 2 deletions src/containers/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,28 @@ const items = [
]

const LAST_CACHE_UPDATE = process.env.REACT_APP_LAST_CACHE_UPDATE || 0
const CACHE_FORCE_UPDATE = process.env.REACT_APP_CACHE_FORCE_UPDATE || 0

function App() {
const [cacheInitialized, setCacheInitialized] = useState(false)
const { data, ...result } = useQuery(GOVERNANCE_INFO_QUERY)

useEffect(() => {
store.getItem<any>('last-update').then(value => {
if (!value || isBefore(fromUnixTime(value), fromUnixTime(Number(LAST_CACHE_UPDATE)))) {
Promise.all([store.getItem<any>('last-update'), store.getItem<any>('forced-update')]).then(([last, forced]) => {
if (
!last ||
isBefore(fromUnixTime(last), fromUnixTime(Number(LAST_CACHE_UPDATE))) ||
!forced ||
isBefore(fromUnixTime(forced), fromUnixTime(Number(CACHE_FORCE_UPDATE)))
) {
import(`../../data/maker-governance-${LAST_CACHE_UPDATE}.json`).then(data => {
Promise.all(
Object.keys(data.default).map(key => {
return store.setItem(key, data.default[key])
}),
).then(() => {
store.setItem('last-update', (Date.now() / 1000).toFixed(0))
store.setItem('forced-update', (Date.now() / 1000).toFixed(0))
setCacheInitialized(true)
})
})
Expand Down
Loading

0 comments on commit ff7adc4

Please sign in to comment.