-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcache.go
156 lines (134 loc) · 4.04 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) Joakim Kennedy, 2018
*/
package clinote
import (
"bytes"
"io"
"os"
"time"
)
var (
// DefaultNotebookCacheTime is the default time limit for when the
// list is considered outdated.
DefaultNotebookCacheTime = 24 * time.Hour
)
// NewNotebookCacheListWithLimit creates a new cache list with the given expiration limit.
func NewNotebookCacheListWithLimit(notebooks []*Notebook, limit time.Duration) *NotebookCacheList {
return &NotebookCacheList{
Notebooks: notebooks,
Limit: limit,
Timestamp: time.Now(),
}
}
// NewNotebookCacheList creates a cache list with the default expiration limit.
func NewNotebookCacheList(notebooks []*Notebook) *NotebookCacheList {
return NewNotebookCacheListWithLimit(notebooks, DefaultNotebookCacheTime)
}
// NotebookCacheList is a list of cached notebooks.
type NotebookCacheList struct {
// Notebooks is the list of notebooks.
Notebooks []*Notebook
// Timestamp of when the list was created.
Timestamp time.Time
// Limit is the until the list outdated.
Limit time.Duration
}
// IsOutdated returns true if the list has expired.
func (n *NotebookCacheList) IsOutdated() bool {
return time.Since(n.Timestamp) > n.Limit
}
// CacheFile has the note content written and the user
// edits the content in the CacheFile to update the note's
// content.
type CacheFile interface {
io.ReadWriteCloser
FilePath() string
ReOpen() error
CloseAndRemove() error
}
// FileCacheFile implements the CacheFile interface and uses
// a temporary file for storing the data on disk.
type FileCacheFile struct {
file *os.File
fp string
}
// Read returns content from the file.
func (f *FileCacheFile) Read(p []byte) (n int, err error) {
return f.file.Read(p)
}
// Write adds content to the file.
func (f *FileCacheFile) Write(p []byte) (n int, err error) {
return f.file.Write(p)
}
// Close closes the file.
// This should be called before the file is edited
// by the editor.
func (f *FileCacheFile) Close() error {
return f.file.Close()
}
// CloseAndRemove closes the file and removes it.
func (f *FileCacheFile) CloseAndRemove() error {
err := f.Close()
if err != nil {
return err
}
return os.Remove(f.fp)
}
// ReOpen opens the file again after it's been closed.
// This should be called after the file has been edited.
func (f *FileCacheFile) ReOpen() error {
file, err := os.OpenFile(f.fp, os.O_RDWR, 0600)
if err != nil {
return err
}
f.file = file
return nil
}
// FilePath returns the absolute path to the temporary file.
func (f *FileCacheFile) FilePath() string {
return f.fp
}
// MemoryCacheFile is a memory based representation of a cache file.
type MemoryCacheFile struct {
buf *bytes.Buffer
pipePath string
}
// Read returns bytes from the memory buffer.
func (m *MemoryCacheFile) Read(p []byte) (n int, err error) {
return m.buf.Read(p)
}
// Write adds bytes to the memory buffer.
func (m *MemoryCacheFile) Write(p []byte) (n int, err error) {
return m.buf.Write(p)
}
// Close handles the close call.
func (m *MemoryCacheFile) Close() error {
return nil
}
// FilePath returns the path to the FIFO pipe that should be used to
// send and receive data to and from the Editer.
func (m *MemoryCacheFile) FilePath() string {
return m.pipePath
}
// ReOpen handles the ReOpen call.
func (m *MemoryCacheFile) ReOpen() error {
return nil
}
// CloseAndRemove deletes the named FIFO pipe.
func (m *MemoryCacheFile) CloseAndRemove() error {
return os.RemoveAll(m.pipePath)
}