Implementation:1broseidon Ketch scrape CachedScrape
| Knowledge Sources | |
|---|---|
| Domains | Scrape, Caching |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
The cache-aware scrape entry point: rewrites and validates the URL, checks the page cache, and falls back to fetch+extract on a miss or stale entry.
Description
CachedScrape is the markdown path of the shared scrape pipeline. It rewrites the URL via the compiled `Rewriter`, validates it with `ValidateWebURL`, and computes the cache key with `Scraper.CacheKey` (which folds in cookie/UA/extract-mode fingerprints). On a non-stale cache hit it returns the cached `*Page` immediately. A hit is stale (and bypassed) when the entry was an unrendered JS shell and a browser is now available — see `CacheStaleForBrowser`. On a miss it calls `Scrape` (HTTP fetch → JS-shell detection → browser fallback → extraction) and writes the result back to the cache with its `source` tag.
The sibling variants CachedScrapeRaw (raw HTML), CachedScrapeForce (force-browser), and ScrapeSelector (CSS-selector extraction) share the same cache-key discipline.
Usage
Call this from the CLI `scrape` command, the MCP `scrape` tool, and the crawler (the crawler uses `ScrapeConditional` for conditional headers, but the cache discipline is the same). Pass a nil `PageCache` to bypass caching.
Code Reference
Source Location
- Repository: ketch
- File: scrape/pipeline.go
Signature
type PageCache interface {
Get(url string) (*Page, string)
Put(url string, page *Page, source string)
GetRaw(url string) (rawHTML, source string, page *Page)
PutRaw(url string, page *Page, source, rawHTML string)
}
// CachedScrape checks the cache first, falls back to fetch+extract.
func (s *Scraper) CachedScrape(ctx context.Context, pc PageCache, url string) (*Page, error)
// CachedScrapeRaw is the raw-HTML path (one fetch yields Page + RawHTML + Source).
func (s *Scraper) CachedScrapeRaw(ctx context.Context, pc PageCache, url string) (*Page, string, string, error)
// CacheStaleForBrowser reports whether a cache entry should be bypassed.
func CacheStaleForBrowser(source string, hasBrowser bool) bool
Import
import "github.com/1broseidon/ketch/scrape"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ctx | context.Context | Yes | Bounds the fetch |
| pc | scrape.PageCache | No | nil bypasses the cache entirely |
| url | string | Yes | URL to scrape (rewritten before fetch/cache-key) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | *scrape.Page | Page with markdown (and title, ETag, ContentHash) |
| return error | error | Invalid URL, fetch failure, or extraction failure |
Usage Examples
Markdown Scrape With Cache
// pc may be nil to bypass caching. The cache key folds in cookie/UA/extract-mode.
page, err := scraper.CachedScrape(ctx, pageCache, "https://example.com")
if err != nil {
return err
}
fmt.Println(page.Markdown)
Stale-Shell Invalidation
// An entry fetched as an unrendered shell (SourceHTTPShell) is bypassed once a
// browser is configured, so configuring Chrome retried rendering automatically.
if page, source := pc.Get(key); page != nil && !scrape.CacheStaleForBrowser(source, scraper.HasBrowser()) {
return page, nil // fresh hit
}
Related Pages
Implements Principle
- Principle:1broseidon_Ketch_Fast_Path_First_Scrape
- Principle:1broseidon_Ketch_Per_Operation_Cache_Handles