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:
61
vendor/github.com/gregjones/httpcache/memcache/appengine.go
generated
vendored
Normal file
61
vendor/github.com/gregjones/httpcache/memcache/appengine.go
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
// +build appengine
|
||||
|
||||
// Package memcache provides an implementation of httpcache.Cache that uses App
|
||||
// Engine's memcache package to store cached responses.
|
||||
//
|
||||
// When not built for Google App Engine, this package will provide an
|
||||
// implementation that connects to a specified memcached server. See the
|
||||
// memcache.go file in this package for details.
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"appengine"
|
||||
"appengine/memcache"
|
||||
)
|
||||
|
||||
// Cache is an implementation of httpcache.Cache that caches responses in App
|
||||
// Engine's memcache.
|
||||
type Cache struct {
|
||||
appengine.Context
|
||||
}
|
||||
|
||||
// cacheKey modifies an httpcache key for use in memcache. Specifically, it
|
||||
// prefixes keys to avoid collision with other data stored in memcache.
|
||||
func cacheKey(key string) string {
|
||||
return "httpcache:" + key
|
||||
}
|
||||
|
||||
// Get returns the response corresponding to key if present.
|
||||
func (c *Cache) Get(key string) (resp []byte, ok bool) {
|
||||
item, err := memcache.Get(c.Context, cacheKey(key))
|
||||
if err != nil {
|
||||
if err != memcache.ErrCacheMiss {
|
||||
c.Context.Errorf("error getting cached response: %v", err)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
return item.Value, true
|
||||
}
|
||||
|
||||
// Set saves a response to the cache as key.
|
||||
func (c *Cache) Set(key string, resp []byte) {
|
||||
item := &memcache.Item{
|
||||
Key: cacheKey(key),
|
||||
Value: resp,
|
||||
}
|
||||
if err := memcache.Set(c.Context, item); err != nil {
|
||||
c.Context.Errorf("error caching response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete removes the response with key from the cache.
|
||||
func (c *Cache) Delete(key string) {
|
||||
if err := memcache.Delete(c.Context, cacheKey(key)); err != nil {
|
||||
c.Context.Errorf("error deleting cached response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// New returns a new Cache for the given context.
|
||||
func New(ctx appengine.Context) *Cache {
|
||||
return &Cache{ctx}
|
||||
}
|
44
vendor/github.com/gregjones/httpcache/memcache/appengine_test.go
generated
vendored
Normal file
44
vendor/github.com/gregjones/httpcache/memcache/appengine_test.go
generated
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
// +build appengine
|
||||
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"appengine/aetest"
|
||||
)
|
||||
|
||||
func TestAppEngine(t *testing.T) {
|
||||
ctx, err := aetest.NewContext(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ctx.Close()
|
||||
|
||||
cache := New(ctx)
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
60
vendor/github.com/gregjones/httpcache/memcache/memcache.go
generated
vendored
Normal file
60
vendor/github.com/gregjones/httpcache/memcache/memcache.go
generated
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
// +build !appengine
|
||||
|
||||
// Package memcache provides an implementation of httpcache.Cache that uses
|
||||
// gomemcache to store cached responses.
|
||||
//
|
||||
// When built for Google App Engine, this package will provide an
|
||||
// implementation that uses App Engine's memcache service. See the
|
||||
// appengine.go file in this package for details.
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"github.com/bradfitz/gomemcache/memcache"
|
||||
)
|
||||
|
||||
// Cache is an implementation of httpcache.Cache that caches responses in a
|
||||
// memcache server.
|
||||
type Cache struct {
|
||||
*memcache.Client
|
||||
}
|
||||
|
||||
// cacheKey modifies an httpcache key for use in memcache. Specifically, it
|
||||
// prefixes keys to avoid collision with other data stored in memcache.
|
||||
func cacheKey(key string) string {
|
||||
return "httpcache:" + key
|
||||
}
|
||||
|
||||
// Get returns the response corresponding to key if present.
|
||||
func (c *Cache) Get(key string) (resp []byte, ok bool) {
|
||||
item, err := c.Client.Get(cacheKey(key))
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return item.Value, true
|
||||
}
|
||||
|
||||
// Set saves a response to the cache as key.
|
||||
func (c *Cache) Set(key string, resp []byte) {
|
||||
item := &memcache.Item{
|
||||
Key: cacheKey(key),
|
||||
Value: resp,
|
||||
}
|
||||
c.Client.Set(item)
|
||||
}
|
||||
|
||||
// Delete removes the response with key from the cache.
|
||||
func (c *Cache) Delete(key string) {
|
||||
c.Client.Delete(cacheKey(key))
|
||||
}
|
||||
|
||||
// New returns a new Cache using the provided memcache server(s) with equal
|
||||
// weight. If a server is listed multiple times, it gets a proportional amount
|
||||
// of weight.
|
||||
func New(server ...string) *Cache {
|
||||
return NewWithClient(memcache.New(server...))
|
||||
}
|
||||
|
||||
// NewWithClient returns a new Cache with the given memcache client.
|
||||
func NewWithClient(client *memcache.Client) *Cache {
|
||||
return &Cache{client}
|
||||
}
|
47
vendor/github.com/gregjones/httpcache/memcache/memcache_test.go
generated
vendored
Normal file
47
vendor/github.com/gregjones/httpcache/memcache/memcache_test.go
generated
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
// +build !appengine
|
||||
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testServer = "localhost:11211"
|
||||
|
||||
func TestMemCache(t *testing.T) {
|
||||
conn, err := net.Dial("tcp", testServer)
|
||||
if err != nil {
|
||||
// TODO: rather than skip the test, fall back to a faked memcached server
|
||||
t.Skipf("skipping test; no server running at %s", testServer)
|
||||
}
|
||||
conn.Write([]byte("flush_all\r\n")) // flush memcache
|
||||
conn.Close()
|
||||
|
||||
cache := New(testServer)
|
||||
|
||||
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