Basic tutorial on getting started with Redis

Source: Internet
Author: User
Tags redis desktop manager redis cluster install redis redis tutorial

Basic tutorial on getting started with Redis

Redis is an open-source, advanced key-value storage perfect solution for building high-performance, scalable Web applications.

Redis inherits three main features from its many competitions:

  • The Redis database is completely in the memory, and the disk is used for persistence only.

  • Compared with many key-value data storage systems, Redis has a rich set of data types.

  • Redis can copy data to any number of slave servers.

Redis advantages
  • Exception fast: Redis is very fast. It can execute about 0.11 million sets per second and more than 81000 records per second.

  • Supports a wide range of data types: Redis supports most developers who already know the image list, set, ordered set, and hash data types. This makes it very easy to solve a variety of problems, because we know which problems can be handled through its data type better.

  • Operations are atomic: All Redis operations are atomic, which ensures that if the two clients access the Redis server at the same time, the updated value will be obtained.

  • Multi-function utility: Redis is a multi-utility tool that can be used in multiple applications, such as cache, message, and queue (Redis native supports publishing/subscription), any temporary data, applications, such as Web application sessions and Web page hit count.

Install and test Redis in Ubuntu 14.04

Redis cluster details

Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis

Redis series-installation, deployment, and maintenance

Getting started with Redis

Install Redis in CentOS 6.3

Learning notes on Redis installation and deployment

Redis. conf

Redis-Environment

Install Redis on Ubuntu, open the terminal, and type the following command:

 
$sudo apt-get update$sudo apt-get install redis-server

This will install Redis on your computer.

Start Redis

$redis-server

Check whether Redis is working?

$redis-cli

This will open a Redis prompt, as shown in:

redis 127.0.0.1:6379>

The above prompt 127.0.0.1 is the local IP address, and 6379 is the port running on the Redis server. Enter the PING command, as shown in.

redis 127.0.0.1:6379> pingPONG

This indicates that you have successfully installed Redis on your machine.

Install Redis Desktop Manager on Ubuntu

Install Redis Desktop Manager on Ubuntu, just open the package from the http://redisdesktop.com/download and install it.

The Redis Desktop Manager provides you with a user interface to manage Redis keys and data.

Redis-Data Type

Redis supports five data types, which are described as follows:

String

Redis string is a byte sequence. Redis strings are binary secure, which means they have a known length without any special characters terminated, so you can store anything, up to 512 megabytes.

Example
redis 127.0.0.1:6379> SET name "yiibai"OKredis 127.0.0.1:6379> GET name"yiibai"

The above is an example of the Redis set and get commands. The Redis name is the key used by yiibai stored in the Redis string value.

Hash

Redis hash is a set of key-value pairs. Redis hash values are mappings between string fields and string values, so they are used to represent objects.

Example
redis 127.0.0.1:6379> HMSET user:1 username yiibai password yiibai points 200OKredis 127.0.0.1:6379> HGETALL user:11) "username"2) "yiibai"3) "password"4) "yiibai"5) "points"6) "200"

In the preceding example, the hash data type is used to store the object of the user whose basic information is contained. Here, the HEGTALL user command user: 1 is the key.

List

The Redis list is a simple string list that sorts the insertion sequence. You can add elements to the header or tail of the Redis list.

Example
redis 127.0.0.1:6379> lpush tutoriallist redis(integer) 1redis 127.0.0.1:6379> lpush tutoriallist mongodb(integer) 2redis 127.0.0.1:6379> lpush tutoriallist rabitmq(integer) 3redis 127.0.0.1:6379> lrange tutoriallist 0 101) "rabitmq"2) "mongodb"3) "redis"

The maximum length of the list is 2.32-1 elements (4294967295, each list can contain more than 4 billion elements ).

Set

Redis collections are unordered collections of strings. In Redis, you can add, delete, and test whether the file exists. The time complexity of member O (1.

Example
redis 127.0.0.1:6379> sadd tutoriallist redis(integer) 1redis 127.0.0.1:6379> sadd tutoriallist mongodb(integer) 1redis 127.0.0.1:6379> sadd tutoriallist rabitmq(integer) 1redis 127.0.0.1:6379> sadd tutoriallist rabitmq(integer) 0redis 127.0.0.1:6379> smembers tutoriallist1) "rabitmq"2) "mongodb"3) "redis"

Note: In the preceding example, the rabitmq set is added twice, but the set element has a unique attribute.

The maximum number of elements in the set is 2.32-1 (4294967295, can accommodate more than 4 billion elements ).

Ordered Set

The sorted set of Redis is similar to the set of Redis, and the string is not repeated. The difference is that each member of an ordered set uses scores to take the ordered set command, which is related to the smallest to the largest member scores. Although the Member is unique, the score may be repeated.

Example
redis 127.0.0.1:6379> zadd tutoriallist 0 redis(integer) 1redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb(integer) 1redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq(integer) 1redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq(integer) 0redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 10001) "redis"2) "mongodb"3) "rabitmq"
Redis-keys

The Redis keys command is used to manage keys in Redis. The Redis keys command syntax is as follows:

Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET yiibai redisOKredis 127.0.0.1:6379> DEL yiibai(integer) 1

In the preceding example, DEL is a command, while yiibai is a key. If the key is deleted, the output value is (integer) 1. Otherwise, the output value is (integer) 0.

Redis-Strings

The Redis strings command is used to manage string values in Redis. The usage syntax of the Redis strings command is as follows:

Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET yiibai redisOKredis 127.0.0.1:6379> GET yiibai"redis"

In the preceding example, SET and GET are commands, while yiibai is keys.

Redis-Hash

The hash value of Redis is the ing between string fields and string values, so they represent the perfect data type of the object.

The hash value in Redis can store up to 400 billion field-value pairs.

Example
redis 127.0.0.1:6379> HMSET yiibai name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000OKredis 127.0.0.1:6379> HGETALL yiibai1) "name"2) "redis tutorial"3) "description"4) "redis basic commands for caching"5) "likes"6) "20"7) "visitors"8) "23000"

In the preceding example, the Redis set named yiibai by hash is named tutorials (name, description, likes, visitors)

Redis-list

The Redis list is a simple string list that sorts the insertion sequence. You can add the Redis element to the header or tail of the list.

The maximum length of the list is 2.32-1 element (the number of each list element exceeds 4294967295 ).

Example
redis 127.0.0.1:6379> LPUSH tutorials redis(integer) 1redis 127.0.0.1:6379> LPUSH tutorials mongodb(integer) 2redis 127.0.0.1:6379> LPUSH tutorials mysql(integer) 3redis 127.0.0.1:6379> LRANGE tutorials 0 101) "mysql"2) "mongodb"3) "redis"

In the above example, the three values are inserted in the redis list and the command tutorial is called LPUSH.

Redis-Set

The Redis set is the unordered set of unique strings. The uniqueness of a set does not allow duplicate keys of data.

Add, delete, and test members in the Redis set in O (1) (constant time regardless of the number of contained element sets ). The maximum length of a set is 2.32-1 element (more than 4294967295 elements per set ).

Example
redis 127.0.0.1:6379> SADD tutorials redis(integer) 1redis 127.0.0.1:6379> SADD tutorials mongodb(integer) 1redis 127.0.0.1:6379> SADD tutorials mysql(integer) 1redis 127.0.0.1:6379> SADD tutorials mysql(integer) 0redis 127.0.0.1:6379> SMEMBERS tutorials1) "mysql"2) "mongodb"3) "redis"

In the preceding example, the three values are inserted into the redis Set Name tutorials by the command SADD.

Redis sequence set

The ordered set of Redis is similar to the set of Redis. It is stored in the set value and is unique. The difference is that each member of an ordered set uses scores to take the ordered set command, from minimum to maximum scores.

Add, delete, and test the existing member O (1) in the sorted set of Redis (fixed time, regardless of the number of elements contained in the set ). The maximum length of the list is 2.32-1 element (more than 4294967295 elements per set ).

Example
redis 127.0.0.1:6379> ZADD tutorials 1 redis(integer) 1redis 127.0.0.1:6379> ZADD tutorials 2 mongodb(integer) 1redis 127.0.0.1:6379> ZADD tutorials 3 mysql(integer) 1redis 127.0.0.1:6379> ZADD tutorials 3 mysql(integer) 0redis 127.0.0.1:6379> ZADD tutorials 4 mysql(integer) 0redis 127.0.0.1:6379> ZRANGE tutorials 0 10 WITHSCORES1) "redis"2) "1"3) "mongodb"4) "2"5) "mysql"6) "4"

In the preceding example, the three values are inserted by the command ZADD and their scores are named tutorials in the sorted set of redis.

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.