package cacheSer import ( "sync" ) type SyncFunc func(list []interface{}) error type BaseWriter interface { Add(doc interface{}) Sync() error } type Writer struct { lock sync.Mutex list []interface{} sync SyncFunc } func NewWriter(syncFunc SyncFunc) BaseWriter { return &Writer{ lock: sync.Mutex{}, list: make([]interface{}, 0), sync: syncFunc, } } func (w *Writer) Add(doc interface{}) { w.lock.Lock() defer w.lock.Unlock() w.list = append(w.list, doc) } func (w *Writer) Sync() error { list := make([]interface{}, 0) func() { w.lock.Lock() defer w.lock.Unlock() list = w.list w.list = make([]interface{}, 0) }() return w.sync(list) }