Redis-explore Redis and learn basic usage of Redis.

Source: Internet
Author: User
Tags install redis

Redis-explore Redis and learn basic usage of Redis.

Author: Gigi Sayfan time: 2018.8.28

Advertisement

Redis is a key-value database in the memory. It is free and open-source. It is implemented in C language and runs very fast. Redis is already a mature product (now version 3.03. It has gone through a very harsh war of baptism, and has been used for many years for large and small users. Redis provides excellent documentation and user libraries for various programming languages.
In this article, I will lead you to explore Redis and learn its basic usage.

What are the special characteristics of Redis?

Redis is very fast. To make a software run fast, the most typical method is to focus on every small function film and then optimize it with the utmost effort. Redis is different. It provides a wide range of advanced features that can rival or even surpass most key-value databases, without affecting the speed. Let's take a quick look at the features of Redis:

  • Rich key value types: String, Dictionary (hash), set, bitmap and hyperlogs
  • Advanced Operations on these values
  • Optional persistence
  • Master-slave settings
  • Asynchronous replication
  • Monitor sentpost, pass, and automatic troubleshooting
  • Transactions
  • Publish and subscribe to messages
  • Automatically reclaim the least frequently used key values
  • Automatically reclaim expired key values
  • Script (use Lua)
Basic Use Cases of Redis

Redis can be used for multiple purposes. It acts as an ultra-fast distributed buffer. However, it is often used as a queue (using commands to push and pop a list entry), or using its publish-subscribe system to implement the event router function. Redis uses its incremental command to count. For use cases that require tracking of the last item, Redis provides a good alternative. Instead of having to have too many indexes in the relational database, you just need to maintain a trim list for the project.

Redis practices

Let's further explore Redis. Download and install Redis from here.
Linking...
If you are using Mac OSX, I suggest using Homebrew for installation.
Then let's start redis. In the redis-server terminal window, you will see this:
Img...
There are also some cute ASCII art words. Now, in another terminal window, redis-cli. Here there is less color, you will see this:

~>redis-cli127.0.0.1:6379

In any case, we are about to begin. Store some keywords and values in the database first.

Simple Value

This example shows how to set and obtain simple string values.

127.0.0.1:6379> set k vOK127.0.0.1:6379> get k"v"127.0.0.1:6379> get no_such_key(nil)

Wow! You have mastered Redis's 80%. This is a very simple high-speed cache.
How can I delete keywords? No problem.

127.0.0.1:6379> del k(integer) 1127.0.0.1:6379> del no_such_key(integer) 0

Redis will tell you how many keywords have been deleted.

Count

Redis supports the + 1 and + n counting methods to change the value corresponding to the key.

127.0.0.1:6379> set counter 0OK127.0.0.1:6379> incr counter 1(integer) 1127.0.0.1:6379> incrby counter 3(integer) 4

The "incrby" command indicates + n. Therefore, you can add as much as you want. Redis can also return the added counter value.
By setting n to a negative number, you can count down.

127.0.0.1:6379> incrby counter -2(integer) 2

Counter operations are atomic operations, which are very important for multi-user environments.

Browse keywords

If you want to know which keywords are available, you can use the "keys" command to match the model:

127.0.0.1:6379> set k1 1OK127.0.0.1:6379> set k2 2OK127.0.0.1:6379> keys k*1) "k1"2) "k2"
Lists

Redis does not just store simple strings and numbers. Let's take a look at the list. Lists is an ordered item set. You can push or pop item from the end of lists. You can query the length of lists, and you can obtain the parts of lists. Here is a simple example:

127.0.0.1:6379> lpush superheroes batman(integer) 1127.0.0.1:6379> lpush superheroes spiderman(integer) 2127.0.0.1:6379> llen superheroes(integer) 2127.0.0.1:6379> lrange superheroes 0 -11) "spiderman"2) "batman

The "lrange" command receives the start index and the last index. -1 indicates that the last entry is displayed. The first index is 0. Do not be confused when the output result starts from 1.
More examples:

127.0.0.1:6379> rpush superheroes "the incredible hulk" catwoman(integer) 4127.0.0.1:6379> rpop superheroes"catwoman"127.0.0.1:6379> lpop superheroes"spiderman"127.0.0.1:6379> lrange superheroes 0 -11) "batman"2) "the incredible hulk"

It provides a complete interface for Data Structure operations. It can provide random access queue lists (using lrange), and later first-in-first-out stack (using lpush/lpop ), first-in-first-out queue (use lpush, rpop) or double-end queue dequeue (push/pop to both ends ).

Sets
For unordered sets of items, sets are often selected. Redis has a complete command for managing sets:

127.0.0.1:6379> sadd primes 2 3 5 7 11 13 17 19(integer) 8127.0.0.1:6379> sadd integers_to_10 1 2 3 4 5 6 7 8 9 10(integer) 10

For example, there are two sets. One is a prime number of no more than 20, and the other is an integer of 1 to 10.
Now, you can perform the first test-"sismember" command:

127.0.0.1:6379> sismember primes 8(integer) 0127.0.0.1:6379> sismember primes 7(integer) 1

Find the prime number not greater than 10, we can use the intersection command

127.0.0.1:6379> sinter primes integers_to_101) "2"2) "3"3) "5"4) "7"

In this example, we are lucky because the results are sorted. However, in general, sets are unordered, and the result we get may be any sequence. Redis also supports sorting sets. Each item corresponds to a score and then sorts sets Based on this score.

Summary
Redis is a multi-functional database with rich advanced features. It also has a relatively new Product accessory to expand its capabilities so that it can handle use cases that cannot be processed only on one machine. Take some time to learn and use it in your system.

Author profile:
Gigi Sayfan is an Aclima director responsible for software architecture. Aclima is a startup company responsible for designing and deploying distributed sensor networks and providing high-level environmental awareness. Gigi has been engaged in professional software development for more than 20 years and involves a wide range of fields, including instant messaging, deformation, chip manufacturing process control, game console and embedded multimedia applications, brain-inspired machine learning, client browser development, WEB services for 3D distributed game platforms, and recent Iot sensors. The programming languages written to the product every day include C, C ++, C #, Python, Java, Delphi, Javascript, and even operating systems (Windows3.11 to windows7, Linux, Max OSX, embedded Lynx and Sony game consoles) Written in Cobol and PowerBuilder. Its technical fields include databases, underlying networks, distributed systems, non-traditional user interfaces, and General Software Development lifecycles.

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.