Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Append #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ func Store(key string, value interface{}, expiryInSeconds int) error {
return nil
}

// Append stores key-value pairs in Redis with expiry
func Append(key string, value interface{}, expiryInSeconds int) error {
conn := Pool.Get()

conn.Send("MULTI")
conn.Send("APPEND", key, value)

if expiryInSeconds > 0 {
conn.Send("EXPIRE", key, expiryInSeconds)
}

_, err := conn.Do("EXEC")
if err != nil {
return err
}

return nil
}

// Retrieve retrieves value by key
func Retrieve(key string) (interface{}, error) {
conn := Pool.Get()
Expand Down
13 changes: 13 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,16 @@ func TestRedis_Delete_Error(t *testing.T) {
err := redis.Delete("key")
assert.Equal(t, "ERR", err.Error())
}

func TestRedis_Append_String(t *testing.T) {
expiryInSeconds := 10
mockRedisConn.Clear()
mockRedisConn.Command("MULTI")
mockRedisConn.Command("APPEND", "key", "value")
mockRedisConn.Command("EXPIRE", "key", expiryInSeconds)
mockRedisConn.Command("EXEC")
err := redis.Append("key", "value", expiryInSeconds)
assert.Nil(t, err)
err2 := redis.Append("key", "value", expiryInSeconds)
assert.Nil(t, err2)
}