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:
43
vendor/github.com/gregjones/httpcache/redis/redis.go
generated
vendored
Normal file
43
vendor/github.com/gregjones/httpcache/redis/redis.go
generated
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
// Package redis provides a redis interface for http caching.
|
||||
package redis
|
||||
|
||||
import (
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"github.com/gregjones/httpcache"
|
||||
)
|
||||
|
||||
// cache is an implementation of httpcache.Cache that caches responses in a
|
||||
// redis server.
|
||||
type cache struct {
|
||||
redis.Conn
|
||||
}
|
||||
|
||||
// cacheKey modifies an httpcache key for use in redis. Specifically, it
|
||||
// prefixes keys to avoid collision with other data stored in redis.
|
||||
func cacheKey(key string) string {
|
||||
return "rediscache:" + key
|
||||
}
|
||||
|
||||
// Get returns the response corresponding to key if present.
|
||||
func (c cache) Get(key string) (resp []byte, ok bool) {
|
||||
item, err := redis.Bytes(c.Do("GET", cacheKey(key)))
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return item, true
|
||||
}
|
||||
|
||||
// Set saves a response to the cache as key.
|
||||
func (c cache) Set(key string, resp []byte) {
|
||||
c.Do("SET", cacheKey(key), resp)
|
||||
}
|
||||
|
||||
// Delete removes the response with key from the cache.
|
||||
func (c cache) Delete(key string) {
|
||||
c.Do("DEL", cacheKey(key))
|
||||
}
|
||||
|
||||
// NewWithClient returns a new Cache with the given redis connection.
|
||||
func NewWithClient(client redis.Conn) httpcache.Cache {
|
||||
return cache{client}
|
||||
}
|
43
vendor/github.com/gregjones/httpcache/redis/redis_test.go
generated
vendored
Normal file
43
vendor/github.com/gregjones/httpcache/redis/redis_test.go
generated
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/garyburd/redigo/redis"
|
||||
)
|
||||
|
||||
func TestRedisCache(t *testing.T) {
|
||||
conn, err := redis.Dial("tcp", "localhost:6379")
|
||||
if err != nil {
|
||||
// TODO: rather than skip the test, fall back to a faked redis server
|
||||
t.Skipf("skipping test; no server running at localhost:6379")
|
||||
}
|
||||
conn.Do("FLUSHALL")
|
||||
|
||||
cache := NewWithClient(conn)
|
||||
|
||||
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