List of basic data structures [Redis] redis

Summary:

Redis List data structure is a linked list type, similar LinkedList. So it is very efficient insertion, the time complexity is O (1). It's slow query efficiency, while O (n).

But in fact the internal Redis, list structure are not a simple linked list, because each node of the LinkedList must hold a pointer on a node and the next node, accounted for relatively more space than the array type list. In Redis, there is a list of the presence zipList compression element which uses a small amount of contiguous memory space, just as when the array, the memory can be saved, and the list structure is composed of a plurality of such composition zipList string together, are called fast lists quickList.

 

When the last element of the list structure is deleted, the list will also be redis released.

operating:

Adding elements from the head: LPUSH listName value1

From the tail adding elements: RPUSH listName value1

Get all the elements: lrange listName 0 -1   // Note that you need to traverse the list, with caution.

Removing elements: LREM KEY_NAME of VALUE COUNT

COUNT value may be the following:

count> 0: Header search starting from the end of the table, with equal removal VALUE element, the number of COUNT.
count <0: start the search from the head end of the table to the table, and remove VALUE equal to the element number of the absolute value of COUNT.
count = 0: remove VALUE equal to all values in the table.

 

Clear List LTRIM listName 1 0 :

 

Remove elements from the list head, and returns that element, lpop listName

Remove elements from the list tail, and returns that element, RPOP listName

 

Returns the number of elements LLEN listName :

Returns the specified index element, lindex listName <lindex> // iterate through the entire list, not efficient:

 

 From the above command to test, you can see list structure supports adding and removing elements at both ends, in fact, can be used to implement the queue (FIFO) and stack (last-out) structure:

lpush and rpop rpush and lpop or a combination, is a queue structure:

 

lpush and lpop rpush and rpop or in combination, it is a stack data structure:

 

 

go code to connect:

First download golang-party libraries to connect the redis:

go get github.com/garyburd/redigo/redis

 Then write code:

package main

import (
	"Github.com/garyburd/redigo/redis"
	"fmt"
)

func main(){

	// connection redis
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		fmt.Errorf("conn redis failed, error info:", err)
		return
	}

	// lpush
	_, err = c.Do("lpush", "country", "China")
	if err != nil {
		fmt.Errorf("lpush failed. error info: ", err)
		return
	}

	// lpush
	_, err = c.Do("lpush", "country", "United States")
	if err != nil {
		fmt.Errorf("lpush failed error info: ", err)
		return
	}

	// lpush
	_, err = c.Do("lpush", "country", "Russia")
	if err != nil {
		fmt.Errorf("lpush failed error info: ", err)
		return
	}

	// lpop
	contryName, err1 := redis.String(c.Do("lpop", "country"))
	if err1 != nil {
		fmt.Errorf("lpop failed error info: ", err1)
		return
	}
	fmt.Println("the contry name is: ", contryName)

	// len
	len, err2 := c.Do("llen", "country")
	if err2 != nil {
		fmt.Errorf("llen failed error info: ", err2)
		return
	}
	fmt.Println("the contrys length is: ", len)

	defer c.Close()
}

run:

 

Guess you like

Origin www.cnblogs.com/wuyizuokan/p/11074919.html
Recommended