Skip to content

Commit

Permalink
(Ajout) Le caching version 0.5 est là. Principalement pour du renderi…
Browse files Browse the repository at this point in the history
…ng. Si une méthode est appeler plus d'une fois pendant le render, la cache renvoit le query cached.
  • Loading branch information
mamarmite committed Jul 16, 2024
1 parent d19c858 commit bbbb7db
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
8 changes: 6 additions & 2 deletions pages/categories/[category]/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import EntitiesGrid from "@/DataTypes/Entity/layouts/EntitiesGrid";
import {getTitle} from "@/DataTypes/MetaData/MetaTitle";
import {getType, TYPE_TAXONOMY} from "@/DataTypes/Entity/Types";
import PageMeta from "@/src/common/PageMeta/PageMeta";

import {getBadgesInfo} from "@/DataTypes/Badges/BadgesSection";

export async function getServerSideProps(context) {
const { slug, category } = context.params;
Expand All @@ -30,11 +30,14 @@ export async function getServerSideProps(context) {
method: 'GET',
});

const badgesInfo = await getBadgesInfo();

if(typeof taxonomy.data._id === 'undefined' || entities.data._id)
return { notFound: true };
return { props: {
taxonomy: taxonomy.data,
data: entities.data
data: entities.data,
badgesInfo: badgesInfo
} };
}

Expand Down Expand Up @@ -109,6 +112,7 @@ const TaxonomiesSinglePage = (props) => {
columnClass={"col-12 col-sm-6 col-lg-4 col-xl-3 g-4"}
feed={data}
noResult={"Aucune entité n’a été lié à cette catégorie pour le moment"}
badgesInfo={props.badgesInfo}
/>

<Modal {...props}>
Expand Down
3 changes: 2 additions & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {clientSideExternalApiRequest, useHttpClient} from '@/src/hooks/http-hook

//Context
import {getType} from "@/DataTypes/Entity/Types";
import { useAuth } from '@/src/authentification/context/auth-context';
import {useAuth} from '@/src/authentification/context/auth-context';

//Images
import backgroundImg from '@/public/general_images/Fusee_Pointilles1.svg'
Expand Down Expand Up @@ -226,6 +226,7 @@ export default HomePage;

//Load badges Info
export async function getServerSideProps() {

const badgeInfo = await getBadgesInfo();
return {
props: {
Expand Down
14 changes: 9 additions & 5 deletions src/DataTypes/Badges/BadgesSection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useEffect, useState} from "react";
import SingleInfo from "../common/layouts/SingleInfo/SingleInfo";
import {lang} from "@/src/common/Data/GlobalConstants";
import {externalApiCache, lang} from "@/src/common/Data/GlobalConstants";
import {clientSideExternalApiRequest} from '@/src/hooks/http-hook';
import Tip from "@/src/common/FormElements/Tip/Tip";

Expand Down Expand Up @@ -72,8 +72,12 @@ export default BadgesSection;


export const getBadgesInfo = async () => {
return await clientSideExternalApiRequest(
'/info/badges',
{ method: 'GET' }
);
if (!externalApiCache.has("badgesInfo)")) {
const badges = await clientSideExternalApiRequest(
'/info/badges',
{ method: 'GET' }
);
externalApiCache.set("badgesInfo", badges);
}
return externalApiCache.get("badgesInfo");
}
67 changes: 67 additions & 0 deletions src/common/Data/Caching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Store request and data here to avoid multiple fetch.
*/
class Caching {

cacheObjects;

constructor() {
this.cacheObjects = {};
console.log("Caching initiated");
}

/**
* register the key into a property and set the key's value into the cacheObject
* @param key
* @param value
*/
set(key, value) {
this.register(key, value);
this.cacheObjects[key] = value;
}

/**
* Basic get from string
* @param key {string}
* @return {*|null}
*/
get(key) {
if (this.has(key)) {
return this.cacheObjects[key];
}
return null;
}

/**
* boolean check if the key have allready a value.
* @param key {string}
* @return {boolean}
*/
has(key) {
console.log("has", key, (key in this.cacheObjects), "have a value", typeof this.cacheObjects[key]);
return (key in this.cacheObjects);
}

/**
* Register the key as a property of the caching object assigning to the that class get / set.
* @param key {string} the string value of the cache elements.
* @param value {*} what need to be cached
* @return {*}
*/
register (key, value) {
if (!this[key]) {
Object.defineProperty(this, key, {
get() {
return this.get(key);
},
set(newvalue) {
this.set(key, newvalue);
},
enumerable: true,
configurable: true,
});
}
}
}

export default Caching;
5 changes: 4 additions & 1 deletion src/common/Data/GlobalConstants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import StringDictionary from "@/src/languages/StringDictionary";
import Caching from "@/common/Data/Caching";

/**
* All the constant that can be loaded and use in lots of file, that don't need to be fetch.
Expand Down Expand Up @@ -32,4 +33,6 @@ export const now = new Date();
export const modes = {
CONSULTING: "consulting",
CONTRIBUTING: "contributing"
}
}

export const externalApiCache = new Caching();

0 comments on commit bbbb7db

Please sign in to comment.