-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.go
109 lines (85 loc) · 2.33 KB
/
persistence.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package inmemory
import (
"encoding/gob"
"log"
"os"
"path/filepath"
"time"
)
// persistenced manages saving inmemory data to disk
// to be able to restart server and restore all data
func (dataStore *DataStore) persistenced() {
// use directory to place backups
backupsDir := ".backups"
// create directory if doesn't exist
if _, err := os.Stat(backupsDir); os.IsNotExist(err) {
os.Mkdir(backupsDir, 0755)
}
// register the structures for correct encoding for the backup
gob.Register(map[string]string{})
for {
time.Sleep(backupInterval)
// Store all the data in the file
err := dataStore.ToFile(backupsDir)
// log the result of saving the data
if err == nil {
log.Println("Backup created")
} else {
log.Println("Error creating backup", err)
}
// number of backups is defined by variable backupNumber
backups, err := filepath.Glob(backupsDir + "/cache_data*.gob")
if err != nil {
log.Println(err)
}
// delete obsolete backups
if len(backups) > backupNumber {
// delete all backups except for the defined number
for _, old := range backups[0 : len(backups)-backupNumber] {
err := os.Remove(old)
if err != nil {
log.Println(err)
}
}
}
}
}
// ToFile writes all data from the data store.
// gob encoding is used for the process
// Only the data is stored, the caching order is omitted
func (dataStore *DataStore) ToFile(path string) error {
timestamp := time.Now().Format("20060102150405")
path += "/cache_data" + timestamp + ".gob"
// create file to write data to
backup, err := os.Create(path)
if err != nil {
log.Println(err)
return err
}
defer backup.Close()
encCache := gob.NewEncoder(backup)
dataStore.RLock()
defer dataStore.RUnlock()
return encCache.Encode(dataStore.values)
}
// FromFile reads gob file and restores data store.
// Also the cache data structure is also filled
func (dataStore *DataStore) FromFile(path string) error {
backup, err := os.Open(path)
if err != nil {
log.Println(err)
return err
}
defer backup.Close()
decCache := gob.NewDecoder(backup)
dataStore.Lock()
decCache.Decode(&dataStore.values)
// restore cache
for _, item := range dataStore.values {
el := dataStore.cache.PushFront(item)
item.el = el
}
dataStore.Unlock()
log.Printf("Restored %d values from backup %s\n", len(dataStore.values), path)
return nil
}