list

package
v0.0.0-...-9cb517b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2022 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package list contains a Go implementation of the list data structure in Redis. For more information about how the data structure works, see the Redis documentation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adjacency

type Adjacency string
const (
	Before Adjacency = "BEFORE"
	After            = "AFTER"
)

type List

type List interface {
	// Base returns the base Type.
	Base() redistypes.Type

	// BlockingLeftPop implements the Redis command BLPOP. It works like LPOP but it
	// blocks until an element exists in the list or timeout is reached. If the timeout
	// is reached, nil is returned. A timeout of 0 can be used to block indefinitely.
	//
	// Since Redis specifies timeout to be in seconds, millisecond-level precision is
	// not possible. If the timeout is not a multiple of one second, an error will be
	// returned.
	//
	// See https://redis.io/commands/blpop.
	BlockingLeftPop(timeout time.Duration) (interface{}, error)

	// BlockingRightPop implements the Redis command BRPOP. It works like RPOP but it
	// blocks until an element exists in the list or timeout is reached. If the timeout
	// is reached, nil is returned. A timeout of 0 can be used to block indefinitely.
	//
	// Since Redis specifies timeout to be in seconds, millisecond-level precision is
	// not possible. If the timeout is not a multiple of one second, an error will be
	// returned.
	//
	// See https://redis.io/commands/brpop.
	BlockingRightPop(timeout time.Duration) (interface{}, error)

	// BlockingRightPopLeftPush implements the Redis command BRPOPLPUSH. It works like
	// RightPopLeftPush except it blocks until timeout is reached. A timeout of 0 can
	// be used to block indefinitely.
	//
	// Since Redis specifies timeout to be in seconds, millisecond-level precision is
	// not possible. If the timeout is not a multiple of one second, an error will be
	// returned.
	//
	// See https://redis.io/commands/brpoplpush.
	BlockingRightPopLeftPush(destination List, timeout time.Duration) (interface{}, error)

	// Index implements the Redis command LINDEX. It returns the value at index in
	// the list. The index is 0-based, with the first index 0. Negative numbers
	// denote indices starting at the end of the list, as described by the documentation.
	//
	// See https://redis.io/commands/lindex.
	Index(index int64) (interface{}, error)

	// Insert implements the Redis command LINSERT. It inserts a value either before
	// or after the pivot, depending on adj. It returns the new length of the list,
	// or -1 if the pivot value wasn't found.
	//
	// See https://redis.io/commands/linsert.
	Insert(adj Adjacency, pivot interface{}, value interface{}) (int64, error)

	// LeftPop implements the Redis command LPOP. It pops the leftmost value from the
	// list and returns it. If no such value exists, it returns nil.
	//
	// See https://redis.io/commands/lpop.
	LeftPop() (interface{}, error)

	// LeftPush implements the Redis command LPUSH. It pushes one or more values onto
	// the left of the list. It returns an error or the total number of values in the
	// list.
	//
	// See https://redis.io/commands/lpush.
	LeftPush(args ...interface{}) (uint64, error)

	// LeftPushX implements the Redis command LPUSHX. It pushes one value onto the left
	// of a list that already exists. It returns an error or the total number of values
	// in the list. If the list doesn't already exist, it is not created and 0 is
	// returned.
	//
	// See https://redis.io/commands/lpushx.
	LeftPushX(arg interface{}) (uint64, error)

	// Length implements the Redis command LLEN. It returns the length of the list. If the
	// list does not already exist, it returns 0.
	//
	// See https://redis.io/commands/llen.
	Length() (uint64, error)

	// Range implements the Redis command LRANGE. It returns a range of values in the
	// list, starting at index start and ending at index stop. If end is negative, it
	// returns all values from start to the end of the list.
	//
	// See https://redis.io/commands/lrange.
	Range(start, stop int64) ([]interface{}, error)

	// Remove implements the Redis command LREM. It removes a number of occurrences of
	// value from the list. If count == 0, it removes all occurrences. If count > 0,
	// it removes the first count occurrences, starting from the beginning of the list.
	// If count < 0, it removes the first -count occurrences, starting from the end of
	// the list.
	//
	// Remove returns the number of values removed, or an error.
	//
	// See https://redis.io/commands/lrem.
	Remove(count int64, value interface{}) (uint64, error)

	// RightPop implements the Redis command RPOP. It pops the rightmost value from the
	// list and returns it. If no such value exists, it returns nil.
	//
	// See https://redis.io/commands/rpop.
	RightPop() (interface{}, error)

	// RightPopLeftPush implements the Redis command RPOPLPUSH. It pops the value on the
	// right of the list and pushes it on the left of destination.
	//
	// See https://redis.io/commands/rpoplpush.
	RightPopLeftPush(destination List) (interface{}, error)

	// RightPush implements the Redis command RPUSH. It pushes one or more values onto
	// the right of the list. It returns an error or the total number of values in the list.
	//
	// See https://redis.io/commands/rpush.
	RightPush(args ...interface{}) (uint64, error)

	// RightPushX implements the Redis command RPUSHX. It pushes one value onto the
	// right of a list that already exists. It returns an error or the total number of
	// values in the list. If the list doesn't already exist, it is not created and 0 is
	// returned.
	//
	// See https://redis.io/commands/rpushx.
	RightPushX(arg interface{}) (uint64, error)

	// Set implements the Redis command LSET. It sets the value at the specified index
	// to value. For more information about the index, see Index.
	//
	// See https://redis.io/commands/lset.
	Set(index int64, value interface{}) error

	// Trim implements the Redis command LTRIM. It trims the list so it contains only
	// the elements from start to stop. Negative values denote values starting from the
	// end of the list, as explained in the documentation.
	//
	// See https://redis.io/commands/ltrim.
	Trim(start, stop int64) error
}

List is a Redis implementation of a linked list.

func NewRedisList

func NewRedisList(conn redis.Conn, name string) List

NewRedisList creates a Redis implementation of List given redigo connection conn and name. The Redis key used to identify the List will be name.

Example
package main

import (
	"fmt"
	"net"
	"time"

	"github.com/MasterOfBinary/redistypes/internal"
	"github.com/MasterOfBinary/redistypes/internal/test"
	"github.com/MasterOfBinary/redistypes/list"
	"github.com/garyburd/redigo/redis"
)

func main() {
	netConn, _ := net.Dial("tcp", internal.GetHostAndPort())

	conn := redis.NewConn(netConn, time.Second, time.Second)
	defer conn.Close()

	l := list.NewRedisList(conn, test.RandomKey())

	values, _ := l.Range(0, -1)
	fmt.Println("Count:", len(values))

	_, _ = l.Base().Delete()

}
Output:

Count: 0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL