diff --git a/client.go b/client.go index 525061f..1c98c17 100644 --- a/client.go +++ b/client.go @@ -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 } } diff --git a/client_test.go b/client_test.go index b353201..356eaeb 100644 --- a/client_test.go +++ b/client_test.go @@ -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() diff --git a/verbs.go b/verbs.go index 74ddd69..5f97db3 100644 --- a/verbs.go +++ b/verbs.go @@ -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") @@ -713,7 +714,9 @@ 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, @@ -721,7 +724,7 @@ func getPayloadWithCAS(r *bufio.Reader) ([]byte, uint64, error) { // 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 }