Restore memory cache from disk file
(path string)
| 159 | |
| 160 | // Restore memory cache from disk file |
| 161 | func (store *MemoStore) Restore(path string) error { |
| 162 | if !util.Exists(path) { |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | f, err := os.Open(path) |
| 167 | if err != nil { |
| 168 | return fmt.Errorf("failed to read cache file: %s", err) |
| 169 | } |
| 170 | |
| 171 | defer func() { |
| 172 | f.Close() |
| 173 | os.Remove(path) |
| 174 | }() |
| 175 | |
| 176 | persisted := &item{} |
| 177 | dec := gob.NewDecoder(f) |
| 178 | if err := dec.Decode(&persisted); err != nil { |
| 179 | return fmt.Errorf("unknown cache file format: %s", err) |
| 180 | } |
| 181 | |
| 182 | items := persisted.Value.(map[string]itemWithTTL) |
| 183 | loaded := 0 |
| 184 | for k, v := range items { |
| 185 | if _, ok := getValue(v, true); ok { |
| 186 | loaded++ |
| 187 | store.Store.Store(k, v) |
| 188 | } else { |
| 189 | util.Log().Debug("Persisted cache %q is expired.", k) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | util.Log().Info("Restored %d items from %q into memory cache.", loaded, path) |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | func (store *MemoStore) DeleteAll() error { |
| 198 | store.Store.Range(func(key any, value any) bool { |