This repository was archived by the owner on Feb 26, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
86 lines (78 loc) · 2.37 KB
/
cache.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
// lwnfeed - A full-text RSS feed generator for LWN.net.
// Copyright (C) 2020-2022 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/gob"
"fmt"
"io"
"os"
"github.com/gorilla/feeds"
log "maunium.net/go/maulogger/v2"
)
var cachedArticles = make(map[int]*feeds.Item)
var cacheFile *os.File
var cacheWriter *gob.Encoder
type CacheItem struct {
ID int
Item *feeds.Item
}
func readCache(path string) error {
log.Infoln("Loading cached articles from", path)
file, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to open file: %w", err)
}
dec := gob.NewDecoder(file)
for {
var cacheItem CacheItem
err = dec.Decode(&cacheItem)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("error decoding item: %w", err)
}
cachedArticles[cacheItem.ID] = cacheItem.Item
}
log.Infoln("Loaded", len(cachedArticles), "cached articles from disk")
return nil
}
func openCacheWriter(path string) (err error) {
log.Debugln("Opening cache file for writing")
cacheFile, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
cacheWriter = gob.NewEncoder(cacheFile)
log.Debugln("Rewriting cache file on disk")
for id, item := range cachedArticles {
err = cacheWriter.Encode(CacheItem{id, item})
if err != nil {
log.Warnfln("Failed to add article %d to disk cache: %v", id, err)
}
}
log.Debugln("Successfully opened cache file")
return
}
func addToCache(id int, item *feeds.Item) {
cachedArticles[id] = item
if cacheWriter != nil {
err := cacheWriter.Encode(CacheItem{id, item})
if err != nil {
log.Warnfln("Failed to add article %d to disk cache: %v", id, err)
}
}
log.Debugln("Added article", id, "to cache")
}