Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge branch 'save-state-to-cache'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Mar 16, 2022
2 parents a78f1e1 + b90723f commit 4575615
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datocms-client",
"version": "3.5.15",
"version": "3.5.16-0",
"description": "DatoCMS API client and CLI tool",
"browser": "dist/client.js",
"main": "lib/index.js",
Expand Down
59 changes: 59 additions & 0 deletions src/local/EntitiesRepo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import JsonApiEntity from './JsonApiEntity';

const entitiesToStorePerCacheKey = 5000;

function payloadEntities(payload) {
const accumulator = [];

Expand All @@ -26,6 +28,63 @@ export default class EntitiesRepo {
this.upsertEntities(...payloads);
}

async saveStateToCache(cache, cachePrefixKey) {
const entityTypes = Object.keys(this.entities);

const manifest = { entityTypeChunkKeys: {} };

for (const entityType of entityTypes) {
const entities = Object.values(this.entities[entityType]);

for (
let i = 0, chunkIndex = 0;
i < entities.length;
i += entitiesToStorePerCacheKey, chunkIndex += 1
) {
const chunkCacheKey = `${cachePrefixKey}--${entityType}-${chunkIndex}`;

manifest.entityTypeChunkKeys[entityType] =
manifest.entityTypeChunkKeys[entityType] || [];
manifest.entityTypeChunkKeys[entityType].push(chunkCacheKey);

await cache.set(
chunkCacheKey,
entities
.slice(i, i + entitiesToStorePerCacheKey)
.map(entity => entity.payload),
);
}
}

await cache.set(cachePrefixKey, manifest);
}

async loadStateFromCache(cache, cachePrefixKey) {
const manifest = await cache.get(cachePrefixKey);

if (!manifest) {
return;
}

this.entities = {};

for (const [entityType, entityTypeChunkKeys] of Object.entries(
manifest.entityTypeChunkKeys,
)) {
this.entities[entityType] = {};

for (const entityTypeChunkKey of entityTypeChunkKeys) {
const chunkEntities = await cache.get(entityTypeChunkKey);
chunkEntities.forEach(entityPayload => {
this.entities[entityType][entityPayload.id] = new JsonApiEntity(
entityPayload,
this,
);
});
}
}
}

serializeState() {
return Object.entries(this.entities).reduce((acc, [type, entitiesById]) => {
return {
Expand Down
11 changes: 2 additions & 9 deletions src/local/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ export default class Loader {
}

saveStateToCache(cache) {
return cache.set(this.cacheKey(), this.entitiesRepo.serializeState());
return this.entitiesRepo.saveStateToCache(cache, this.cacheKey());
}

loadStateFromCache(cache) {
return cache.get(this.cacheKey()).then(serializedState => {
if (!serializedState) {
return false;
}

this.entitiesRepo.loadState(serializedState);
return true;
});
return this.entitiesRepo.loadStateFromCache(cache, this.cacheKey());
}

loadSchema() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/seoTagsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export const builders = {
ogTag('og:image:height', finalUpload.height),
altValue && ogTag('og:image:alt', altValue),
altValue && ogTag('twitter:image:alt', altValue),
];
].filter(x => !!x);
},
};

Expand Down

0 comments on commit 4575615

Please sign in to comment.