Documentation
¶
Overview ¶
Package ringbuffer 提供定长环形缓冲:只保留最近 N 条,写满后覆盖最旧的, O(1) 追加。泛型 Ring[T]。
典型:直播间"最近 50 条弹幕"、玩家"最近 20 场战绩"、"最近 100 行滚动日志"、 监控采样窗口。这类"只关心最近若干条、旧的自动丢弃"的需求,用 slice 手撸要反复 裁剪且容易写错边界,本包封装成固定内存、无分配增长的环。
提供两个类型:
- Ring[T]:非并发安全,零开销,单 goroutine 或调用方自行加锁时用;
- SyncRing[T]:内置 RWMutex 的并发安全版,读多写少场景直接用。
容量固定(构造时定),永不扩容;追加满后覆盖最旧元素。零值不可用,用 New 构造。
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Ring ¶
type Ring[T any] struct { // contains filtered or unexported fields }
Ring 定长环形缓冲(非并发安全)。零值不可用,用 New 构造。
Click to show internal directories.
Click to hide internal directories.