Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the ttl on cached data configurable #49

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ Universal react application for the chapter website.

## Environment variables

| Name | Default | Description |
|--------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------|
| TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on |
| RAZZLE_TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on. **Set during build**. Should probably be the same as TAITAN_URL |
| RAZZLE_CALYPSO_URL | https://calypso.datasektionen.se/api | URL to get news from calypso on. **Set during build** |
| PORT | 3000 | Port to listen on |
| Name | Default | Description |
|--------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------|
| TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on |
| RAZZLE_TAITAN_URL | https://taitan.datasektionen.se | URL to get contents from taitan on. **Set during build**. Should probably be the same as TAITAN_URL |
| RAZZLE_CALYPSO_URL | https://calypso.datasektionen.se/api | URL to get news from calypso on. **Set during build** |
| TAITAN_CACHE_TTL | 3600 | Time to keep content from taitan cached in seconds. Tip: Set to 0 if using local taitan & bawang-content. |
| CALYPSO_CACHE_TTL | 30 | Time to keep news from calypso cached in seconds. Tip: Set to 0 if using local calypso. |
| PORT | 3000 | Port to listen on |

## Running
Bawang runs on Node v.10.x.x. It doesn't work on later versions (v.12.20.1)
Expand Down
3 changes: 2 additions & 1 deletion src/components/Calypso.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fetch from 'cross-fetch'
import { DataLoader } from './DataLoader'

const RAZZLE_CALYPSO_URL = process.env.RAZZLE_CALYPSO_URL || 'https://calypso.datasektionen.se/api'
const CALYPSO_CACHE_TTL = process.env.CALYPSO_CACHE_TTL ? parseInt(process.env.CALYPSO_CACHE_TTL, 10) : 30

const calypsoFetcher = url =>
fetch(url)
Expand All @@ -16,7 +17,7 @@ export const Calypso = ({ type, search, children, ttl }) =>
<DataLoader
cacheKey={`${RAZZLE_CALYPSO_URL}/${type || 'list'}${search || ''}`}
fetcher={calypsoFetcher}
ttl={ ttl || 30 }
ttl={CALYPSO_CACHE_TTL}
>
{({ data, loading, time }) => children(data) }
</DataLoader>
Expand Down
13 changes: 5 additions & 8 deletions src/components/DataLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,23 @@ export const DataLoader = withConsumer(class extends Component {

const cacheKey = this.props.cacheKey

if(cache[cacheKey]) {
if(cache[cacheKey].loading)
if (cache[cacheKey]) {
if (cache[cacheKey].loading) {
this.props.promises.push(new Promise((resolve, reject) => cache[cacheKey].waiting.push(resolve)))
else
} else {
this.props.promises.push(Promise.resolve(cache[cacheKey]))
}

return
}

console.log('fetching', cacheKey)

cache[cacheKey] = {
data: {},
cacheKey,
loading: true,
waiting: []
}

console.log('Object.keys(cache).length:', Object.keys(cache).length)

this.props.promises.push(
this.props
.fetcher(cacheKey)
Expand All @@ -68,7 +65,7 @@ export const DataLoader = withConsumer(class extends Component {

setTimeout(() => {
delete cache[cacheKey]
}, (this.props.ttl || 60) * 1000)
}, (typeof this.props.ttl === "number" ? this.props.ttl : 60) * 1000)

this.forceUpdate()

Expand Down
5 changes: 3 additions & 2 deletions src/components/Taitan.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fetch from 'cross-fetch'
import { DataLoader } from './DataLoader'

const RAZZLE_TAITAN_URL = process.env.RAZZLE_TAITAN_URL || 'https://taitan.datasektionen.se'
const TAITAN_CACHE_TTL = process.env.TAITAN_CACHE_TTL ? parseInt(process.env.TAITAN_CACHE_TTL, 10) : 60 * 60

const taitanFetcher = url =>
fetch(url)
Expand All @@ -26,11 +27,11 @@ const taitanFetcher = url =>
})
.then(res => ({ status: 200, redirect: false, ...res }))

export const Taitan = ({ pathname, children, ttl }) =>
export const Taitan = ({ pathname, children }) =>
<DataLoader
cacheKey={RAZZLE_TAITAN_URL + pathname}
fetcher={taitanFetcher}
ttl={ttl || 60 * 60}
ttl={TAITAN_CACHE_TTL}
>
{({ data, loading, error }) => {
if (!loading && data && data.redirect)
Expand Down
Loading