Caching bandcamp albums call.

This commit is contained in:
Bradley Shellnut 2023-02-17 16:23:28 -08:00
parent 073988acf5
commit c12966c893
3 changed files with 18 additions and 3 deletions

View file

@ -16,6 +16,7 @@
<h3>
<a
target="_blank"
title={`Link to ${article.title}`}
aria-label={`Link to ${article.title}`}
href={article.url.toString()}
rel="noreferrer"

View file

@ -1,9 +1,22 @@
import { BANDCAMP_USERNAME } from '$env/static/private';
import { BANDCAMP_USERNAME, USE_REDIS_CACHE } from '$env/static/private';
import scrapeIt from 'scrape-it';
import { redis } from '../server/redis';
import type { Album } from '../types/album';
export async function fetchBandcampAlbums() {
try {
if (USE_REDIS_CACHE) {
const cached = await redis.get('bandcampAlbums');
if (cached) {
const response = JSON.parse(cached);
console.log(`Cache hit!`);
const ttl = await redis.ttl('bandcampAlbums');
return response;
}
}
const { data } = await scrapeIt(`https://bandcamp.com/${BANDCAMP_USERNAME}`, {
collectionItems: {
listItem: '.collection-item-container',
@ -30,6 +43,9 @@ export async function fetchBandcampAlbums() {
// console.log(`Albums ${JSON.stringify(albums)}`);
if (albums && albums?.length > 0) {
if (USE_REDIS_CACHE) {
redis.set('bandcampAlbums', JSON.stringify(albums), 'EX', 43200);
}
return albums;
} else {
return [];

View file

@ -29,7 +29,6 @@
<div>
<h1>Hello! I'm Bradley Shellnut.</h1>
</div>
<div>
<p>
I'm a full stack software engineer currently working on Java Spring, PostgreSQL, and React / Angular JS.
</p>
@ -69,7 +68,6 @@
</a>
, or read more <a href="/about">about me</a>.
</p>
</div>
<div class="social-info">
<Bandcamp {albums} />
<Articles {articles} {totalArticles} compact={true} />