vendor files

This commit is contained in:
Serguei Bezverkhi
2018-01-09 13:57:14 -05:00
parent 558bc6c02a
commit 7b24313bd6
16547 changed files with 4527373 additions and 0 deletions

View 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}
}

View 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")
}
}

View 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}
}

View 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")
}
}