feat: add aliyundrive driver
This commit is contained in:
35
pkg/cron/cron.go
Normal file
35
pkg/cron/cron.go
Normal file
@ -0,0 +1,35 @@
|
||||
package cron
|
||||
|
||||
import "time"
|
||||
|
||||
type Cron struct {
|
||||
d time.Duration
|
||||
ch chan struct{}
|
||||
}
|
||||
|
||||
func NewCron(d time.Duration) *Cron {
|
||||
return &Cron{
|
||||
d: d,
|
||||
ch: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cron) Do(f func()) {
|
||||
go func() {
|
||||
ticker := time.NewTicker(c.d)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
f()
|
||||
case <-c.ch:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *Cron) Stop() {
|
||||
c.ch <- struct{}{}
|
||||
close(c.ch)
|
||||
}
|
15
pkg/cron/cron_test.go
Normal file
15
pkg/cron/cron_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCron(t *testing.T) {
|
||||
c := NewCron(time.Second)
|
||||
c.Do(func() {
|
||||
t.Logf("cron log")
|
||||
})
|
||||
time.Sleep(time.Second * 5)
|
||||
c.Stop()
|
||||
}
|
19
pkg/utils/hash.go
Normal file
19
pkg/utils/hash.go
Normal file
@ -0,0 +1,19 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func GetSHA1Encode(data string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(data))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func GetMD5Encode(data string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(data))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
Reference in New Issue
Block a user