Browse Source

Remove server dependencies as Drone does not support services

master
Tyler 3 years ago
parent
commit
4af5b3af83
  1. 4
      .drone.yml
  2. 9
      cache.go
  3. 2
      memcache.go
  4. 21
      memcache_test.go
  5. 23
      redis_test.go

4
.drone.yml

@ -3,10 +3,6 @@ type: docker
name: default
steps:
- name: redis
image: redis:latest
- name: memcache
image: memcached:latest
- name: test
image: golang
commands:

9
cache.go

@ -3,6 +3,7 @@ package cache
import (
"errors"
"github.com/vmihailenco/msgpack/v4"
"net"
"net/url"
"strconv"
"strings"
@ -37,8 +38,14 @@ func New(uri string) (CacheInterface, error) {
switch u.Scheme {
case Redis:
port := u.Port()
if port == "" {
port = "6379"
}
return NewRedisCache(RedisSettings{
Address: u.Host,
Address: net.JoinHostPort(u.Hostname(), port),
Password: query.Get("password"),
})
case Memcache:

2
memcache.go

@ -10,6 +10,7 @@ type MemcacheSettings struct {
}
type MemcacheCache struct {
servers []string
backend *memcache.Client
}
@ -17,6 +18,7 @@ func NewMemcacheCache(s MemcacheSettings) (CacheInterface, error) {
c := memcache.New(s.Servers...)
return &MemcacheCache{
servers: s.Servers,
backend: c,
}, nil
}

21
memcache_test.go

@ -2,11 +2,10 @@ package cache
import (
"testing"
"time"
)
func Test_MemcacheURI(t *testing.T) {
cache, err := New("memcache://memcache")
cache, err := New("memcache://localhost")
if err != nil {
t.Fatal("Error creating cache:", err)
@ -17,22 +16,20 @@ func Test_MemcacheURI(t *testing.T) {
}
}
func Test_MemcacheStoreGet(t *testing.T) {
cache, err := New("memcache://memcache")
func Test_MemcacheURIMultipleServers(t *testing.T) {
cache, err := New("memcache://localhost,localhost2")
if err != nil {
t.Fatal("Error creating cache:", err)
}
obj := "test"
c, ok := cache.(*MemcacheCache)
cache.Set("test", obj, time.Minute)
var new string
cache.Get("test", &new)
if !ok {
t.Fatal("Cache is not instance of MemcacheCache")
}
if obj != new {
t.Fatal("Expected", obj, "got", new)
if len(c.servers) != 2 {
t.Fatal("Number of servers does not match!")
}
}

23
redis_test.go

@ -2,11 +2,10 @@ package cache
import (
"testing"
"time"
)
func Test_RedisURI(t *testing.T) {
cache, err := New("redis://redis")
cache, err := New("redis://127.0.0.1:6389")
if err != nil {
t.Fatal("Error creating cache:", err)
@ -16,23 +15,3 @@ func Test_RedisURI(t *testing.T) {
t.Fatal("Cache is not instance of RedisCache")
}
}
func Test_RedisStoreGet(t *testing.T) {
cache, err := New("redis://redis")
if err != nil {
t.Fatal("Error creating cache:", err)
}
obj := "test"
cache.Set("test", obj, time.Minute)
var new string
cache.Get("test", &new)
if obj != new {
t.Fatal("Expected", obj, "got", new)
}
}

Loading…
Cancel
Save