Redis Basic Use Learning Notes

Source: Internet
Author: User
Tags bind echo name hash redis port number install redis redis server


Redis Introduction

Redis is an open source, with the ANSI C language, support network, can be based on memory can also be persistent log-type, Key-value database, and provide a variety of language APIs, including Java,c/c++,c#,php,javascript,perl, Object-c,python,ruby,erlang, etc.

Redis is a high-performance key-value database that supports stored value types including string (string), list (linked list), set (set), Zset (sorted set– ordered set), and hash (hash type). These data types support Push/pop, Add/remove and intersection-set and differential sets and richer operations, and these operations are atomic. On this basis, Redis supports a variety of different ways of ordering.

To ensure efficiency, the data is cached in memory and periodically writes the updated data to the disk or writes the modification operation to the appended record file, and on this basis realizes the Master-slave (master-slave) synchronization. Redis can complement the relational database in some application situations. In some cases, it can be considered that Redis is a memory database, through the memory caching function to achieve a significant increase in performance.

1. The environment constructs
Redis can be installed in two different ways

Installing through source code
Download the latest version of the redis-x.y.z.tar.gz from the Redis.io, and then go to the Redis-x.y.z folder directly make can
Installing by installing the package management tool
Take Ubuntu for example: #sudo apt-get Install Redis-server
The Redis server installed in this way is not the latest version, but satisfying basic usage is no problem

2. Common Configuration
Redis provides a rich configuration, and the default profile is the redis.conf under the installation root directory. Typically, most configuration items require only the default values to satisfy most scenarios, and the following are some of the most commonly used configuration items.

Whether to run in daemon form
Daemonize No

Default is No

Respond to requested IP
Bind 192.168.1.100 10.0.0.1

Lack of capital response to all available network card connection requests, if you want to respond to all machine connections, you need to modify to bind 0.0.0.0

Port number
Port 6379

Log level
Divided into four levels, that is, debug, verbose, notice, warning, where the highest level of warning
LogLevel Notice

LogFile Setting the location where the log files are built
LogFile

If blank, the log is output to standard output

Total number of databases
Databases 16

The number of these 16 databases will be 0 to 15. The default database is the database with number 0. Users can use Select <DBid> to select the appropriate database

Data is saved to disk, which controls the Rdb snapshot feature
Save 900 1//indicates that a persistence is triggered once every 15 minutes and at least 1 key changes are initiated

Save 300 10//indicates that a persistence is triggered once every 5 minutes and at least 10 key changes are initiated

Save 60 10000//indicates that there are at least 10,000 key changes per 60 seconds, triggering a persistent

Disabling the RDB persistence policy can achieve the same effect as long as no save directives are set, or if you pass in an empty string parameter to save

Set Redis master-slave Sync
Slaveof <masterip> <masterport>

AOF Persistence
AppendOnly No

Redis executed all write instructions recorded, the next time the Redis reboot, as long as these write instructions to repeat the past, you can implement data recovery

3. Common commands
Client Connection
#src/REDIS-CLI

Stop Redis Service:
src/redis-cli> shutdown

Remove all currently matched key
> Keys *

Set the value of a key
> Set KeyName keyvalue

Gets the value of the key
> Get KeyName

Whether the current key exists, 0 indicates that it does not exist
> Exists Larry

(integer) 0

Deletes the specified key
> del lv

Set Expiration Time
> Expire Larry 10

(integer) 1

Move Larry Key value pair to AD4 database
> Move Larry Ad4

(integer) 1

Removes the expiration time for the current key
> Persist LV

(integer) 1

Return a key at random
> Randomkey

Rename Key
> Rename

Test whether the connection is still
> Ping

PONG

Print
> Echo Name

"Larry"

Database switching
> select Ad4databank

Ok

Exit connection
> Quit

Number of key keys in the current database
> dbsize

(integer) 12

Server basic Information
> Info

Get the server's parameter configuration
> config get

Empty the current database
> Flushdb

Clear all databases
> Flushall

4. Application interface
Because the Redis data operation is very simple, the Application API interface usage is also very intuitive, with the corresponding Third-party library support, connected to the Redis server, the rest is a very simple operation, the above fourth section of the command support, the application API interface is also basically supported.

Java
After the introduction of Jedis, this third party library, we can use the example code for simple data insertion and query action.


public static void Main (string[] args) {
Jedis Jedis = new Jedis ("147.151.240.234", 6379);
Jedis.set ("foo", "Bar");
String value = Jedis.get ("foo");
System.out.println (value);

}

Of course, there are good people to further encapsulate the Jedis, making the API more convenient and flexible, a well-known example is the jfinal in the Redisplugin

Python
The first need to install the Python support library, by executing the command sudo apt-get install Python-redis or in the Windows environment to perform PIP install Redis, and then the data additions and deletions are checked operation, very intuitive


Import Redis
 
r = Redis. Redis (host= ' 10.0.1.7 ', port=6379, db=1)
 
 
 
# Check database size
 
print ' \ndbsize:%s ' % r.dbsize ()
 
# plug Data
 
r[' c1 ' = ' bar '
 
#或者
 
R.set (' C2 ', ' Bar ')
&NBSP
# Fetch data
 
print ' r[']: ', r[' C1 '
 
#或者
 
print ' Get: ', R.get (' a ')
&nbs P
#或者 fetch a batch at a time
 
print ' mget: ', R.mget (' C1 ', ' C2 ')
 
#或者 take a batch of their names (key) just like you don't want to lose all
 
print ' keys: ', R.keys (' c* ')
 
#又或者 you just want to randomly take one:
 
print ' Randomkey: ', R.randomkey ()
&NBSP
 
 
# View a data there is no 1 no 0
 
print ' existes: ', r.exists (' a ')
 
&NBSP;
 
# Erase data 1 is delete success 0 and none is not this thing
 
print ' Delete: ', R.delete (' cc ')
 
# Oh, yes, it is.
&N that supports bulk operations Bsp
print ' Delete: ', R.delete (' C1 ', ' C2 ')
 

5. Case study
In our project, Redis as a relational database of a memory cache, when there is a user query, first look at the Redis whether there are users need information, and some words directly back; there is no need for information, and then to the relational database query, The results of the query are cached in the Redis, speeding up the performance of subsequent queries. Overall, the performance of the entire application has increased by a hundredfold after using Redis.

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.