Skip to content

Commit

Permalink
added caching wrapper function
Browse files Browse the repository at this point in the history
  • Loading branch information
ARtheboss committed Oct 17, 2023
1 parent 7b5b3ea commit 8ca463c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 2 additions & 11 deletions backend/src/modules/catalog/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ function matchCsCourseId(id: any) {
}
}

export async function getCatalog(term: TermInput, info: GraphQLResolveInfo): Promise<CatalogItem[] | null> {

const children = getChildren(info)

const cacheData = await redis.get("getCatalog", [termToString(term), children.includes("gradeAverage").toString()]);
if(cacheData){
return cacheData;
}
export async function getCatalog(term: TermInput, gradeAverageIncluded: boolean): Promise<CatalogItem[] | null> {

let classes = await ClassModel
.find({
Expand Down Expand Up @@ -74,7 +67,7 @@ export async function getCatalog(term: TermInput, info: GraphQLResolveInfo): Pro
const gradesMap: { [key: string]: GradeType[] } = {}
courses.forEach(c => gradesMap[getCourseKey(c)] = [])

if (children.includes("gradeAverage")) {
if (gradeAverageIncluded) {
const grades = await GradeModel.find(
{
/*
Expand Down Expand Up @@ -127,8 +120,6 @@ export async function getCatalog(term: TermInput, info: GraphQLResolveInfo): Pro
}

const ans = Object.values(catalog);

redis.set('getCatalog', [termToString(term), children.includes("gradeAverage").toString()], ans);

return ans as CatalogItem[];
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/modules/catalog/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CatalogModule } from "./generated-types/module-types";
import { getCatalog, getClass, getClassById, getClassSections, getCourse, getCourseById, getCourseClasses, getCourseList, getCrossListings, getPrimarySection, getSection } from "./controller"
import { cache } from "../../redis";
import { getChildren } from "../../utils/graphql";

const resolvers: CatalogModule.Resolvers = {
Query: {
catalog: (_, args, __, info) => getCatalog(args.term, info),
catalog: (_, args, __, info) => cache(getCatalog, args.term, getChildren(info).includes("gradeAverage")),
course: (_, args) => getCourse(args.subject, args.courseNumber, args.term),
class: (_, args) => getClass(args.subject, args.courseNumber, args.term, args.classNumber),
section: (_, args) => getSection(args.subject, args.courseNumber, args.term, args.classNumber, args.sectionNumber),
Expand Down
20 changes: 18 additions & 2 deletions backend/src/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class RedisConnection {

async load() {
this.client = createClient({ url: config.REDIS_URI });
await this.client.connect();
this.client.on('error', (err) => console.log('Redis Client Error', err));
};
isActive() : boolean {
Expand Down Expand Up @@ -34,7 +35,22 @@ class RedisConnection {
}
}

let redis = new RedisConnection();
const redis = new RedisConnection();

export {redis};
async function cache(controller : Function, ...args: any[]) {

const cacheData = await redis.get("getCatalog", args.map((v) => JSON.stringify(v)));
if(cacheData){
return cacheData;
}

const resp = await controller(...args);

redis.set("getCatalog", args.map((v) => JSON.stringify(v)), resp);

return resp;

}

export { redis, cache };

0 comments on commit 8ca463c

Please sign in to comment.