data manipulation redis

redis

  • definition:

    • redisIs open source, memory database, the database can be used for caching and messaging middleware, support for multiple data types, strings, hashes, lists, sets, ordered collections, etc.

Local Start - common commands

  • redis-cli

  • redis-server.exe # Premise that you installed with style

  • /etc/init.d/redis-server stop ---> Stop

  • /etc/init.d/redis-server start ---> Start

  • /etc/init.d/redis-server restart ---> Restart

Remote connection redisdatabase

  • redis-cli -h <hostname> -p <port>

Database switch

  • select 数据库名 Note: a few numbers database or new database name

Query by key value

  • All inquiries by the key values

    • Search key

      • keys *

    • The key contains a query

      • keys 'a*'

View type value corresponding to the key value

  • type key

The type of value

  • String string

  • Hash hash

  • List list

  • set set

  • Ordered set zset

string

Save the settings key

  • There is no set value is added, there is modified

    • set key value

Set the key and the expiration time, in seconds

  • setex key 秒数 value

A plurality of keys

  • mset key1 value1 key2 value2 ...

Added value

  • append 'key' 'value'

    • Understood as additional common append the tail

Get the value

Obtaining a single key value

  • get key

Obtaining a plurality of values ​​by a plurality of key

  • mget key1 key2...

Determine whether there is key

  • If there is a return, there is no return 0

    • exists key1

delete

Delete keys and corresponding values

  • del key1 key2...

    • It can be a single, a plurality of can

Setting key expiration time, in seconds

  • expire 'key' 秒数

View the effective time, in seconds

  • ttl key

hash

  • For storing objects, the structure of the object attribute, value +

  • Value is of type string

Save Settings Properties

Providing a single property

  • hset key field value

    • key field for the value of property

    • Personal understanding: a dictionary value is another dictionary

Set multiple properties

  • hmset key field1 value1 field2 value2 ...

Obtain

Get all the attributes specified key

  • hkeys key

Gets the value of the specified property

  • hget key field

Acquiring a plurality of property values ​​specified

  • hmget key field1 field2 ...

Get all of the key attributes

  • hvals key

delete

  • Delete the entire hash keys and values, using the del command

  • Delete attributes, attribute values ​​corresponding to be removed together

    • hdel key field1 field2 ...

list

  • A list of elements of data type string

  • Sort insert data sequence

increase

Inserting data from the left

  • lpush key value1 value2 ...

Inserting data from the right

  • rpush key value1 value2 ...

Data into left and right side understood:
  • After the first added value, added data element value in the left or right side of the first outwardly

Inserting a new element before or after the specified element

  • linsert key before或after 现有元素 新元素

    • linsert key before 目标元素 新添元素

      • Add a new element before the target element

    • linsert key after 目标元素 新添元素

      • Add a new element after the target element

     

Obtain

Traversing print all the values ​​of the key

  • lrange key

    • lrange key 起始下标 结束下标

      • Index interval setting comprises closing the left and right index close

Setting element values ​​specified index

  • lset key index value

    • Example:lset a 1 z

      • Modify key is a subscript for the element value is 1, z

delete

Delete the specified element

  • lren key count value

    • count> 0 is removed from the head to the tail

    • count <0 is removed from the head to the tail

    • count = 0 Remove all

set & zset

set

  • Unordered collection

  • Elements of type string

  • Element unique, not repeated deduplication

  • No modification operation for collection

increase

Adding elements

  • sadd key member1 member2 ...

  • sadd key 元素1 元素2 ...

 

Gets a collection of all the elements

  • smembers key

 

delete

Delete the specified element

  • srem key member1 member2 ...

    • Delete the specified key single or multiple elements

zset

  • sorted set ordered set

  • Elements of type string

  • Element unique, not repeated deduplication

  • Will be associated with each element of a double type of score, represents the weight

  • Note: No modification operations

increase

Adding elements

  • zadd key score1 member1 score2 member2 ...

    • score weighting value (as can be appreciated sort in forward order, from small to large values)

    • member1 value string type element

Obtain

Get the specified range of elements (the subscript, left closing and right closing)

  • zrange key 起始下标 结尾下标

Score value acquisition element between the min and max

  • zrangebyscore key min max

    • min and max values ​​depend on the value of score

Gets score value of the element member

  • zscore key member

    • score value can be referred to weight

delete

Delete the specified element

  • zrem key memeber1 member2 ...

Delete the weight value within the specified range of elements

  • zremrangebyscore key min max

Affairs

  • Command and description:

  • 1 DISCARD   cancel the transaction, giving up all the commands in a transaction block.

  • 2 EXEC   Run all transactions within the block.

  • . 3 the MULTI   mark the start of a block transaction.

  • 4 unwatch   cancel WATCH command to monitor all the key.

  • 5 [WATCH Key Key ... ]   monitor one (or more) key, if this (or these) key changes to other orders are executed before the transaction, then the transaction will be interrupted.

Redis Affairs

  • Multiple commands can be executed once, and with two important guarantees:

  • It is placed in a batch operation before sending queue buffer EXEC command.

  • After receiving EXEC command to enter the transaction execution, transaction arbitrary command execution fails, the rest of the command is still being executed.

  • During the execution of a transaction, other command request submitted by the client will not be inserted into the transaction execution command sequence.

From start to execute a transaction will go through the following three stages:

  • Begin a transaction.

  • Command into the team.

  • The enforcement branch.

Example:

redis 127.0.0.1:6379> MULTI
OK

redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
QUEUED

redis 127.0.0.1:6379> GET book-name
QUEUED

redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
QUEUED

127.0.0.1:6379 repeat> tag SMEMBERS
QUEUED

redis 127.0.0.1:6379> EXEC
1) OK
2) "Mastering C++ in 21 days"
3) (integer) 3
4) 1) "Mastering Series"
2) "C++"
3) "Programming"

Guess you like

Origin www.cnblogs.com/canhun/p/11123010.html