Skip to content

Commit

Permalink
feat: deal with cached images
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Ellison committed Jun 19, 2023
1 parent 9d4a60c commit 608f852
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions components/solutions/SolutionView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function SolutionView({
}) {


console.log('SolutionView:menuStructure: ', menuStructure)
// console.log('SolutionView:menuStructure: ', menuStructure)
const navDrawerWidth = 300;
const topBarHeight = 64;
const [menuOpen, setMenuOpen] = useState(true);
Expand Down Expand Up @@ -70,7 +70,7 @@ export function SolutionView({
}}
>
{/* {frontmatter && <ServicesHeader frontmatter={frontmatter} controlCoverage={controlCoverage} />} */}
<Typography variant="h1" component="h1" sx={{ mx: '2%' }}>{frontmatter?.title && frontmatter.title}</Typography>
<Typography variant="h1" component="h1" sx={{ pl: 0, mx: '2%' }}>{frontmatter?.title && frontmatter.title}</Typography>
{frontmatter?.format === 'presentation' && <Box sx={{ background: 'rgb(229, 246, 253)', px: '10%' }}>
<Grid container alignItems="center" spacing={1}>
<Grid item>
Expand Down
10 changes: 7 additions & 3 deletions constants/baseTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,19 @@ const baseTheme = deepmerge(
color: palette.palette.text.highlight,
},
ul: {
display: 'inline',
display: 'inline-block',
breakInside: 'avoid-column',
listStyleType: 'circle',
li: {
listStylePosition: 'inside',
"::marker": {
color: 'tertiary',
}
}
},
span: {
display: 'inline',
},
},

},
table: {
display: "inline-table",
Expand Down
6 changes: 3 additions & 3 deletions lib/content/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function getSolutions(siteConfig) {
});
});
const content = await Promise.all(solutionsContentPromises);
await cacheWrite(cacheKey, JSON.stringify(content), 600); // cache for 10 minutes
await cacheWrite(cacheKey, JSON.stringify(content), 60*60*24); // cache for 24 hours
return content
}

Expand Down Expand Up @@ -77,7 +77,7 @@ async function getKnowledge(siteConfig) {
});

const content = await Promise.all(knowledgeContentPromises);
await cacheWrite(cacheKey, JSON.stringify(content), 600); // cache for 10 minutes
await cacheWrite(cacheKey, JSON.stringify(content), 60*60*24); // cache for 24 hours
return content


Expand Down Expand Up @@ -186,6 +186,6 @@ export async function getMenuStructureSolutions(siteConfig) {


const content = { solutionMenu, chapterFiles, knowledgeFiles, designFiles: null };
await cacheWrite(cacheKey, JSON.stringify(content), 600); // cache for 10 minutes
await cacheWrite(cacheKey, JSON.stringify(content), 60*60*24); // cache for 24 hours
return content
}
2 changes: 1 addition & 1 deletion lib/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function getFileContent(owner, repo, branch, path) {
// Check if the content is in the cache
const cachedContent = await cacheRead(cacheKey);
if (cachedContent) {
// console.info('[Github][Cache][HIT]:',cacheKey )
console.info('[Github][Cache][HIT]:',cacheKey )
// If the content was found in the cache, return it
return cachedContent;
} else {
Expand Down
17 changes: 12 additions & 5 deletions lib/redis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,25 @@ export async function createRedisInstance(config = getRedisConfiguration()) {
throw new Error(`[Redis] Could not create a Redis instance`);
}
}

// Function to write data to the cache
export async function cacheWrite(key, value, ttl = null) {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}

const isBuffer = Buffer.isBuffer(value);
const stringifiedValue = isBuffer ? JSON.stringify({ buffer: [...value] }) : JSON.stringify(value);

if (ttl) {
try {
await redisInstance.set(key, value, "EX", ttl);
await redisInstance.set(key, stringifiedValue, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.set(key, value);
await redisInstance.set(key, stringifiedValue);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
Expand All @@ -104,9 +106,14 @@ export async function cacheWrite(key, value, ttl = null) {
}
}

// Function to read data from the cache
export async function cacheRead(key) {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
return await redisInstance.get(key);
const value = await redisInstance.get(key);
if (!value) return null;
const parsedValue = JSON.parse(value);

return parsedValue.buffer ? Buffer.from(parsedValue.buffer) : parsedValue;
}
2 changes: 1 addition & 1 deletion pages/api/content/[owner]/[repo].js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async function handler(req, res) {
// // console.log('api:data: ', contentType, data);
res.setHeader("Content-Type", contentType);
res.send(data);
// res.send(Buffer.from(data, "base64"));
// res.end(Buffer.from(data, "base64"));
// const content = Buffer.from(data.content ?? "", "base64").toString("utf8");
// res.status(200).json({ data });
}
Expand Down

0 comments on commit 608f852

Please sign in to comment.