mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
vendor files
This commit is contained in:
51
vendor/github.com/gregjones/httpcache/leveldbcache/leveldbcache.go
generated
vendored
Normal file
51
vendor/github.com/gregjones/httpcache/leveldbcache/leveldbcache.go
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
// Package leveldbcache provides an implementation of httpcache.Cache that
|
||||
// uses github.com/syndtr/goleveldb/leveldb
|
||||
package leveldbcache
|
||||
|
||||
import (
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
)
|
||||
|
||||
// Cache is an implementation of httpcache.Cache with leveldb storage
|
||||
type Cache struct {
|
||||
db *leveldb.DB
|
||||
}
|
||||
|
||||
// Get returns the response corresponding to key if present
|
||||
func (c *Cache) Get(key string) (resp []byte, ok bool) {
|
||||
var err error
|
||||
resp, err = c.db.Get([]byte(key), nil)
|
||||
if err != nil {
|
||||
return []byte{}, false
|
||||
}
|
||||
return resp, true
|
||||
}
|
||||
|
||||
// Set saves a response to the cache as key
|
||||
func (c *Cache) Set(key string, resp []byte) {
|
||||
c.db.Put([]byte(key), resp, nil)
|
||||
}
|
||||
|
||||
// Delete removes the response with key from the cache
|
||||
func (c *Cache) Delete(key string) {
|
||||
c.db.Delete([]byte(key), nil)
|
||||
}
|
||||
|
||||
// New returns a new Cache that will store leveldb in path
|
||||
func New(path string) (*Cache, error) {
|
||||
cache := &Cache{}
|
||||
|
||||
var err error
|
||||
cache.db, err = leveldb.OpenFile(path, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cache, nil
|
||||
}
|
||||
|
||||
// NewWithDB returns a new Cache using the provided leveldb as underlying
|
||||
// storage.
|
||||
func NewWithDB(db *leveldb.DB) *Cache {
|
||||
return &Cache{db}
|
||||
}
|
46
vendor/github.com/gregjones/httpcache/leveldbcache/leveldbcache_test.go
generated
vendored
Normal file
46
vendor/github.com/gregjones/httpcache/leveldbcache/leveldbcache_test.go
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
package leveldbcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDiskCache(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "httpcache")
|
||||
if err != nil {
|
||||
t.Fatalf("TempDir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
cache, err := New(filepath.Join(tempDir, "db"))
|
||||
if err != nil {
|
||||
t.Fatalf("New leveldb,: %v", err)
|
||||
}
|
||||
|
||||
key := "testKey"
|
||||
_, ok := cache.Get(key)
|
||||
if ok {
|
||||
t.Fatal("retrieved key before adding it")
|
||||
}
|
||||
|
||||
val := []byte("some bytes")
|
||||
cache.Set(key, val)
|
||||
|
||||
retVal, ok := cache.Get(key)
|
||||
if !ok {
|
||||
t.Fatal("could not retrieve an element we just added")
|
||||
}
|
||||
if !bytes.Equal(retVal, val) {
|
||||
t.Fatal("retrieved a different value than what we put in")
|
||||
}
|
||||
|
||||
cache.Delete(key)
|
||||
|
||||
_, ok = cache.Get(key)
|
||||
if ok {
|
||||
t.Fatal("deleted key still present")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user