47 lines
786 B
Go
47 lines
786 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Response 统一的返回格式
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"msg"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
// Task 任务
|
|
type Task struct {
|
|
data gin.H
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func NewTask() *Task {
|
|
return &Task{data: gin.H{}}
|
|
}
|
|
|
|
// Store 存储一个字段
|
|
func (t *Task) Store(key string, value interface{}) {
|
|
t.lock.Lock()
|
|
defer t.lock.Unlock()
|
|
t.data[key] = value
|
|
}
|
|
|
|
// Load 取一个字段
|
|
func (t *Task) Load(key string) (value interface{}, ok bool) {
|
|
t.lock.Lock()
|
|
defer t.lock.Unlock()
|
|
value, ok = t.data[key]
|
|
return
|
|
}
|
|
|
|
func (t *Task) MarshalJSON() ([]byte, error) {
|
|
t.lock.Lock()
|
|
defer t.lock.Unlock()
|
|
return json.Marshal(t.data)
|
|
}
|