Skip to content

Commit

Permalink
feat(gogoanime): Add redis cache (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
hase0278 authored Mar 6, 2024
1 parent 26fbd3c commit 07c5e7b
Showing 1 changed file with 95 additions and 24 deletions.
119 changes: 95 additions & 24 deletions src/routes/anime/gogoanime.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
import { ANIME } from '@consumet/extensions';
import { StreamingServers } from '@consumet/extensions/dist/models';
import cache from '../../utils/cache';
import { redis } from '../../main';
import { Redis } from 'ioredis';

const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const gogoanime = new ANIME.Gogoanime();
const redisCacheTime = 60 * 60;
const redisPrefix = 'gogoanime:';

fastify.get('/', (_, rp) => {
rp.status(200).send({
Expand All @@ -30,7 +35,12 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const query = (request.params as { query: string }).query;
const page = (request.query as { page: number }).page || 1;

const res = await gogoanime.search(query, page);
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}search;${page};${query}`,
async () => await gogoanime.search(query, page),
redisCacheTime,
) : await gogoanime.search(query, page);

reply.status(200).send(res);
});
Expand All @@ -39,9 +49,16 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const id = decodeURIComponent((request.params as { id: string }).id);

try {
const res = await gogoanime
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}info;${id}`,
async () => await gogoanime
.fetchAnimeInfo(id)
.catch((err) => reply.status(404).send({ message: err }));
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : gogoanime
.fetchAnimeInfo(id)
.catch((err) => reply.status(404).send({ message: err }));

reply.status(200).send(res);
} catch (err) {
Expand All @@ -53,12 +70,19 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

fastify.get('/genre/:genre', async (request: FastifyRequest, reply: FastifyReply) => {
const genre = (request.params as { genre: string }).genre;
const page = (request.query as { page: number }).page;
const page = (request.query as { page: number }).page ?? 1;

try {
const res = await gogoanime
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}genre;${page};${genre}`,
async () => await gogoanime
.fetchGenreInfo(genre, page)
.catch((err) => reply.status(404).send({ message: err }));
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchGenreInfo(genre, page)
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch {
reply
Expand All @@ -69,9 +93,17 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

fastify.get('/genre/list', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const res = await gogoanime

const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}genre-list`,
async () => await gogoanime
.fetchGenreList()
.catch((err) => reply.status(404).send({ message: err }));
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime * 24,
) : await gogoanime
.fetchGenreList()
.catch((err) => reply.status(404).send({ message: err }));
reply.status(200).send(res);
} catch {
reply
Expand All @@ -91,9 +123,16 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
}

try {
const res = await gogoanime
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}watch;${server};${episodeId}`,
async () => await gogoanime
.fetchEpisodeSources(episodeId, server)
.catch((err) => reply.status(404).send({ message: err }));
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchEpisodeSources(episodeId, server)
.catch((err) => reply.status(404).send({ message: err }));

reply.status(200).send(res);
} catch (err) {
Expand All @@ -110,9 +149,16 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
const episodeId = (request.params as { episodeId: string }).episodeId;

try {
const res = await gogoanime
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}servers;${episodeId}`,
async () => await gogoanime
.fetchEpisodeServers(episodeId)
.catch((err) => reply.status(404).send({ message: err }));
.catch((err) => reply.status(404).send({ message: err })),
redisCacheTime,
) : await gogoanime
.fetchEpisodeServers(episodeId)
.catch((err) => reply.status(404).send({ message: err }));

reply.status(200).send(res);
} catch (err) {
Expand All @@ -125,9 +171,14 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

fastify.get('/top-airing', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page;
const page = (request.query as { page: number }).page ?? 1;

const res = await gogoanime.fetchTopAiring(page);
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}top-airing;${page}`,
async () => await gogoanime.fetchTopAiring(page),
redisCacheTime,
) : await gogoanime.fetchTopAiring(page);

reply.status(200).send(res);
} catch (err) {
Expand All @@ -139,9 +190,14 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

fastify.get('/movies', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page;
const page = (request.query as { page: number }).page ?? 1;

const res = await gogoanime.fetchRecentMovies(page);
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}movies;${page}`,
async () => await gogoanime.fetchRecentMovies(page),
redisCacheTime,
) : await gogoanime.fetchRecentMovies(page);

reply.status(200).send(res);
} catch (err) {
Expand All @@ -153,9 +209,14 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {

fastify.get('/popular', async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page;
const page = (request.query as { page: number }).page ?? 1;

const res = await gogoanime.fetchPopular(page);
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}popular;${page}`,
async () => await gogoanime.fetchPopular(page),
redisCacheTime,
) : await gogoanime.fetchPopular(page);

reply.status(200).send(res);
} catch (err) {
Expand All @@ -169,10 +230,15 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
'/recent-episodes',
async (request: FastifyRequest, reply: FastifyReply) => {
try {
const type = (request.query as { type: number }).type;
const page = (request.query as { page: number }).page;
const type = (request.query as { type: number }).type ?? 1;
const page = (request.query as { page: number }).page ?? 1;

const res = await gogoanime.fetchRecentEpisodes(page, type);
const res = redis ? await cache.fetch(
redis as Redis,
`${redisPrefix}recent-episodes;${page};${type}`,
async () => await gogoanime.fetchRecentEpisodes(page, type),
redisCacheTime,
) : await gogoanime.fetchRecentEpisodes(page, type);

reply.status(200).send(res);
} catch (err) {
Expand All @@ -186,9 +252,14 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
'/anime-list',
async (request: FastifyRequest, reply: FastifyReply) => {
try {
const page = (request.query as { page: number }).page;
const page = (request.query as { page: number }).page ?? 1;

const res = await gogoanime.fetchAnimeList(page);
const res = redis ? await cache.fetch(
redis as Redis,
`gogoanime:anime-list;${page}`,
async () => await gogoanime.fetchAnimeList(page),
redisCacheTime,
) : await gogoanime.fetchAnimeList(page);

reply.status(200).send(res);
} catch (err) {
Expand All @@ -200,4 +271,4 @@ const routes = async (fastify: FastifyInstance, options: RegisterOptions) => {
);
};

export default routes;
export default routes;

0 comments on commit 07c5e7b

Please sign in to comment.