Skip to content

Commit

Permalink
feat(bulk-req): bulk req cache fix for multitenant applications (#95)
Browse files Browse the repository at this point in the history
* add console

* chore(release): 2.4.1-bulk-api-test.0

* feat(cache): Add caching layer for bulk request API

* chore(release): 2.5.0-bulk-api-test.0

* fix cache

* bulk req fix

* fix(ttl): add ttl

* chore(release): 2.5.0-bulk-api-test.1

* remove console logs

* revert package version change
  • Loading branch information
Jeevan-Kishore authored Jun 3, 2024
1 parent ed684e5 commit 926ef3e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
4 changes: 3 additions & 1 deletion cache-constant.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const CACHE_TIME = 240000;
const MAX_CACHE = 200;
const BULK_REQ_TTL_CACHE = 10000000;
const ENABLE_TTL_CACHE = false;

module.exports = {
CACHE_TIME,
MAX_CACHE,
ENABLE_TTL_CACHE
ENABLE_TTL_CACHE,
BULK_REQ_TTL_CACHE
};
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const hash = require("object-hash");
const { createCache, memoryStore } = require("cache-manager");

const { DEFAULT_REQUEST_TIMEOUT, ENABLE_AXIOS } = require("./constants");
const { CACHE_TIME, MAX_CACHE, ENABLE_TTL_CACHE } = require("./cache-constant");
const { CACHE_TIME, MAX_CACHE, ENABLE_TTL_CACHE, BULK_REQ_TTL_CACHE } = require("./cache-constant");

const memoryStoreInit = memoryStore();
const memoryCache = createCache(memoryStoreInit, {
Expand Down Expand Up @@ -330,8 +330,8 @@ class Collection extends BaseAPI {
- Cricket `(Level 3)`
- Football `(Level 3)`
- Tennis `(Level 3)`
In the above example with nestedCollectionLimit: {ThreeColGrid: [2, 3, 4]}, Cricket collection will fetch 2 items, Football will fetch 5 items and Tennis will fetch 4 items. (default: defaultNestedLimit || 40)
* @param {Object} options.collectionOfCollectionsIndexes It accepts array of indexes(collection's position) to fetch collection of collection of items when the depth is 1. (Ex: collectionOfCollectionsIndexes: [0, 4]).
In the above example with nestedCollectionLimit: {ThreeColGrid: [2, 3, 4]}, Cricket collection will fetch 2 items, Football will fetch 5 items and Tennis will fetch 4 items. (default: defaultNestedLimit || 40)
* @param {Object} options.collectionOfCollectionsIndexes It accepts array of indexes(collection's position) to fetch collection of collection of items when the depth is 1. (Ex: collectionOfCollectionsIndexes: [0, 4]).
eg:
- Home `(Level 1)`
- Sports Row `(Level 2)`
Expand All @@ -341,10 +341,10 @@ class Collection extends BaseAPI {
- Movie `(Level 3)`
- Song `(Level 3)`
In the above example if we need to fetch the stories from `Sports Row` child collection we need to pass collectionOfCollectionsIndexes : [0], where 0 is the position of collection Sports Row and stories from Cricket and Football will be fetched
* @param {Object} options.customLayouts It accepts an array of objects to fetch the custom storyLimit and custom nestedCollectionLimit of custom layouts. (Ex: customLayouts: [{layout: "ArrowThreeColGrid", storyLimit: 9}, {layout: "ArrowTwoColTenStories", storyLimit: 2, nestedCollectionLimit: [5,5]}]).
* @return {(Promise<Collection|null>)}
* @see {@link https://developers.quintype.com/swagger/#/collection/get_api_v1_collections__slug_ GET /api/v1/collections/:slug} API documentation for a list of parameters and fields
*/
* @param {Object} options.customLayouts It accepts an array of objects to fetch the custom storyLimit and custom nestedCollectionLimit of custom layouts. (Ex: customLayouts: [{layout: "ArrowThreeColGrid", storyLimit: 9}, {layout: "ArrowTwoColTenStories", storyLimit: 2, nestedCollectionLimit: [5,5]}]).
* @return {(Promise<Collection|null>)}
* @see {@link https://developers.quintype.com/swagger/#/collection/get_api_v1_collections__slug_ GET /api/v1/collections/:slug} API documentation for a list of parameters and fields
*/
static getCollectionBySlug(client, slug, params, options = {}) {
const {
depth = DEFAULT_DEPTH,
Expand Down Expand Up @@ -1043,10 +1043,6 @@ class Client {

async getInBulk(requests) {
const requestHash = hash(requests);
this._cachedPostBulkLocations[requestHash] =
this._cachedPostBulkLocations[requestHash] ||
(await this._cachedPostBulkGate(requestHash, getBulkLocation.bind(this)));
return this.request(this._cachedPostBulkLocations[requestHash]);

async function getBulkLocation() {
const response = await this.request("/api/v1/bulk-request", {
Expand All @@ -1059,15 +1055,20 @@ class Client {
simple: false,
resolveWithFullResponse: true
});

if (ENABLE_AXIOS && response.statusCode === 200 && response.redirectCount > 0) {
return response.headers["content-location"];
} else if (response.statusCode === 303 && response.caseless.get("Location")) {
return response.caseless.get("Location");
if (response.statusCode === 303 && response.caseless.get("Location")) {
const contentLocation = response.caseless.get("Location");
await memoryCache.set(requestHash, contentLocation, BULK_REQ_TTL_CACHE);
return contentLocation;
} else {
throw new Error(`Could Not Convert POST bulk to a get, got status ${response.statusCode}`);
}
}

let cachedRequestHash = await memoryCache.get(requestHash);
if (!cachedRequestHash) {
cachedRequestHash = await getBulkLocation.bind(this)();
}
return this.request(cachedRequestHash);
}

getAmpStoryBySlug(slug) {
Expand Down

0 comments on commit 926ef3e

Please sign in to comment.