-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
40 lines (33 loc) · 1.05 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package scheduler
import (
"sync"
)
// Repository is a storage interface which can be implemented by multiple backend
// (in-memory map, sql database, in-memory cache, file system, ...)
// It allows standard CRUD operation on situations
type Repository interface {
Create(schedule InternalSchedule) (int64, error)
Get(id int64) (InternalSchedule, bool, error)
Update(schedule InternalSchedule) error
Delete(id int64) error
GetAll() (map[int64]InternalSchedule, error)
}
var (
_globalRepositoryMu sync.RWMutex
_globalRepository Repository
)
// R is used to access the global repository singleton
func R() Repository {
_globalRepositoryMu.RLock()
defer _globalRepositoryMu.RUnlock()
repository := _globalRepository
return repository
}
// ReplaceGlobalRepository affect a new repository to the global repository singleton
func ReplaceGlobalRepository(repository Repository) func() {
_globalRepositoryMu.Lock()
defer _globalRepositoryMu.Unlock()
prev := _globalRepository
_globalRepository = repository
return func() { ReplaceGlobalRepository(prev) }
}