| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"os" |
| 6 |
"path" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 11 |
"github.com/syndtr/goleveldb/leveldb" |
| 12 |
"github.com/syndtr/goleveldb/leveldb/util" |
| 13 |
) |
| 14 |
|
| 15 |
type JMAPCache struct { |
| 16 |
mem map[string][]byte |
| 17 |
file *leveldb.DB |
| 18 |
blobsDir string |
| 19 |
} |
| 20 |
|
| 21 |
func NewJMAPCache(state, blobs bool, accountName string) *JMAPCache { |
| 22 |
c := new(JMAPCache) |
| 23 |
cacheDir := xdg.CachePath() |
| 24 |
if state && cacheDir != "" { |
| 25 |
var err error |
| 26 |
dir := path.Join(cacheDir, "aerc", accountName, "state") |
| 27 |
_ = os.MkdirAll(dir, 0o700) |
| 28 |
c.file, err = leveldb.OpenFile(dir, nil) |
| 29 |
if err != nil { |
| 30 |
log.Errorf("failed to open goleveldb: %s", err) |
| 31 |
c.mem = make(map[string][]byte) |
| 32 |
} |
| 33 |
} else { |
| 34 |
c.mem = make(map[string][]byte) |
| 35 |
} |
| 36 |
if blobs && cacheDir != "" { |
| 37 |
c.blobsDir = path.Join(cacheDir, "aerc", accountName, "blobs") |
| 38 |
} |
| 39 |
return c |
| 40 |
} |
| 41 |
|
| 42 |
var notfound = errors.New("key not found") |
| 43 |
|
| 44 |
func (c *JMAPCache) get(key string) ([]byte, error) { |
| 45 |
switch { |
| 46 |
case c.file != nil: |
| 47 |
return c.file.Get([]byte(key), nil) |
| 48 |
case c.mem != nil: |
| 49 |
value, ok := c.mem[key] |
| 50 |
if !ok { |
| 51 |
return nil, notfound |
| 52 |
} |
| 53 |
return value, nil |
| 54 |
} |
| 55 |
panic("jmap cache with no backend") |
| 56 |
} |
| 57 |
|
| 58 |
func (c *JMAPCache) put(key string, value []byte) error { |
| 59 |
switch { |
| 60 |
case c.file != nil: |
| 61 |
return c.file.Put([]byte(key), value, nil) |
| 62 |
case c.mem != nil: |
| 63 |
c.mem[key] = value |
| 64 |
return nil |
| 65 |
} |
| 66 |
panic("jmap cache with no backend") |
| 67 |
} |
| 68 |
|
| 69 |
func (c *JMAPCache) delete(key string) error { |
| 70 |
switch { |
| 71 |
case c.file != nil: |
| 72 |
return c.file.Delete([]byte(key), nil) |
| 73 |
case c.mem != nil: |
| 74 |
delete(c.mem, key) |
| 75 |
return nil |
| 76 |
} |
| 77 |
panic("jmap cache with no backend") |
| 78 |
} |
| 79 |
|
| 80 |
func (c *JMAPCache) purge(prefix string) error { |
| 81 |
switch { |
| 82 |
case c.file != nil: |
| 83 |
txn, err := c.file.OpenTransaction() |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
iter := txn.NewIterator(util.BytesPrefix([]byte(prefix)), nil) |
| 88 |
for iter.Next() { |
| 89 |
err = txn.Delete(iter.Key(), nil) |
| 90 |
if err != nil { |
| 91 |
break |
| 92 |
} |
| 93 |
} |
| 94 |
iter.Release() |
| 95 |
if err != nil { |
| 96 |
txn.Discard() |
| 97 |
return err |
| 98 |
} |
| 99 |
return txn.Commit() |
| 100 |
case c.mem != nil: |
| 101 |
for key := range c.mem { |
| 102 |
if strings.HasPrefix(key, prefix) { |
| 103 |
delete(c.mem, key) |
| 104 |
} |
| 105 |
} |
| 106 |
return nil |
| 107 |
} |
| 108 |
panic("jmap cache with no backend") |
| 109 |
} |