From 119fdacd4a47486247eff5ab0e349070219b36f6 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Sun, 29 Nov 2026 18:04:23 -0600 Subject: [PATCH] config: default to localhost and standard port --- README.md | 3 +++ client.go | 6 +++++- client_test.go | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 908b45c..534c2ee 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/client.go b/client.go index 07016e6..901718c 100644 --- a/client.go +++ b/client.go @@ -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 diff --git a/client_test.go b/client_test.go index a9e9fe7..48c0e76 100644 --- a/client_test.go +++ b/client_test.go @@ -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()