Skip to content

Commit 88c41bc

Browse files
author
王涛
committed
init
0 parents  commit 88c41bc

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## 说明
2+
fixedlist 提供定长列表,初始化时定义好长度,add()负责向尾部添加数据,当数据达到指定长度时,fixedlist会自动删除头部数据。
3+
4+
该场景常用于统计最近X时间的数据,比如最近60秒内每秒的请求量,只需把每秒的统计数据顺序add即可。
5+
## 安装
6+
```
7+
go get github.com/go-basic/fixedlist
8+
```
9+
## 使用
10+
```
11+
package main
12+
13+
import (
14+
"fmt"
15+
"github.com/go-basic/fixedlist"
16+
)
17+
18+
func main() {
19+
f := fixedlist.NewFixedList(2)
20+
f.Add("a")
21+
f.Add("b")
22+
fmt.Println(f.Data())
23+
f.Add("c")
24+
fmt.Println(f.Data())
25+
f.Add("d")
26+
fmt.Println(f.Data())
27+
f.Add("e")
28+
fmt.Println(f.Data())
29+
}
30+
/**
31+
[a b]
32+
[b c]
33+
[c d]
34+
[d e]
35+
*/
36+
```

example/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/go-basic/fixedlist"
6+
)
7+
8+
func main() {
9+
f := fixedlist.NewFixedList(2)
10+
f.Add("a")
11+
f.Add("b")
12+
fmt.Println(f.Data())
13+
f.Add("c")
14+
fmt.Println(f.Data())
15+
f.Add("d")
16+
fmt.Println(f.Data())
17+
f.Add("e")
18+
fmt.Println(f.Data())
19+
}
20+
21+
/**
22+
[a b]
23+
[b c]
24+
[c d]
25+
[d e]
26+
*/

fixedlist.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package fixedlist
2+
3+
import (
4+
"container/list"
5+
"sync"
6+
)
7+
8+
type FixedList interface {
9+
Add(interface{})
10+
Len() int
11+
Data() []interface{}
12+
}
13+
14+
type fixedList struct {
15+
sync.RWMutex
16+
length int
17+
data *list.List
18+
}
19+
20+
//创建定长列表
21+
func NewFixedList(len int) FixedList {
22+
f := &fixedList{}
23+
f.length = len
24+
f.data = list.New()
25+
return f
26+
}
27+
28+
//添加一条记录
29+
func (f *fixedList) Add(val interface{}) {
30+
f.Lock()
31+
defer f.Unlock()
32+
f.data.PushBack(val)
33+
if f.data.Len() > f.length {
34+
for i := 0; i <= f.data.Len()-f.length; i++ {
35+
f.data.Remove(f.data.Front())
36+
}
37+
}
38+
}
39+
40+
//获取数据长度
41+
func (f *fixedList) Len() int {
42+
return f.data.Len()
43+
}
44+
45+
//获取数据
46+
func (f *fixedList) Data() []interface{} {
47+
f.RLock()
48+
defer f.RUnlock()
49+
var data []interface{}
50+
for i := f.data.Front(); i != nil; i = i.Next() {
51+
data = append(data, i.Value)
52+
}
53+
return data
54+
}

fixedlist_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fixedlist
2+
3+
import "testing"
4+
5+
func TestFixedList_Add(t *testing.T) {
6+
f := NewFixedList(2)
7+
f.Add("a")
8+
if f.Data()[0] != "a" {
9+
t.Error("add err")
10+
}
11+
}
12+
13+
func TestFixedList_Len(t *testing.T) {
14+
var length int
15+
f := NewFixedList(length)
16+
f.Add("a")
17+
f.Add("b")
18+
f.Add("c")
19+
if f.Len() != length {
20+
t.Error("len err")
21+
}
22+
}
23+
24+
func TestFixedList_Data(t *testing.T) {
25+
var length int
26+
f := NewFixedList(length)
27+
f.Add("a")
28+
f.Add("b")
29+
f.Add("c")
30+
if len(f.Data()) != length {
31+
t.Error("data err")
32+
}
33+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/go-basic/fixedlist
2+
3+
go 1.14

0 commit comments

Comments
 (0)