Redis (i) Installation and basic data type operations

Source: Internet
Author: User
Tags delete key

Redis Installation and useRedis Installation
wget http://download.redis.io/redis-stable.tar.gztar zxvf redis-stable.tar.gzcd redis-stable.tar.gzmakemake Install
Redis Boot
Redis-server
Redis off
REDIS-CLI shutdown
Configuration

Method One

redis-server/path/to/redis.confredis-server/path/to/redis.conf--loglevel warning//set log level

Method Two

Redis-cliredis > CONFIG SET loglevel Warning
selection of multiple databasesBy default, the client automatically selects database number No. 0. You can use the Select command to select a different database.
> select 1
Redis can not set the name of the database, which is used by number. Redis has only one global password and no password to access a database。 Basic Database Operations(a) string

(1) Add data and find data, add and get data by Set/get command

127.0.0.1:6379> Select 1ok127.0.0.1:6379[1]> Set bar 1ok127.0.0.1:6379[1]> get Bar "1" 127.0.0.1:6379[1]> Get AAA (Nil)

(2) to determine whether a key exists, the exists command can determine whether the key exists, there is return 1, there is no return 0

127.0.0.1:6379[1]> exists bar (integer) 1127.0.0.1:6379[1]> exists aaa (integer) 0

(3) Delete data, del command Delete key, return value is the number of successful delete key

127.0.0.1:6379[1]> del aaa (integer) 0127.0.0.1:6379[1]> del Bar (integer) 1

(4) Get the type of value

127.0.0.1:6379[1]> Set bar 1ok127.0.0.1:6379[1]> type barstring

(5) Self-increment and decrement, incr, DECR and Incrby and Decrby achieve integer addition and subtraction

127.0.0.1:6379[1]> INCR AB (integer) 1127.0.0.1:6379[1]> INCR ab (integer) 2127.0.0.1:6379[1]> Incrby AB 2 ( Integer) 4127.0.0.1:6379[1]> decr ab (integer) 3127.0.0.1:6379[1]> decrby ab 2 (integer) 1

(6) Increase the specified floating-point number, incrbyfloat can specify the self-increment floating point number

127.0.0.1:6379[1]> Incrbyfloat AB 1.1 "2.1"

(7) Append to tail, append command to append content to trailing character

127.0.0.1:6379[1]> set key hellook127.0.0.1:6379[1]> append key World (integer) 10127.0.0.1:6379[1]> get Key " HelloWorld

(8) Gets the length of the string, strlen gets the length of the value

127.0.0.1:6379[1]> STRLEN Key (integer) 10

(9) Batch setup and acquisition, through Mset and Mget commands can be executed in batch settings and get

127.0.0.1:6379[1]> mset key1 va1 key2 va2ok127.0.0.1:6379[1]> mget key1 key21) "VA1" 2) "VA2"

(10) Bit operation

Getbit key Offsetsetbit key Offsetbitcount key [start] [end]bitop operation Destkey key [key ...]

(11) Total query data

Dbsize

Keys *

(ii) hash type (hashes)In fact, the hash type here is similar to unstructured data, like JSON data.
{    "id": 1,    "name": "Test"}
Basic operations

(1) Storage method, can set up and get data through Hset and Hget

(2) Determine if the field exists, Hexists command, return 1 exists, return 0 does not exist

 

(3) Use HSETNX (Hset if not exists) if no assignment exists

  

(4) Increase the number

127.0.0.1:6379[1]> Hincrby User Score (integer) 60127.0.0.1:6379[1]> hget user Score "60"

(5) Delete a field

127.0.0.1:6379[1]> Hdel User Score (integer) 1127.0.0.1:6379[1]> hget user score (nil)
How do I store data? (1) Use hash type to store data, hash data includes three parts (Key, field, field value)
Key           field     field values post:id field value     
127.0.0.1:6379[1]> incr userid (integer) 1127.0.0.1:6379[1]> hset user:1 name Test (integer) 1127.0.0.1:6379[1] > Hset user:1 Score (integer) 1127.0.0.1:6379[1]> incr userid (integer) 2127.0.0.1:6379[1]> hset user:2 name tes T2 (integer) 1127.0.0.1:6379[1]> hset user:2 Score (integer) 1

Two data were added, with the name of ID 1 being the test score of 90, and the ID of 2 with the test2 score of 90.

(2) To get data for multiple fields, use the Hmget command and make a field name
127.0.0.1:6379[1]> hmget user:1 name score1) "Test" 2) "90"
(3) Get a row of data, do not need to specify the field name, only need to indicate the key name
127.0.0.1:6379[1]> hgetall user:21) "name" 2) "Test2" 3) "score" 4) "99"
(4) Get Field name or field value only
127.0.0.1:6379[1]> hkeys user:11) "name" 2) "Score" 127.0.0.1:6379[1]> hvals user:11) "Test" 2) "90"
(5) Get the number of fields
127.0.0.1:6379[1]> hlen user:1 (integer) 2
It is important to note that the hash type cannot get all the existing key values, that is, the ID, if you delete the middle ID, you can only use the exist command to determine whether the key exists.
(iii) list typeThe class table type solves the above problem. Class table types are ordered and values can be duplicated. The list type is implemented through a doubly linked list, where the time complexity of adding elements to the lists is O (1), and the speed of the elements at both ends is the fastest. Basic operations

The basic Operations command for a list starts with L.

(1) Add and eject elements use Lpush and Rpush and Lpop and Rpop to add and remove elements from the left and right side of the list, respectively.
127.0.0.1:6379> lpush User Test (integer) 1127.0.0.1:6379> rpush user test1 (integer) 2127.0.0.1:6379> lpop user " Test "127.0.0.1:6379> rpop user" test1 "127.0.0.1:6379>
(2) Llen View the number of elements
127.0.0.1:6379> llen User (integer) 0127.0.0.1:6379> lpush user Test (integer) 1127.0.0.1:6379> llen User ( Integer) 1
(3) Get list fragments
127.0.0.1:6379> lrange user 0) "test" 2) "Test1" 3) "Test2" 4) "Test3" 127.0.0.1:6379> lrange user 0) "test" 2) "Te St1 "3)" Test2 "
(4) Remove an element from the list
Lrem     Key     Count     Value@count is the number of deletions, more than 0 words starting from the left, less than 0 words from the right start @value to delete the value

We can store the ID of the data in the list, when a row of data is deleted, only need to delete the value as key, while querying the data, you need to read all the IDs from the list, and then read the data from the hash table.

(5) Gets and sets the index element value (6) Inserts an element into the list
127.0.0.1:6379> lrange user 0-11) "test" 2) "Test1" 3) "Test2" 4) "Test3" 127.0.0.1:6379> linsert user before test1 tes T0 (integer) 5127.0.0.1:6379> linsert user after test1 test1.5 (integer) 6127.0.0.1:6379> lrange user 0-11) "test" 2) "Test0" 3) "Test1" 4) "test1.5" 5) "test2" 6) "Test3"
(7) Moving elements from one list to another
Rdroplpush     source     destination pops a data from the right side of the source list and adds to the left of the destination list
(iv) collection type (set) Duplicate data is not allowed in the collection, the data is unique and unordered. An empty hash table is implemented with hash, so the operating time complexity is O (1). (1) Add sadd and delete Srem, get all elements smembers, return value identifies the number of elements that were successfully added:
127.0.0.1:6379> sadd key 1 (integer) 1127.0.0.1:6379> sadd key 2 (integer) 1127.0.0.1:6379> sadd key 2 (integer) 012 7.0.0.1:6379> smembers key1) "1" 2) "2" 127.0.0.1:6379> Srem key 2 (integer) 1127.0.0.1:6379> smembers key1) "1"
(2) Determines whether the element can use the Sismember command in the collection.
127.0.0.1:6379> sismember key 1 (integer) 1127.0.0.1:6379> sismember key 3 (integer) 0
(3) Set operation
Sdiff      key     [key ...] Sinter     key     [key ...] Sunion     key     [key ...]
(4) Get the number of elements
127.0.0.1:6379> scard Key (integer) 1
(5) Set operation and save result
Sdiffstore       Destination     key [key ...] Sinterstore      destination     key[key ...] Sunionstore      destination     key[key ...]
(6) Randomly get the elements of a collection
Srandmember key [Count] 127.0.0.1:6379> srandmember key 31) "4" 2) "2" 3) "3"
It is important to note that the random elements taken by Srandmember are not completely random when the collection is small, because the Redis storage method uses the hash bucket + data chain to store the same data when the bucket and the number of elements in each bucket are very few hours. (v) ordered set type sorted setAn ordered set is associated with a fraction of each element in the set as compared to a normal collection type, which allows us to not only perform the basic operations of inserting, deleting, and judging the collection of elements, but also the highest (or lowest) fraction of the top n elements, the elements in the specified fractional range, and so on. Although the elements in the collection must be different, their scores can be the same. The difference between the ordered combination and the use of lists: (1) The list type is implemented through a chain, getting close to both ends of the data is very fast, and when the element increases, access to the intermediate elements of the data will be slow, so she is more suitable for such as "new" or "log" such as less access to intermediate elements of the application (2) An ordered collection type is implemented using a hash and a jump table, so even reading the data in the middle part is fast (the time complexity is O (log (n))). (3) The position of an element cannot be easily adjusted in the list, but an ordered set can be adjusted by adjusting the score. (4) An ordered set consumes more memory. Basic operations

(1) zadd add element and set the score score

127.0.0.1:6379> zadd Students xiaoming (integer) 1127.0.0.1:6379> zadd Students (integer) 1
(2) Zscore get element score
127.0.0.1:6379> zscore Students xiaoming "60"
(3) Get a list of rankings in a range, with elements starting from 0
127.0.0.1:6379> zrange students 0) "Xiaoming" 2) "Daxiong"
(4) Gets the element of the specified fractional range, used to filter the data
127.0.0.1:6379> zrangebyscore Students 0 701) "xiaoming" 127.0.0.1:6379> zrangebyscore students 0 1001) "Xiaoming" 2 ) "Daxiong"
The limit command can implement the limit effect in an SQL statement
Get first 2 data starting from 0 127.0.0.1:6379> zrangebyscore students 0 limit 0) "Xiaoming" 2) "Daxiong" get first 1 data from 2 127.0.0.1 :6379> zrangebyscore Students 0 limit 1) "Daxiong"
(5) Increase and decrease the score of an element
127.0.0.1:6379> Zincrby Students 5 xiaoming "up" 127.0.0.1:6379> Zincrby students-5 xiaoming "60"
(6) Gets the number of elements in the collection
127.0.0.1:6379> zcard Students (integer) 2
(7) Gets the number of elements in the specified fraction range
Zcount     key     min     max127.0.0.1:6379> zcount students 0 (integer) 1127.0.0.1:6379> zcount students 0 90 (integer) 2
(8) Delete one or more elements
Zrem     key     [key ...]
(9) Delete elements according to the rank range
Zremrangebyrank     key     start     stop
(10) Delete elements according to the score range
Zremrangebyscore     key     min     max
(11) Get the rank of the element, note that the element is sorted starting from 0
127.0.0.1:6379> zrank Students xiaoming (integer) 0
Reference

Redis Getting Started Guide

http://redis.readthedocs.org/en/latest/

Redis (i) Installation and basic data type operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.