Skip to content

Commit

Permalink
Introduce caching to reduce dynamo reads
Browse files Browse the repository at this point in the history
  • Loading branch information
amits97 committed Nov 21, 2024
1 parent 4981897 commit 48fe34a
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 266 deletions.
66 changes: 41 additions & 25 deletions api/common/post-fuzzy-search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Fuse from "fuse.js";
import * as dynamoDbLib from "../libs/dynamodb-lib";

var lastScanDate = 0;
// scanResult is the object that FuseJS creates for the search
var scanResult;

async function getPostDetails(postId) {
const params = {
TableName: "NaadanChords",
Expand All @@ -16,33 +20,45 @@ async function getPostDetails(postId) {

export async function fuzzySearch(query) {
let allPostIds = [];
let params = {
TableName: "NaadanChords",
IndexName: "postType-updatedAt-index",
KeyConditionExpression: "postType = :postType",
ExpressionAttributeValues: {
":postType": "POST",
},
ScanIndexForward: false,
ProjectionExpression: "postId",
};
let responseFromCache = true;

let lek = "init";
while (lek) {
const data = await dynamoDbLib.call("query", params);
allPostIds.push(...data.Items);
lek = data.LastEvaluatedKey;
if (lek) params.ExclusiveStartKey = lek;
}
if (
lastScanDate === 0 ||
scanResult === undefined ||
lastScanDate + 3600000 < new Date().getTime()
) {
responseFromCache = false;
lastScanDate = new Date().getTime();

const options = {
includeScore: true,
threshold: 0.4,
isCaseSensitive: false,
keys: ["postId"],
};
let params = {
TableName: "NaadanChords",
IndexName: "postType-updatedAt-index",
KeyConditionExpression: "postType = :postType",
ExpressionAttributeValues: {
":postType": "POST",
},
ScanIndexForward: false,
ProjectionExpression: "postId",
};

let lek = "init";
while (lek) {
const data = await dynamoDbLib.call("query", params);
allPostIds.push(...data.Items);
lek = data.LastEvaluatedKey;
if (lek) params.ExclusiveStartKey = lek;
}

const options = {
includeScore: true,
threshold: 0.4,
isCaseSensitive: false,
keys: ["postId"],
};

scanResult = new Fuse(allPostIds, options);
}

const scanResult = new Fuse(allPostIds, options);
let result = scanResult.search(query, { limit: 15 });

if (result.length > 0) {
Expand All @@ -58,5 +74,5 @@ export async function fuzzySearch(query) {
result.Items.push(res);
}

return result;
return { ...result, responseFromCache };
}
Loading

0 comments on commit 48fe34a

Please sign in to comment.