question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add entries to internal cache

See original GitHub issue

I’m using a store to fetch entities organised in a tree structure. Each entity is identified by a path and can either be, let’s say a folder or a file (but I don’t know which one in advance).

To save network requests, when I fetch a folder, the response already includes the files it contains. What I’d like to do is to store those files in the store’s internal cache against their respective paths, so that if they are ever requested directly, they do not get fetched again.

Right now, I’m achieving this behaviour by wrapping the fetch function I’m passing to the store and maintaining my own child cache:

const childCache = new Map<string, Entity>();

const store = createFetchStore(async (path: string) => {
  const cachedEntity = childCache.get(path);
  if (cachedEntity) {
    return cachedEntity;
  }

  const entity = await fetchEntity(path);

  if (isFolder(entity)) {
    // Cache children ...
    entity.children.forEach((child) => {
      // ... except sub-folders, since we do want those to be re-fetched fully (with their children)
      if (!isFolder(child)) {
        childCache.set(child.path, child);
      }
    });
  }

  return entity;
});

This code could be simplified a lot if I had a way to add entries to the store’s internal cache. My initial thought was that store.prefetch() could perhaps receive a resolved value as second argument. 🤷

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

0reactions
axelboccommented, Aug 2, 2022

Works like a charm, thanks a lot! ✨

Read more comments on GitHub >

github_iconTop Results From Across the Web

Caching Objects in Android Internal Storage
The list that should be saved to internal storage. List<Entry> entries = new ArrayList<Entry>();. entries.add( new Entry( "House" ));.
Read more >
Why does Cache.Add return an object that represents the ...
The "second" call to .Add returns what is currently in the Cache under our key and fails to update the entry! Share.
Read more >
Cache internals | Apigee Edge
Because the persistent cache is shared across message processors (even in different regions), cache entries are available regardless of which node receives a ......
Read more >
Cache in-memory in ASP.NET Core - Microsoft Learn
When one cache entry is used to create another, the child copies the parent entry's expiration tokens and time-based expiration settings.
Read more >
org.eclipse.core.internal.utils.Cache$Entry java code examples
Best Java code snippets using org.eclipse.core.internal.utils.Cache$Entry (Showing top 18 results out of 315) · Most used methods · Popular in Java.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found