Skip to content

refactor(cache)!: extract hybrid_cache package#2477

Merged
j2rong4cn merged 6 commits into
OpenListTeam:mainfrom
j2rong4cn:pr/refactor8
May 18, 2026
Merged

refactor(cache)!: extract hybrid_cache package#2477
j2rong4cn merged 6 commits into
OpenListTeam:mainfrom
j2rong4cn:pr/refactor8

Conversation

@j2rong4cn
Copy link
Copy Markdown
Member

@j2rong4cn j2rong4cn commented May 16, 2026

Description / 描述

  • 主要改动

    • 将 HybridCache 相关的代码移动到 internal/hybrid_cache 包中
    • 引入 BackingStore 抽象,包含 BufferStore 和 FileStore
    • 在 config/env/bootstrap 中将 CacheThreshold 重命名为 AutoMemoryLimit
  • HybridCache 的缓存策略

    • 小于等于auto_memory_limit的数据流,仅使用[]byte缓存(Go自动管理内存),无回退文件缓存
    • 大于auto_memory_limit的数据流,优先使用手动管理内存(Unix Mmap或者Windows VirtualAlloc)用作缓存,内存不足回退到文件缓存
    • min_free_memory小于0时,仅使用文件缓存
  • HybridCache 相关配置项:

    • auto_memory_limit 单位MB
      • 默认值:4
      • 在HybridCache中使用[]byte缓存数据流的限制,内存为自动管理,直到GC。
    • min_free_memory 单位MB
      • 默认值:小于16为自动大小,总内存的10%,最大1024
      • 最小空闲内存,当内存不足时,HybridCache会回退到文件缓存。
      • 如果小于0,HybridCache仅使用文件缓存,不占用内存。
    • max_block_limit 单位BM
      • 默认值:小于4为自动大小,总内存的3%,最大64
      • 限制HybridCache手动管理内存单次的扩容大小,超过该阈值将分多次扩容(为了检测内存是否充足)。 min_free_memory大于0时,也限制多线程下载的分片大小

Motivation and Context / 背景

Relate #2460

How Has This Been Tested? / 测试

  • go test -run '^(TestRangeRead|TestPreHash|TestStreamSectionReader)$' ./internal/stream
  • go test -run '^(TestDownloadOrder|TestHighConcurrency|TestDownloadSingle)$' ./internal/net
  • go test ./internal/hybrid_cache

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it’s needed).
    我已相应更新了相关仓库(若适用)。

j2rong4cn added 2 commits May 16, 2026 12:09
…_threshold to auto_memory_limit

* split HybridCache and stores into internal/hybrid_cache package
* introduce BackingStore abstraction with BufferStore and FileStore
* rename CacheThreshold to AutoMemoryLimit in config/env/bootstrap
* update stream/request integration and related tests
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors stream caching by extracting the former internal/mem.HybridCache into a dedicated internal/hybrid_cache package (with BackingStore abstractions), and introduces a breaking configuration rename from cache_threshold to auto_memory_limit to control when Go-managed memory vs manual/file-backed caching is used.

Changes:

  • Extract HybridCache into internal/hybrid_cache and add BackingStore implementations (BufferStore, FileStore).
  • Remove pkg/buffer’s old Reader/PeekFile utilities and consolidate NewByteBlock into a smaller file.
  • Rename config cache_thresholdauto_memory_limit and update bootstrap + tests accordingly.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
pkg/buffer/utils.go Moves provider interface types out; keeps adapter helpers.
pkg/buffer/type.go Defines WriteAtSeekerProvider / ReadAtSeekerProvider alongside core buffer types.
pkg/buffer/file.go Removes PeekFile implementation (deleted).
pkg/buffer/bytes.go Removes buffer.Reader and embedded byteBlock (deleted).
pkg/buffer/buffer.go Reintroduces NewByteBlock + byteBlock in a dedicated file.
internal/stream/util.go Switches stream section caching to internal/hybrid_cache.
internal/stream/stream.go Reworks stream caching to always use the new HybridCache path.
internal/stream/stream_test.go Updates tests for the AutoMemoryLimit rename/behavior.
internal/net/request.go Switches downloader caching to internal/hybrid_cache.
internal/mem/cache.go Removes old HybridCache implementation (deleted).
internal/hybrid_cache/type.go Introduces BackingStore interface.
internal/hybrid_cache/hybrid_cache.go New HybridCache implementation and policy selection logic.
internal/hybrid_cache/file.go File-backed store implementation and constructors.
internal/hybrid_cache/file_test.go Updates tests to new package/types (MultiFileStore, GrowTo).
internal/hybrid_cache/buffer.go Adds BufferStore (Go-managed memory backing store).
internal/hybrid_cache/buffer_test.go Adds/updates tests for BufferStore.
internal/conf/var.go Renames threshold var to AutoMemoryLimit and updates comments.
internal/conf/config.go Renames config field/tag to auto_memory_limit and updates defaults.
internal/bootstrap/config.go Reads and logs AutoMemoryLimit instead of CacheThreshold.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/hybrid_cache/hybrid_cache.go
Comment thread internal/hybrid_cache/hybrid_cache.go
Comment thread internal/hybrid_cache/hybrid_cache.go
Comment thread internal/hybrid_cache/hybrid_cache.go
Comment thread internal/hybrid_cache/buffer.go
Comment thread internal/hybrid_cache/buffer.go
Comment thread internal/hybrid_cache/buffer.go
Comment thread internal/conf/var.go Outdated
@j2rong4cn j2rong4cn changed the title refactor(HybridCache)!: extract hybrid_cache package and rename cache_threshold to auto_memory_limit refactor(cache)!: extract hybrid_cache package and rename cache_threshold to auto_memory_limit May 16, 2026
@j2rong4cn j2rong4cn changed the title refactor(cache)!: extract hybrid_cache package and rename cache_threshold to auto_memory_limit refactor(cache)!: extract hybrid_cache package May 16, 2026
@j2rong4cn j2rong4cn merged commit 9eae625 into OpenListTeam:main May 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants