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

Commit

Permalink
Add new methods load/save from cache in chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Mar 16, 2022
1 parent a78f1e1 commit 3116823
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
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

0 comments on commit 3116823

Please sign in to comment.