mirror of
https://github.com/BradNut/personal-website-sveltekit
synced 2025-09-08 23:20:18 +00:00
Caching bandcamp albums call.
This commit is contained in:
parent
073988acf5
commit
c12966c893
3 changed files with 18 additions and 3 deletions
|
|
@ -16,6 +16,7 @@
|
||||||
<h3>
|
<h3>
|
||||||
<a
|
<a
|
||||||
target="_blank"
|
target="_blank"
|
||||||
|
title={`Link to ${article.title}`}
|
||||||
aria-label={`Link to ${article.title}`}
|
aria-label={`Link to ${article.title}`}
|
||||||
href={article.url.toString()}
|
href={article.url.toString()}
|
||||||
rel="noreferrer"
|
rel="noreferrer"
|
||||||
|
|
|
||||||
|
|
@ -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 scrapeIt from 'scrape-it';
|
||||||
|
import { redis } from '../server/redis';
|
||||||
import type { Album } from '../types/album';
|
import type { Album } from '../types/album';
|
||||||
|
|
||||||
export async function fetchBandcampAlbums() {
|
export async function fetchBandcampAlbums() {
|
||||||
try {
|
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}`, {
|
const { data } = await scrapeIt(`https://bandcamp.com/${BANDCAMP_USERNAME}`, {
|
||||||
collectionItems: {
|
collectionItems: {
|
||||||
listItem: '.collection-item-container',
|
listItem: '.collection-item-container',
|
||||||
|
|
@ -30,6 +43,9 @@ export async function fetchBandcampAlbums() {
|
||||||
// console.log(`Albums ${JSON.stringify(albums)}`);
|
// console.log(`Albums ${JSON.stringify(albums)}`);
|
||||||
|
|
||||||
if (albums && albums?.length > 0) {
|
if (albums && albums?.length > 0) {
|
||||||
|
if (USE_REDIS_CACHE) {
|
||||||
|
redis.set('bandcampAlbums', JSON.stringify(albums), 'EX', 43200);
|
||||||
|
}
|
||||||
return albums;
|
return albums;
|
||||||
} else {
|
} else {
|
||||||
return [];
|
return [];
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@
|
||||||
<div>
|
<div>
|
||||||
<h1>Hello! I'm Bradley Shellnut.</h1>
|
<h1>Hello! I'm Bradley Shellnut.</h1>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
|
||||||
<p>
|
<p>
|
||||||
I'm a full stack software engineer currently working on Java Spring, PostgreSQL, and React / Angular JS.
|
I'm a full stack software engineer currently working on Java Spring, PostgreSQL, and React / Angular JS.
|
||||||
</p>
|
</p>
|
||||||
|
|
@ -69,7 +68,6 @@
|
||||||
</a>
|
</a>
|
||||||
, or read more <a href="/about">about me</a>.
|
, or read more <a href="/about">about me</a>.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
|
||||||
<div class="social-info">
|
<div class="social-info">
|
||||||
<Bandcamp {albums} />
|
<Bandcamp {albums} />
|
||||||
<Articles {articles} {totalArticles} compact={true} />
|
<Articles {articles} {totalArticles} compact={true} />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue