Redis: Installation, Jedis and Common Commands on Linux

1. Introduction to Redis

1. About relational databases and nosql databases

A relational database is a database based on relational tables, which will eventually persist data to disk, while a nosql database is a database based on a special structure that stores data in memory. In terms of performance, nosql database is better than relational database, and in terms of security, relational database is better than nosql database, so in actual development, nosql and relational database will be used together in a project to achieve the best performance and Double guarantee of security.

 

2. Why use Redis

 1) Easy expansion 
 2) Large data volume improves performance 
 3) Diverse and flexible data models 

 

3. Installation of redis on Linux

1) Install the c environment compiled by redis, yum install gcc-c++

2) Upload redis-2.6.16.tar.gz to the Linux system

3) Unzip to /usr/local tar -xvf redis-2.6.16.tar.gz -C /usr/local

4) Enter the redis-2.6.16 directory and use the make command to compile redis

5) Use the make PREFIX=/usr/local/redis install command in the redis-2.6.16 directory to install redis into /usr/local/redis

6) Copy redis.conf in redis-2.6.16 to the bin of redis in the installation directory

7) Start redis and execute the command redis-server redis.conf under bin

8) If you need to connect to redis remotely, you need to configure the redis port 6379 to develop in the linux firewall

/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

/etc/rc.d/init.d/iptables save

 

After startup, I see the above welcome page, but this window cannot be closed. When the window is closed, it is considered that redis is also closed. Solution: You can configure redis to start in the background by modifying the configuration file, that is, the server is started but the console window will not be created.

Change the daemonize in the redis.conf file from no to yes to indicate background startup

 

Use the command to see if port 6379 starts ps -ef | grep redis

Use ./redis-cli to enter the client under bin

 

8) How to close after starting through the background? 

 

 

 

2. Use java to operate Redis

 

3. Common commands of Redis

redis is an advanced key-value storage system

The key is a string type, as far as possible to meet the following points:

1) The key should not be too long, it is best not to operate 1024 bytes, which will not only consume memory but also reduce the search efficiency

2) The key should not be too short, if it is too short, it will reduce the readability of the key

3) In the project, the key preferably has a unified naming convention (according to the needs of the enterprise)

where value supports five data types:

1) String type string

2) String list lists

3) String collection sets

4) sorted sets

5) Hash type hashs

 

Our learning of Redis is mainly about the storage of data. Let's learn the storage operations of various Redis data types in the future:

 

 

1. store string

The string type is the most basic data storage type in Redis. It is binary-safe in Redis, which means that this type can accept data in any format, such as JPEG image data or Json object description information. In Redis, the maximum length of data that the String type Value can hold is 512M

 

 

1) set key value : Set the key to hold the specified string value, and perform an overwrite operation if the key exists. always returns "OK"

2) get key : Get the value of the key. If the value associated with the key is not of type String, redis will return an error message, because the get command can only be used to obtain the String value; if the key does not exist, it will return null.

 

 

3) getset key value : first get the value of the key, and then set the value of the key.

 

4) incr key : atomically increment the value of the specified key by 1. If the key does not exist, its initial value is 0, and its value is 1 after incr. If the value of value cannot be converted to an integer, such as hello, the operation will fail and return the corresponding error message.

5) decr key : atomically decrements the value of the specified key by 1. If the key does not exist, its initial value is 0, and its value is -1 after incr. If the value of value cannot be converted to an integer, such as hello, the operation will fail and return the corresponding error message.

 

6) incrby key increment : atomically increases the value of the specified key by increment. If the key does not exist, the initial value of the device is 0. After incrby, the value is increment. If the value cannot be converted to an integer, such as hello, it will fail and return an error message

7) decrby key decrement : atomically reduce the value of the specified key to decrement. If the key does not exist, the initial value of the device is 0. After decrby, the value is decrement. If the value cannot be converted to an integer, such as hello, it will fail and return an error message


8) append key value : if the key exists, append the value after the original value; if the key does not exist, recreate a key/value

 

 

2. store list type

In Redis, the List type is a linked list of strings sorted in insertion order. Like a normal linked list in a data structure, we can add new elements to its head (left) and tail (right). On insert, if the key does not exist, Redis will create a new linked list for the key. Conversely, if all elements in the linked list are removed, the key will also be removed from the database. The maximum number of elements that can be contained in a List is 4294967295.

         From the perspective of the efficiency of element insertion and deletion, if we insert or delete elements at both ends of the linked list, this will be a very efficient operation, even if there are millions of records stored in the linked list, the operation can be performed in constant time completed within. However, it should be noted that if the element insertion or deletion operation is in the middle of the linked list, it will be very inefficient. I believe that for developers with a good data structure foundation, this is not difficult to understand.

 

 

1) lpush key value1 value2... : Insert all values ​​at the head of the list associated with the specified key, if the key does not exist, the command creates an empty linked list associated with the key before inserting, and then Insert data to the head of the linked list. If the insertion is successful, the number of elements is returned.

2) rpush key value1, value2... : add elements at the end of the list

3) lrange key start end : Get the value of the element from start to end in the linked list, start and end can be negative numbers, if it is -1, it means the element at the end of the linked list, -2 means the second to last, and so on...

 

4) lpushx key value : only when the key specified in the parameter exists (if there is no value in the list managed by the key, the key does not exist), insert the value in the head of the list associated with the specified key.

5) rpushx key value : add elements at the end of the list

 


6) lpop key : Returns and pops the first element in the linked list associated with the specified key, that is, the head element.

7) rpop key : pops elements from the tail.

 


8) rpoplpush resource destination : pops the tail element in the linked list and adds it to the head

 

9) llen key : Returns the number of elements in the linked list associated with the specified key.

 

 

10) lset key index value : Set the element value of the subscript of the index in the linked list, 0 represents the head element of the linked list, -1 represents the tail element of the linked list.

 

 

11) lrem key count value : delete count elements whose value is value. If count is greater than 0, traverse and delete count elements whose value is value. If count is less than 0, traverse and delete from end to head. If count is equal to 0, delete all elements in the linked list that are equal to value.

 

 

12) linsert key before|after pivot value : Insert the value element before or after the pivot element.

 

 

3. store sets type

In Redis, we can regard the Set type as an unsorted set of characters. Like the List type, we can also perform operations such as adding, deleting, or judging whether an element exists on the data value of this type. It should be noted that the time for these operations is constant time. The maximum number of elements a Set can contain is 4294967295.

Unlike the List type, duplicate elements are not allowed in the Set collection. Compared with the List type, the Set type also has a very important feature in function, that is, the aggregation calculation operations between multiple Sets are completed on the server side, such as unions, intersections and differences. Since these operations are all done on the server side, they are extremely efficient and save a lot of network IO overhead.

 


1) sadd key value1, value2...: add data to the set, if the value of the key already exists, it will not be added repeatedly

2) smembers key: Get all the members in the set

3) scar key: Get the number of members in the set

 

 

4) sismember key member: Determine whether the member specified in the parameter is in the set, 1 means it exists, 0 means it does not exist or the key itself does not exist

5) srem key member1, member2...: delete the members specified in the set

 

 

6) srandmember key: randomly returns a member in the set

 

 

7) sdiff sdiff key1 key2: Returns the members of the difference between key1 and key2, and it is related to the order of keys. That is, the difference is returned.

 

 

8) sdiffstore destination key1 key2: store the members with the difference between key1 and key2 on the destination

 

 

9) sinter key[key1, key2...]: Returns the intersection.

10) sinterstore destination key1 key2: store the returned intersection on the destination

11) sunion key1, key2: return the union.

 

 

12) sunionstore destination key1 key2: store the returned union on destination

 

 

 

4. store sortedset

Sorted-Sets and Sets types are very similar, they are both collections of strings, and they do not allow duplicate members to appear in a Set. The main difference between them is that each member in Sorted-Sets will have a score associated with it, and Redis sorts the members of the set from small to large through the score. It should be noted, however, that although the members of Sorted-Sets must be unique, scores can be repeated.

Adding, removing, or updating a member in a Sorted-Set is a very fast operation with a time complexity of the logarithm of the number of members in the set. Since the members in Sorted-Sets are ordered by their positions in the set, even accessing members located in the middle of the set is still very efficient. In fact, this feature of Redis is difficult to achieve in many other types of databases. In other words, it is very difficult to model in other databases to achieve the same efficiency as Redis at this point. of.

For example: game rankings, Weibo hot topics and other usage scenarios.

 

 

1) zadd key score member score2 member2 …  : store all members and the score of this member in sorted-set

2) zcard key : Get the number of members in the set

 

 

3) zcount key min max : Get members whose scores are between [min, max]

zincrby key increment member: Set the added score of the specified member.

zrange key start end [withscores]: Get the members whose footer is start-end in the collection. The [withscores] parameter indicates that the returned members contain their scores.

zrangebyscore key min max [withscores] [limit offset count]: Returns the members whose scores are in [min, max] and sorts them from low to high. [withscores]: display scores; [limit offset count]: offset, indicating that it starts from the element whose footer is offset and returns count members.

zrank key member: Returns the member's position in the set.

zrem key member[member…]: Remove the specified member from the set, and multiple members can be specified.

zscore key member: returns the score of the specified member

 

 

5. store hash

The Hashes type in Redis can be seen as a map container with String Key and String Value. So this type is very suitable for storing information about value objects. Such as Username, Password and Age. If the Hash contains few fields, then that type of data will also take up very little disk space. Each Hash can store 4294967295 key-value pairs.

 

 

1) hset key field value : Set a field/value pair (key-value pair) for the specified key.

2) hgetall key: Get all the filed-vaule in the key

 

 

3) hget key field : Returns the value of the field in the specified key

4) hmset key fields : set multiple filed/value in key

5) hmget key fileds : Get the values ​​of multiple fileds in the key

6) hexists key field : determine whether the filed in the specified key exists

7) hlen key : Get the number of fields contained in the key

8) hincrby key field increment : Set the value of filed in the key to increase increment, such as: age increases by 20

 

Summarize:

1、nosql

2, redis installation ---- linux (key)

3, jedis (key)

4. 5 types of data operations in redis (understand) --- string and hash

5. Others of redis

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025928&siteId=291194637