mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
feat(cache): implement Delete
This commit is contained in:
parent
cd5c6c6a72
commit
0b04ec9adb
@ -498,6 +498,27 @@ func (d *DB) Get(hash *byte) []byte {
|
||||
}
|
||||
|
||||
// Delete remove item with the given key 'hash' from the database file.
|
||||
func (d *DB) Delete(sha1 *byte) error {
|
||||
return errors.New("impl me")
|
||||
func (d *DB) Delete(hash *byte) error {
|
||||
var h [hashSize]byte
|
||||
copyhash(&h[0], hash)
|
||||
|
||||
off := d.delete(d.top, &h[0])
|
||||
if off == 0 {
|
||||
return nil // not found key
|
||||
}
|
||||
|
||||
d.top = collapse(d, d.top)
|
||||
freeQueued(d)
|
||||
d.flushSuper()
|
||||
|
||||
d.fd.Seek(off, io.SeekStart)
|
||||
length, err := read32(d.fd) // len: 0
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "btree I/O error")
|
||||
}
|
||||
|
||||
d.freeChunk(off, int(length+4))
|
||||
freeQueued(d)
|
||||
d.flushSuper()
|
||||
return nil
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func tempfile(t *testing.T) string {
|
||||
temp, err := os.CreateTemp("", "temp.*.db")
|
||||
temp, err := os.CreateTemp(".", "temp.*.db")
|
||||
assert2.NoError(t, temp.Close())
|
||||
assert2.NoError(t, err)
|
||||
return temp.Name()
|
||||
@ -48,9 +48,14 @@ func TestBtree(t *testing.T) {
|
||||
for i, tt := range tests {
|
||||
assert2.Equal(t, []byte(tt), bt.Get(sha[i]))
|
||||
}
|
||||
assert2.NoError(t, bt.Close())
|
||||
|
||||
for i := range tests {
|
||||
assert2.NoError(t, bt.Delete(sha[i]))
|
||||
}
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
println(tableSize)
|
||||
for i := range tests {
|
||||
assert2.Equal(t, []byte(nil), bt.Get(sha[i]))
|
||||
}
|
||||
|
||||
assert2.NoError(t, bt.Close())
|
||||
}
|
||||
|
10
internal/cache/cache.go
vendored
10
internal/cache/cache.go
vendored
@ -50,6 +50,16 @@ func (c *Cache) Get(md5 []byte) []byte {
|
||||
return c.db.Get(&hash[0])
|
||||
}
|
||||
|
||||
// Delete 删除指定缓存
|
||||
func (c *Cache) Delete(md5 []byte) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
var hash [16]byte
|
||||
copy(hash[:], md5)
|
||||
_ = c.db.Delete(&hash[0])
|
||||
}
|
||||
|
||||
// Init 初始化 Cache
|
||||
func Init() {
|
||||
node, ok := base.Database["cache"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user