|
@@ -0,0 +1,50 @@
|
|
|
|
+package public
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ qutil "app.yhyue.com/moapp/jybase/common"
|
|
|
|
+ "fmt"
|
|
|
|
+ "sync"
|
|
|
|
+ "time"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ VarOrderCode *orderCode
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type orderCode struct {
|
|
|
|
+ pool chan string
|
|
|
|
+ all *sync.Map
|
|
|
|
+ lock *sync.Mutex
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (o *orderCode) gc() {
|
|
|
|
+ VarOrderCode.all.Range(func(key, value interface{}) bool {
|
|
|
|
+ if time.Now().Day() != value.(int) {
|
|
|
|
+ VarOrderCode.all.Delete(key)
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+ })
|
|
|
|
+ time.AfterFunc(24*time.Hour, o.gc)
|
|
|
|
+}
|
|
|
|
+func init() {
|
|
|
|
+ VarOrderCode = &orderCode{
|
|
|
|
+ pool: make(chan string, 20),
|
|
|
|
+ all: &sync.Map{},
|
|
|
|
+ lock: &sync.Mutex{},
|
|
|
|
+ }
|
|
|
|
+ VarOrderCode.gc()
|
|
|
|
+ for i := 0; i < 10; i++ {
|
|
|
|
+ go func() {
|
|
|
|
+ VarOrderCode.lock.Lock()
|
|
|
|
+ defer VarOrderCode.lock.Unlock()
|
|
|
|
+ for {
|
|
|
|
+ o := fmt.Sprintf("%d%s", time.Now().Unix()+900000000, qutil.GetRandom(2))
|
|
|
|
+ if _, ok := VarOrderCode.all.Load(o); ok {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ VarOrderCode.all.Store(o, time.Now().Day())
|
|
|
|
+ VarOrderCode.pool <- o
|
|
|
|
+ }
|
|
|
|
+ }()
|
|
|
|
+ }
|
|
|
|
+}
|