Skip to content

Commit 91d8cab

Browse files
author
John Lyu
committed
fixup! feat: Add filesystem-only caching with global refresh support
1 parent 493f390 commit 91d8cab

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

docs/usage/caching.md

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ When generating images, charts, or running expensive computations in your docume
1414

1515
All cached results are stored in `.markdown-exec-cache/` in your project root directory:
1616

17-
```
17+
```sh
1818
your-project/
1919
├── docs/
2020
├── mkdocs.yml
@@ -62,19 +62,21 @@ The cache file will be stored as `.markdown-exec-cache/my-plot.cache`.
6262

6363
To force re-execution and update the cache for a specific code block, use `refresh="yes"`:
6464

65-
```markdown
65+
````markdown
6666
```python exec="yes" cache="my-plot" refresh="yes"
6767
# This will always re-execute and update the cache
6868
print("Fresh execution!")
6969
```
70-
```
70+
````
7171

7272
!!! note "refresh vs removing cache"
73-
**`refresh="yes"`** forces re-execution but **keeps the cache enabled** - it updates the cached result for future builds.
74-
75-
**Removing `cache` option** completely disables caching - the code executes every time with no caching at all.
76-
77-
Use `refresh="yes"` when you want to update stale cache but keep caching benefits for subsequent builds.
73+
**`refresh="yes"`** forces re-execution but **keeps the cache enabled** - it updates the cached result for future builds.
74+
75+
```bash
76+
**Removing `cache` option** completely disables caching - the code executes every time with no caching at all.
77+
78+
Use `refresh="yes"` when you want to update stale cache but keep caching benefits for subsequent builds.
79+
```
7880

7981
### Global Cache Refresh
8082

@@ -91,6 +93,7 @@ MARKDOWN_EXEC_CACHE_REFRESH=on mkdocs build
9193
```
9294

9395
This is useful for:
96+
9497
- CI/CD pipelines where you want fresh builds
9598
- Ensuring all documentation is up-to-date
9699
- Debugging cache-related issues
@@ -116,68 +119,79 @@ rm -rf .markdown-exec-cache/
116119
## How It Works
117120

118121
1. **Hash Computation**: For `cache="yes"`, a SHA-256 hash is computed from:
122+
119123
- The code content
120124
- Execution options (language, HTML mode, working directory, etc.)
121-
122-
2. **Cache Lookup**: Before execution, the filesystem cache is checked for a matching entry
123-
124-
3. **Execution & Storage**: If no cached result is found:
125+
126+
1. **Cache Lookup**: Before execution, the filesystem cache is checked for a matching entry
127+
128+
1. **Execution & Storage**: If no cached result is found:
129+
125130
- Code is executed
126131
- Output is stored in the filesystem cache
127-
128-
4. **Cache Retrieval**: Cached output is used instead of re-executing the code
132+
133+
1. **Cache Retrieval**: Cached output is used instead of re-executing the code
129134

130135
## Best Practices
131136

132137
### When to Use Caching
133138

134139
**Good use cases:**
140+
135141
- Generating plots, diagrams, or images
136142
- Running expensive computations
137143
- Calling external APIs or services
138144
- Processing large datasets
139145

140146
**Avoid caching for:**
147+
141148
- Simple print statements
142149
- Code demonstrating output variations
143150
- Time-sensitive or non-deterministic code
144151

145152
### Choosing Cache Type
146153

147154
- **`cache="yes"`** (hash-based):
155+
148156
- Automatically invalidated when code changes
149157
- Great for development and production
150158
- No manual cache management needed
151159

152160
- **`cache="custom-id"`** (custom ID):
161+
153162
- Use for expensive operations where you want explicit control
154163
- Easier to identify and manage specific cache files
155164
- Requires manual invalidation or `refresh="yes"` when code changes
156165

157166
### Cache Invalidation Strategy
158167

159168
**For hash-based caching (`cache="yes"`):**
169+
160170
- Cache is automatically invalidated when code or options change
161171
- No manual intervention needed
162172

163173
**For custom ID caching (`cache="custom-id"`):**
164174

165175
1. **Change the ID** when you want to force re-execution:
176+
166177
```markdown
167178
cache="my-plot-v2" # Changed from my-plot
168179
```
169180

170-
2. **Use refresh temporarily**:
181+
1. **Use refresh temporarily**:
182+
171183
```markdown
172184
cache="my-plot" refresh="yes" # Remove refresh="yes" after update
173185
```
174186

175-
3. **Use global refresh** for all caches:
187+
1. **Use global refresh** for all caches:
188+
176189
```bash
177190
MARKDOWN_EXEC_CACHE_REFRESH=1 mkdocs build
178191
```
179192

180-
4. **Clear cache directory** before important builds:
193+
1. **Clear cache directory** before important builds:
194+
181195
```bash
182196
rm -rf .markdown-exec-cache/
183197
```
@@ -223,14 +237,14 @@ print(f"⭐ **{stars}** stars on GitHub!")
223237
### Cache Not Working
224238

225239
1. Ensure the cache directory is writable
226-
2. Check that you're using `cache="yes"` or a custom ID
227-
3. Verify the cache directory exists: `ls -la .markdown-exec-cache/`
240+
1. Check that you're using `cache="yes"` or a custom ID
241+
1. Verify the cache directory exists: `ls -la .markdown-exec-cache/`
228242

229243
### Stale Cache Results
230244

231245
1. Use `refresh="yes"` to force re-execution
232-
2. Delete the specific cache file
233-
3. Clear the entire cache directory
246+
1. Delete the specific cache file
247+
1. Clear the entire cache directory
234248

235249
### Large Cache Directory
236250

src/markdown_exec/_internal/formatters/base.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,6 @@ def base_format(
192192

193193
# Cache the output if caching is enabled
194194
if cache:
195-
cache_manager = get_cache_manager()
196-
cache_id = cache if isinstance(cache, str) else None
197-
cache_options = {
198-
"language": language,
199-
"html": html,
200-
"result": result,
201-
"returncode": returncode,
202-
"workdir": workdir,
203-
"width": width,
204-
"extra": extra,
205-
}
206195
cache_manager.set(cache_id, source_input, output, **cache_options)
207196

208197
if not output and not source:

0 commit comments

Comments
 (0)