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
30 changes: 20 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,29 @@ func (c *Client) Close() error {
// expiration is more than 30 days (2_592_000 seconds), in which case the
// absolute timestamp is used and expected by the memcached instance
func (c *Client) seconds(expiration time.Duration) (int, error) {
return c.duration(expiration, ErrExpiration)
}

// flushDelay returns the number of seconds to wait before flushing, unless the
// delay is more than 30 days (2_592_000 seconds), in which case the absolute
// timestamp is used and expected by the memcached instance
func (c *Client) flushDelay(timeout time.Duration) (int, error) {
return c.duration(timeout, ErrFlushDelay)
}

// duration converts a duration into the value expected by the memcached
// instance, returning the given invalid error when the duration is non-zero
// but less than one second.
func (c *Client) duration(d time.Duration, invalid error) (int, error) {
switch {
case expiration == 0:
case d == 0:
return 0, nil
case expiration < 1*time.Second:
return 0, ErrExpiration
case expiration > 2_592_000*time.Second:
unix := c.now()
later := unix.Add(expiration)
s := int(later.Unix())
return s, nil
case d < 1*time.Second:
return 0, invalid
case d > 2_592_000*time.Second:
return int(c.now().Add(d).Unix()), nil
default:
s := int(expiration.Seconds())
return s, nil
return int(d.Seconds()), nil
}
}

Expand Down
41 changes: 41 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,47 @@ func Test_seconds(t *testing.T) {
})
}

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

c := &Client{
now: func() time.Time {
// January 23rd, 2026, 10:24:00 AM
return time.Date(2026, 1, 23, 10, 24, 0, 0, time.UTC)
},
}

t.Run("zero", func(t *testing.T) {
s, err := c.flushDelay(0)
must.NoError(t, err)
must.Zero(t, s)
})

t.Run("millis", func(t *testing.T) {
_, err := c.flushDelay(250 * time.Millisecond)
must.ErrorIs(t, err, ErrFlushDelay)
})

t.Run("negative", func(t *testing.T) {
_, err := c.flushDelay(-time.Second)
must.ErrorIs(t, err, ErrFlushDelay)
})

t.Run("seconds", func(t *testing.T) {
s, err := c.flushDelay(4 * time.Second)
must.NoError(t, err)
must.Eq(t, 4, s)
})

t.Run("longer", func(t *testing.T) {
ttl := 30 * 24 * time.Hour
fix := ttl + (1 * time.Second)
s, err := c.flushDelay(fix)
must.NoError(t, err)
must.Eq(t, 1771755841, s) // February 22, 2026 10:24:01 AM
})
}

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

Expand Down
7 changes: 5 additions & 2 deletions verbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
ErrNotFound = errors.New("memc: item not found")
ErrConflict = errors.New("memc: CAS conflict")
ErrExpiration = errors.New("memc: expiration ttl is not valid")
ErrFlushDelay = errors.New("memc: flush delay is not valid")
ErrClientClosed = errors.New("memc: client has been closed")
ErrNegativeInc = errors.New("memc: increment delta must be non-negative")
ErrNonNumeric = errors.New("memc: cannot increment non-numeric value")
Expand Down Expand Up @@ -713,15 +714,17 @@ func getPayloadWithCAS(r *bufio.Reader) ([]byte, uint64, error) {
// Flush will delete all items from memcached.
//
// The timeout parameter is optional. A timeout of 0 means flush right now.
// A non-zero timeout will delay the flush by that many seconds.
// A non-zero timeout up to 30 days will delay the flush by that many seconds.
// A timeout greater than 30 days is treated as an absolute Unix timestamp.
// A timeout less than 1 second (other than 0) returns ErrFlushDelay.
//
// Note: this operation is performed on a single memcached server, even when
// the Client is configured with multiple server addresses. This is intentional,
// as flush is typically used by local administration tools that connect to a
// single memcached instance.
func (c *Client) Flush(timeout time.Duration) error {
return c.do("", func(conn *iopool.Buffer) error {
expiration, err := c.seconds(timeout)
expiration, err := c.flushDelay(timeout)
if err != nil {
return err
}
Expand Down
Loading