88from __future__ import annotations
99
1010import asyncio
11+ from enum import Enum
1112from typing import TYPE_CHECKING , Optional
1213
1314from marimo import _loggers
@@ -97,6 +98,11 @@ def _stop(self) -> None:
9798 self .heartbeat_task .cancel ()
9899
99100
101+ class CacheMode (Enum ):
102+ READ = "read"
103+ READ_WRITE = "write"
104+
105+
100106class CachingExtension (SessionExtension , SessionEventListener ):
101107 """Extension for caching session state to disk.
102108
@@ -111,15 +117,18 @@ def __init__(
111117 * ,
112118 enabled : bool ,
113119 interval : int = SESSION_CACHE_INTERVAL_SECONDS ,
120+ mode : CacheMode = CacheMode .READ_WRITE ,
114121 ) -> None :
115122 """Initialize the caching extension.
116123
117124 Args:
118125 enabled: Whether to enable caching
119126 interval: How often to write cache (in seconds)
127+ mode: Whether to read cache only or read/write.
120128 """
121129 self .interval = interval
122130 self .enabled = enabled
131+ self .mode = mode
123132 self .session_cache_manager : Optional [SessionCacheManager ] = None
124133 self .event_bus : Optional [SessionEventBus ] = None
125134
@@ -165,7 +174,8 @@ def on_attach(self, session: Session, event_bus: SessionEventBus) -> None:
165174 )
166175
167176 # Start the background task to write the session view to disk
168- self .session_cache_manager .start ()
177+ if self .mode is CacheMode .READ_WRITE :
178+ self .session_cache_manager .start ()
169179
170180 def on_detach (self ) -> None :
171181 """Stop cache manager when detached."""
@@ -179,6 +189,8 @@ async def on_session_notebook_renamed(
179189 ) -> None :
180190 """Rename the path for the cache manager."""
181191 del old_path
192+ if self .mode is not CacheMode .READ_WRITE :
193+ return None
182194 path = session .app_file_manager .path
183195 if self .session_cache_manager and path :
184196 self .session_cache_manager .rename_path (path )
0 commit comments