https://github.com/wgliang/timewheel
Timewheel-Golang
https://github.com/wgliang/timewheel
Last synced: 6 months ago
JSON representation
Timewheel-Golang
- Host: GitHub
- URL: https://github.com/wgliang/timewheel
- Owner: wgliang
- License: apache-2.0
- Created: 2016-11-25T03:03:42.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-25T06:00:22.000Z (almost 9 years ago)
- Last Synced: 2025-03-30T06:51:11.284Z (6 months ago)
- Language: Go
- Size: 10.7 KB
- Stars: 15
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# timewheel
timewheel-golang
[](https://travis-ci.org/wgliang/timewheel)
[](https://godoc.org/github.com/wgliang/timewheel)
[](https://gitter.im/timewheel/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://landscape.io/github/wgliang/timewheel/master)
[](https://www.quantifiedcode.com/app/project/98b2cb0efd774c5fa8f9299c4f96a8c5)
[](https://goreportcard.com/report/github.com/wgliang/timewheel)
[](http://www.apache.org/licenses/LICENSE-2.0.html)timewheel is a library which provides a timer on your resource,such as net-connection
or data-in-memory and so on. The timewheel contain interface 'TimeWheel.Start()',
'TimeWheel.Add()','TimeWheel.Stop()' and 'TimeWheel.Remove()'.Major additional concepts are:
In addition to the [godoc API documentation](http://godoc.org/github.com/wgliang/timewheel)
## Recent Changes
support type-string,but you can rewrite it in line [155](https://github.com/wgliang/timewheel/blob/master/timewheel.go#L155).
## Install
go get github.com/wgliang/timewheel
## Usage
Below is an example which shows some common use cases for timewheel. Check
[wheel_test.go](https://github.com/wgliang/timewheel/blob/master/wheel_test.go) for more
usage.```go
package mainimport (
"fmt"
"strconv"
"sync"
"time""github.com/wgliang/timewheel"
)type Wheel struct {
mux *sync.Mutex
Cache map[string]string
}func NewWheel() *Wheel {
w := &Wheel{
mux: new(sync.Mutex),
}
w.Cache = make(map[string]string, 0)
return w
}func (w *Wheel) Get(key string) string {
w.mux.Lock()
defer w.mux.Unlock()
return w.Cache[key]
}func (w *Wheel) Add(key, value string) {
w.mux.Lock()
defer w.mux.Unlock()
w.Cache[key] = value
}func (w *Wheel) Remove(key string) {
w.mux.Lock()
defer w.mux.Unlock()
delete(w.Cache, key)
}func goValue(tw *timewheel.TimeWheel, w *Wheel) {
ti := time.Tick(1 * time.Second)
key := 0
for {
select {
case <-ti:
if key == 20 {
return
}
key++
value := time.Now().Format("2016-11-14 22:22:22")
// 业务中对资源的管理
w.Add(strconv.Itoa(key), value)
fmt.Println("add to Wheel...", value)
// 不要忘记同时在时间轮里也要做改变,原则就是业务中的改变记得通知时间轮,但时间轮做的工作我们无需关心
tw.Add(strconv.Itoa(key))
fmt.Println("add to TimeWheel...", key)
}
}
}func PrintWheel(w *Wheel) {
ti := time.Tick(1 * time.Second)
key := 0
for {
select {
case <-ti:
if key == 30 {
return
}
key++
w.mux.Lock()
fmt.Println(w.Cache)
w.mux.Unlock()
}
}
}func main() {
// 初始化你的资源和接口
w := NewWheel()
// 传入你要对你的资源要做的操作,以及传入回调函数
wheel := timewheel.NewTimeWheel(time.Second*1, 10, func(w interface{}, key interface{}) {
w.(*Wheel).Remove(key.(string))
}, w)
// 开启我们的时间轮
wheel.Start()
// 在这里代表你的项目中对资源做的改变,增加等等,然后剩下的就交给时间轮管理吧
go goValue(wheel, w)
go PrintWheel(w)
time.Sleep(30 * time.Second)
}
```