Clearing Cache#

labapi caches each container’s children and each page’s entries, so repeated reads make no API calls.

Refreshing Tree and Entry Caches#

Tree nodes (notebooks, directories, and pages) provide a refresh() method that clears cached children for containers or cached entries for pages, so the next property access fetches them again.

Refreshing a Page#

# Get a page
page = notebook.traverse("My Folder/My Page")

# Work with entries
entries = page.entries  # Fetches from API and caches

# Some time later, you want to see if new entries were added
page.refresh()

# Next access will fetch fresh data from the API
entries = page.entries  # Re-fetches from API

Refreshing a Directory or Notebook#

# Get a directory
directory = notebook.traverse("My Folder")

# Access children
children = directory.children  # Fetches and caches

# Refresh to see if new pages/subdirectories were added
directory.refresh()

# Next access gets fresh data
children = directory.children  # Re-fetches from API

When Local Mutations Do Not Need Refresh#

Create, rename, move, and delete update the in-memory tree once the API call succeeds, so you do not need refresh() afterwards.

create() appends the new page or directory to its parent, and node.name = "..." renames the object in memory. move_to() updates the node’s parent and both containers’ child lists, and delete() renames the node and moves it into API Deleted Items.

from labapi import NotebookDirectory, NotebookPage

archive = notebook.create(NotebookDirectory, "Archive")
page = notebook.create(NotebookPage, "Fresh Results")

print("Fresh Results" in list(notebook))

page.move_to(archive)
print(page.parent is archive)  # True without refresh()

When to Refresh Data#

Use refresh() when you need to pick up changes made by another user, the LabArchives web UI, or another API session.

Example: Polling for New Entries#

import time

page = notebook.traverse("Experiment Results")

while True:
    # Clear cache and check for new entries
    page.refresh()
    entries = page.entries

    print(f"Current entry count: {len(entries)}")

    if len(entries) >= 10:
        print("Required entries found!")
        break

    time.sleep(30)  # Wait 30 seconds before checking again

Limitations#

Warning

refresh() does not update child pages, directories, or entries that you already hold, so they can keep stale data:

page = notebook.traverse("My Page")
entry = page.entries[0]  # Get reference to first entry

# Refresh the page
page.refresh()

# This entry object still has old cached data!
# It wasn't invalidated by the refresh
print(entry.content)  # May show stale data

After refresh(), re-fetch child objects before reading them:

page = notebook.traverse("My Page")

# Refresh and re-fetch entries
page.refresh()
entry = page.entries[0]  # Get a fresh reference

print(entry.content)  # Shows current data

What Gets Cached#

On a notebook or directory, refresh() clears the list of child pages and subdirectories and the child count. On a page, it clears the list of entries and their content and metadata.

Not cleared by refresh():

  • The User notebooks collection, which is built once at login