Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ client := memc.New(
)
```

If no instances are provided, the client defaults to the local memcached
instance at `127.0.0.1:11211`.

##### Setting a value in memcached.

```go
Expand Down
6 changes: 5 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,20 @@ const (
defaultExpiration = 1 * time.Hour
defaultIdleCount = 1
defaultIOTimeout = 0
defaultInstance = "127.0.0.1:11211"
)

// New creates a new Client capable of sharding across the given set of
// instances and pooling connections to each instance.
//
// If no instances are provided, the client defaults to the local memcached
// instance at 127.0.0.1:11211.
//
// Certain behaviors can be configured by specifying one or more ClientOption
// options.
func New(instances []string, opts ...ClientOption) *Client {
c := new(Client)
c.addrs = instances
c.addrs = lang.Maybe(len(instances) > 0, instances, []string{defaultInstance})
c.timeout = defaultDialTimeout
c.iotimeout = defaultIOTimeout
c.expiration = defaultExpiration
Expand Down
19 changes: 19 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ func Test_SetIOTimeout(t *testing.T) {
})
}

func Test_New_default_instance(t *testing.T) {
t.Parallel()

t.Run("nil", func(t *testing.T) {
c := New(nil)
must.Eq(t, []string{defaultInstance}, c.addrs)
})

t.Run("empty", func(t *testing.T) {
c := New([]string{})
must.Eq(t, []string{defaultInstance}, c.addrs)
})

t.Run("provided", func(t *testing.T) {
c := New([]string{"localhost:11211"})
must.Eq(t, []string{"localhost:11211"}, c.addrs)
})
}

func Test_SetDefaultTTL(t *testing.T) {
t.Parallel()

Expand Down
Loading