资讯详情

资讯详情

Crawl4AI 旧版缓存布尔参数(bypass_cache 等)如何迁移到 CacheMode 枚举

Crawl4AI 旧版缓存布尔参数bypass_cache 等如何迁移到 CacheMode 枚举【免费下载链接】crawl4ai Crawl4AI: Open-source LLM Friendly Web Crawler Scraper. Dont be shy, join here: https://discord.gg/jP8KfhDhyN项目地址: https://gitcode.com/GitHub_Trending/craw/crawl4ai如果你维护的脚本里还在给crawler.arun()传bypass_cacheTrue、disable_cacheTrue这类布尔参数从 Crawl4AI 0.5.0 开始它们已经不再是受支持的用法官方迁移文档 cache-modes.md 说明从 0.5.0 起 Crawl4AI 用CacheMode枚举加单个cache_mode参数取代了旧布尔标志。本文只解决一件事把旧代码中出现的bypass_cache、disable_cache、no_cache_read、no_cache_write以及AsyncWebCrawler(always_by_pass_cacheTrue)这种构造参数逐处改写成cache_modeCacheMode.X并给出迁移后的行为验证方式。适用前提你使用的 Crawl4AI 为 0.5.0 或更高版本0.5.0 的 changelog 条目中列出了旧标志的移除与迁移示例见 CHANGELOG.md。迁移前先定位代码中的旧参数0.5.0 的 changelog 示例展示了两类旧写法# 旧写法0.5.0 之前文档示例 crawler AsyncWebCrawler(always_by_pass_cacheTrue) result await crawler.arun(urlhttps://example.com, bypass_cacheTrue)注意旧参数名本身还有一个拼写细节AsyncWebCrawler上叫always_by_pass_cache带下划线分隔的 by_pass0.5.0 起对应参数为always_bypass_cache。迁移时要一并改名。用下面命令在项目里把所有待迁移的调用点找出来grep -rn bypass_cache\|disable_cache\|no_cache_read\|no_cache_write\|always_by_pass_cache\|always_bypass_cache . --include*.py逐个确认每个命中点属于哪种情况作为arun(...)/arun_many(...)的直接关键字参数传入作为CrawlerRunConfig的字段赋值如config.bypass_cache True作为AsyncWebCrawler(...)的构造参数always_by_pass_cacheTrue。这三处都要改但改法不同见下面各节。新旧参数对照每个布尔参数换成哪个 CacheModeCacheMode定义在 crawl4ai/cache_context.py从顶层包即可导入crawl4ai/__init__.py的导出列表包含CacheMode。五个枚举值及文档给出的含义CacheMode行为CacheMode.ENABLED正常缓存读写CacheMode.DISABLED完全不使用缓存CacheMode.READ_ONLY只读缓存不写入CacheMode.WRITE_ONLY只写缓存不读取CacheMode.BYPASS本次操作跳过缓存cache-modes.md 给出的迁移对照表为旧参数替换写法bypass_cachecache_modeCacheMode.BYPASSdisable_cachecache_modeCacheMode.DISABLEDno_cache_readcache_modeCacheMode.READ_ONLYno_cache_writecache_modeCacheMode.WRITE_ONLY这里有一个必须知道的文档冲突no_cache_read与no_cache_write两个参数在两份资料中指向相反的值。文档表格cache-modes.mdno_cache_read→READ_ONLYno_cache_write→WRITE_ONLY源码中两处一致地给出相反映射crawl4ai/async_configs.py 的弃用提示为no_cache_read : Instead, use cache_modeCacheMode.WRITE_ONLY、no_cache_write : Instead, use cache_modeCacheMode.READ_ONLYcrawl4ai/cache_context.py 的内部转换函数_legacy_to_cache_mode中no_cache_readTrue返回CacheMode.WRITE_ONLYno_cache_writeTrue返回CacheMode.READ_ONLY。从语义看源码映射是自洽的no_cache_read表示不从缓存读对应只写不读的WRITE_ONLYno_cache_write表示不写入缓存对应只读不写的READ_ONLY。如果你需要精确复现 0.4.x 时代某个布尔组合的行为请按源码映射cache_context.py 的完整优先级选择# 源码 _legacy_to_cache_mode 的转换优先级crawl4ai/cache_context.py if disable_cache: return CacheMode.DISABLED if bypass_cache: return CacheMode.BYPASS if no_cache_read and no_cache_write: return CacheMode.DISABLED if no_cache_read: return CacheMode.WRITE_ONLY if no_cache_write: return CacheMode.READ_ONLY return CacheMode.ENABLED也就是说多个布尔同时为真时disable_cache优先级最高其次是bypass_cacheno_cache_read和no_cache_write同时为真等价于DISABLED。改写步骤1.arun()直接传布尔参数移入 CrawlerRunConfig旧写法把布尔直接挂在arun()上新写法统一通过CrawlerRunConfig的cache_mode字段传入。CrawlerRunConfig的签名为cache_mode: CacheMode CacheMode.BYPASS见 crawl4ai/async_configs.py即不传时默认就是BYPASS。cache-modes.md 给出的推荐写法文档示例URL 可替换为你自己的目标地址import asyncio from crawl4ai import AsyncWebCrawler, CacheMode from crawl4ai.async_configs import CrawlerRunConfig async def use_proxy(): # Use CacheMode in CrawlerRunConfig config CrawlerRunConfig(cache_modeCacheMode.BYPASS) async with AsyncWebCrawler(verboseTrue) as crawler: result await crawler.arun( urlhttps://www.nbcnews.com/business, configconfig # Pass the configuration object ) print(len(result.markdown)) async def main(): await use_proxy() if __name__ __main__: asyncio.run(main())按上表替换原来的bypass_cacheTrue就是cache_modeCacheMode.BYPASS原来什么都不传、依赖默认缓存的场景则显式写cache_modeCacheMode.ENABLED。另外注意AsyncWebCrawler.arun内部有一处行为crawl4ai/async_webcrawler.py当config.cache_mode is None时会被改写为CacheMode.ENABLED再构造CacheContext。2.CrawlerRunConfig字段赋值改成构造函数参数如果你以前通过CrawlerRunConfig(bypass_cacheTrue)或config.no_cache_write True这种方式设置直接删除这些赋值改在构造时传cache_mode# 旧已弃用config.bypass_cache True / CrawlerRunConfig(no_cache_readTrue) # 新 config CrawlerRunConfig(cache_modeCacheMode.WRITE_ONLY) # 对应原 no_cache_readTrue当前版本中读取或写入这些旧字段会直接抛出AttributeError报错文案来自 crawl4ai/async_configs.py 的_UNWANTED_PROPS处理例如Setting bypass_cache is deprecated. Instead, use cache_modeCacheMode.BYPASS Setting disable_cache is deprecated. Instead, use cache_modeCacheMode.DISABLED Setting no_cache_read is deprecated. Instead, use cache_modeCacheMode.WRITE_ONLY Setting no_cache_write is deprecated. Instead, use cache_modeCacheMode.READ_ONLY注意报错文案与文档表格在no_cache_read/no_cache_write上同样是冲突的见上一节。运行时如果抛出上面这种AttributeError就按报错里给出的建议改即可。3.AsyncWebCrawler(always_by_pass_cacheTrue)改名always_bypass_cache0.5.0 的 changelog 示例展示了这组迁移前后对照文档示例# Old way crawler AsyncWebCrawler(always_by_pass_cacheTrue) result await crawler.arun(urlhttps://example.com, bypass_cacheTrue) # New way from crawl4ai import CacheMode crawler AsyncWebCrawler(always_bypass_cacheTrue) result await crawler.arun(urlhttps://example.com, cache_modeCacheMode.BYPASS)同时 changelog 列出的移除清单0.5.0 条目bypass_cache、disable_cache、no_cache_read、no_cache_write、always_by_pass_cache均已被cache_mode取代。迁移后如何验证缓存行为迁移完成的判断标准不是代码不报错而是缓存读写语义符合预期。crawl4ai/cache_context.py 中CacheContext的两个判定方法给出了明确的读写规则should_read()仅当cache_mode为ENABLED或READ_ONLY时返回 Trueshould_write()仅当cache_mode为ENABLED或WRITE_ONLY时返回 True。也就是说迁移后各枚举的实际效果是ENABLED读写、READ_ONLY只读、WRITE_ONLY只写、DISABLED与BYPASS对该次操作既不读也不写区别在于DISABLED表示全程不使用缓存BYPASS表示仅本次操作跳过定义见 cache_context.py 的注释。验证时可按这个规则核对你的场景例如把原来no_cache_writeTrue不写缓存的调用改成CacheMode.READ_ONLY后确认该请求命中should_read()为 True、should_write()为 False 的组合。限制与注意事项不要在cache_mode之外保留旧布尔参数做双保险当前代码里旧字段既不出现在arun的有效参数列表中读写也会触发AttributeError。no_cache_read/no_cache_write的替换值存在文档与源码的冲突上文已列出两处来源按语义复现旧行为请以 crawl4ai/cache_context.py 的_legacy_to_cache_mode为准仅追求不读或不写的目标时READ_ONLY/WRITE_ONLY的语义描述本身是清楚的。仓库内的 Docker API 服务deploy/docker/api.py接收的是cache查询参数并映射为CacheMode.ENABLED/CacheMode.WRITE_ONLY与 Python 库的cache_mode不是同一个接口如果你只使用 Python 库本节的迁移不涉及 API 参数改动。更宏观的升级例如 0.7.x 升到 0.8.0 的 hooks 默认禁用、file://阻断等不在本文范围内参见 v0.8.0-upgrade-guide.md。【免费下载链接】crawl4ai Crawl4AI: Open-source LLM Friendly Web Crawler Scraper. Dont be shy, join here: https://discord.gg/jP8KfhDhyN项目地址: https://gitcode.com/GitHub_Trending/craw/crawl4ai创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →