Commit 11d671c6 by bergquist

add support for memcached

parent 5ced863f
......@@ -39,12 +39,12 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
}
if opts.name == "memcache" {
return nil
return newMemcacheStorage("localhost:9090")
}
if opts.name == "memory" {
return nil
}
// if opts.name == "memory" {
// return nil
// }
return newDatabaseCache(sqlstore) //&databaseCache{SQLStore: sqlstore}
}
......
......@@ -27,7 +27,7 @@ func createTestClient(t *testing.T, name string) cacheStorage {
}
func TestAllCacheClients(t *testing.T) {
clients := []string{"database", "redis"} // add redis, memcache, memory
clients := []string{"database", "redis", "memcached"} // add redis, memcache, memory
for _, v := range clients {
client := createTestClient(t, v)
......
package distcache
import (
"time"
"github.com/bradfitz/gomemcache/memcache"
)
type memcacheStorage struct {
c *memcache.Client
}
func newMemcacheStorage(connStr string) *memcacheStorage {
return &memcacheStorage{
c: memcache.New(connStr),
}
}
func NewItem(sid string, data []byte, expire int32) *memcache.Item {
return &memcache.Item{
Key: sid,
Value: data,
Expiration: expire,
}
}
// Set sets value to given key in the cache.
func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration) error {
item := &Item{Val: val}
bytes, err := EncodeGob(item)
if err != nil {
return err
}
memcacheItem := NewItem(key, bytes, int32(expires))
s.c.Add(memcacheItem)
return nil
}
// Get gets value by given key in the cache.
func (s *memcacheStorage) Get(key string) (interface{}, error) {
i, err := s.c.Get(key)
if err != nil {
return nil, err
}
item := &Item{}
err = DecodeGob(i.Value, item)
if err != nil {
return nil, err
}
return item.Val, nil
}
// Delete delete a key from the cache
func (s *memcacheStorage) Delete(key string) error {
return s.c.Delete(key)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment