globocom/go-buffer
GitHub: globocom/go-buffer
go-buffer 是一个 Go 异步数据缓冲库,支持按大小、时间间隔或手动触发刷新,帮助应用在写入外部存储前高效聚合数据。
Stars: 28 | Forks: 8
# go-buffer
`go-buffer` 表示一个会异步刷新其内容的缓冲区。它非常适合需要在将数据写入外部存储之前进行聚合的应用程序。缓冲区可以手动刷新,也可以在缓冲区满时或经过指定时间间隔后自动刷新,以最先触发的条件为准。
## 安装说明
```
go get github.com/globocom/go-buffer
```
Go < 1.18:
```
go get github.com/globocom/go-buffer@v2
```
## 示例
### 基于大小触发刷新
```
package main
import (
"time"
"github.com/globocom/go-buffer/v3"
)
func main() {
buff := buffer.New(
// call this function when the buffer needs flushing
func(items []string) {
for _, item := range items {
println(string)
}
},
// buffer can hold up to 5 items
buffer.WithSize(5),
)
// ensure the buffer
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
buff.Push("item 4")
buff.Push("item 5")
// block the current goroutine
time.Sleep(3 * time.Second)
println("done")
}
```
### 基于时间间隔触发刷新
```
package main
import (
"time"
"github.com/globocom/go-buffer/v3"
)
func main() {
buff := buffer.New(
// call this function when the buffer needs flushing
func(items []string) {
for _, item := range items {
println(item)
}
},
// buffer can hold up to 5 items
buffer.WithSize(5),
// buffer will be flushed every second, regardless of
// how many items were pushed
buffer.WithFlushInterval(time.Second),
)
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
// block the current goroutine
time.Sleep(3 * time.Second)
println("done")
}
```
### 手动刷新
```
package main
import (
"time"
"github.com/globocom/go-buffer/v3"
)
func main() {
buff := buffer.New(
// call this function when the buffer needs flushing
func(items []string) {
for _, item := range items {
println(item)
}
},
// buffer can hold up to 5 items
buffer.WithSize(5),
)
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
// block the current goroutine
time.Sleep(3*time.Second)
buff.Flush()
println("done")
}
```
## 示例 v2
### 基于大小触发刷新
```
package main
import (
"time"
"github.com/globocom/go-buffer/v2"
)
func main() {
buff := buffer.New(
// buffer can hold up to 5 items
buffer.WithSize(5),
// call this function when the buffer needs flushing
buffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}) {
for _, item := range items {
println(item.(string))
}
})),
)
// ensure the buffer
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
buff.Push("item 4")
buff.Push("item 5")
// block the current goroutine
time.Sleep(3 * time.Second)
println("done")
}
```
### 基于时间间隔触发刷新
```
package main
import (
"time"
"github.com/globocom/go-buffer/v2"
)
func main() {
buff := buffer.New(
// buffer can hold up to 5 items
buffer.WithSize(5),
// buffer will be flushed every second, regardless of
// how many items were pushed
buffer.WithFlushInterval(time.Second),
// call this function when the buffer needs flushing
buffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}) {
for _, item := range items {
println(item.(string))
}
})),
)
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
// block the current goroutine
time.Sleep(3 * time.Second)
println("done")
}
```
### 手动刷新
```
package main
import (
"time"
"github.com/globocom/go-buffer/v2"
)
func main() {
buff := buffer.New(
// buffer can hold up to 5 items
buffer.WithSize(5),
// call this function when the buffer needs flushing
buffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}) {
for _, item := range items {
println(item.(string))
}
})),
)
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
// block the current goroutine
time.Sleep(3*time.Second)
buff.Flush()
println("done")
}
```
## 文档
访问 [Pkg.go.dev](https://pkg.go.dev/github.com/globocom/go-buffer) 查看完整文档。
## 许可证
[MIT 许可证](https://github.com/globocom/go-buffer/blob/master/LICENSE)
标签:EVTX分析, Go, Ruby工具, SOC Prime, 中间件, 开发工具, 异步处理, 数据缓冲, 数据聚合, 日志审计