-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
55 lines (47 loc) · 1.12 KB
/
store.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package youcrawl
import (
"sync"
)
//store engine global
type GlobalStore interface {
Init() error
SetValue(key string, value interface{})
GetValue(key string) interface{}
GetOrCreate(key string, value interface{}) interface{}
}
type MemoryGlobalStore struct {
sync.Map
Content map[string]interface{}
}
func (s *MemoryGlobalStore) GetOrCreate(key string, value interface{}) interface{} {
result, _ := s.LoadOrStore(key, value)
return result
}
func (s *MemoryGlobalStore) SetValue(key string, value interface{}) {
s.Store(key, value)
}
func (s *MemoryGlobalStore) GetValue(key string) interface{} {
target, exist := s.Load(key)
if exist {
return target
} else {
return nil
}
}
func (s *MemoryGlobalStore) Init() error {
return nil
}
// global store pipeline
// save current item to global items
type GlobalStorePipeline struct {
}
func (g *GlobalStorePipeline) Process(item interface{}, store GlobalStore) error {
rawItems := store.GetValue("items")
if rawItems == nil {
rawItems = make([]interface{}, 0)
}
items := rawItems.([]interface{})
items = append(items, item)
store.SetValue("items", items)
return nil
}