Skip to content
Open
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
14 changes: 14 additions & 0 deletions asynchclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ func (c *asyncClient) Setnx(arg0 string, arg1 []byte) (result FutureBool, err Er

}

// Redis SETEX command.
func (c *asyncClient) Setex(arg0 string, arg1 int64, arg2 []byte) (stat FutureBool, err Error) {
arg0bytes := []byte(arg0)
arg1bytes := []byte(fmt.Sprintf("%d", arg1))
arg2bytes := arg2

resp, err := c.conn.QueueRequest(&SETEX, [][]byte{arg0bytes, arg1bytes, arg2bytes})
if err == nil {
stat = resp.future.(FutureBool)
}

return
}

// Redis GETSET command.
func (c *asyncClient) Getset(arg0 string, arg1 []byte) (result FutureBytes, err Error) {
arg0bytes := []byte(arg0)
Expand Down
59 changes: 59 additions & 0 deletions examples/setex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2009 Joubin Houshyar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main

import (
"flag"
"fmt"
"log"
"redis"
)

// Test Setex command
func main() {

// Parse command-line flags; needed to let flags used by Go-Redis be parsed.
flag.Parse()

spec := redis.DefaultSpec().Db(13).Password("go-redis")
client, e := redis.NewSynchClientWithSpec(spec)
if e != nil {
log.Println("failed to create the client", e)
return
}

key := "expire:test"

if err := client.Setex(key, 120, []byte("dummy-key-value")); err != nil {
log.Println("error on Setex", err)
return
}

value, e := client.Get(key)
if e != nil {
log.Println("error on Get", e)
return
}

ttl, e := client.Ttl(key)
if e != nil {
log.Println("error on Ttl", e)
return
}

fmt.Printf("%s with value %s expires in %d seconds", key, value, ttl)

return
}
6 changes: 6 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type Client interface {
// Redis SETNX command.
Setnx(key string, arg1 []byte) (result bool, err Error)

// Redis SETEX command.
Setex(key string, arg1 int64, arg2 []byte) (err Error)

// Redis GETSET command.
Getset(key string, arg1 []byte) (result []byte, err Error)

Expand Down Expand Up @@ -370,6 +373,9 @@ type AsyncClient interface {
// Redis SETNX command.
Setnx(key string, arg1 []byte) (result FutureBool, err Error)

// Redis SETEX command.
Setex(key string, arg1 int64, arg2 []byte) (result FutureBool, err Error)

// Redis GETSET command.
Getset(key string, arg1 []byte) (result FutureBytes, err Error)

Expand Down
2 changes: 2 additions & 0 deletions specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
KEY_NUM
KEY_SPEC
KEY_NUM_NUM
KEY_NUM_VALUE
KEY_VALUE
KEY_IDX_VALUE
KEY_KEY_VALUE
Expand Down Expand Up @@ -126,6 +127,7 @@ var (
GETSET Command = Command{"GETSET", KEY_VALUE, BULK}
MGET Command = Command{"MGET", MULTI_KEY, MULTI_BULK}
SETNX Command = Command{"SETNX", KEY_VALUE, BOOLEAN}
SETEX Command = Command{"SETEX", KEY_NUM_VALUE, STATUS}
INCR Command = Command{"INCR", KEY, NUMBER}
INCRBY Command = Command{"INCRBY", KEY_NUM, NUMBER}
DECR Command = Command{"DECR", KEY, NUMBER}
Expand Down
11 changes: 11 additions & 0 deletions synchclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ func (c *syncClient) Setnx(arg0 string, arg1 []byte) (result bool, err Error) {

}

// Redis SETEX command.
func (c *syncClient) Setex(arg0 string, arg1 int64, arg2 []byte) (err Error) {
arg0bytes := []byte(arg0)
arg1bytes := []byte(fmt.Sprintf("%d", arg1))
arg2bytes := arg2

//var resp Response
_, err = c.conn.ServiceRequest(&SETEX, [][]byte{arg0bytes, arg1bytes, arg2bytes})
return
}

// Redis GETSET command.
func (c *syncClient) Getset(arg0 string, arg1 []byte) (result []byte, err Error) {
arg0bytes := []byte(arg0)
Expand Down
15 changes: 15 additions & 0 deletions synchclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ func TestSetnx(t *testing.T) {
flushAndQuitOnCompletion(t, client)
}

func TestSetex(t *testing.T) {
client, e := _test_getDefaultSyncClient()
if e != nil {
t.Fatalf("on getDefaultClient - %s", e)
}

for k, v := range testdata[_testdata_kv].(map[string][]byte) {
if e := client.Setex(k, 120, v); e != nil {
t.Errorf("on Setex(%s, 120, %s) - %s", k, v, e)
}
}

flushAndQuitOnCompletion(t, client)
}

func TestGetset(t *testing.T) {
client, e := _test_getDefaultSyncClient()
if e != nil {
Expand Down