Jredis Jedis introduces the use of redis

Source: Internet
Author: User
Tags download redis redis version install redis
Use redis Jedis
    Blog type: 

  • Open Source
I. Introduction

Redis is an advanced key-value database. It is similar to memcached, but data can be persistent and supports a wide range of data types. There are string, linked list, set, and ordered set (list, set, sorted set, and hash ). Allows you to compute, merge, and merge sets (difference) on the server side. It also supports multiple sorting functions. So redis can also be seen as a data structure server.

All redis data is stored in the memory and then asynchronously stored to the disk (this is called the "semi-persistent mode "); you can also write every data change into one (this is called "Full persistence mode"). memcache can be used, but the value of memcache only includes the string type. There are far fewer redis value types. Redis also supports master-slave replication ). Other features of redis include simple transaction support and pub/sub channel functions, and redis configuration management is very simple. There are also open-source client class libraries in various languages. There are many similar open-source products. I have been familiar with memcache, membase, and memlink.

Note: The following describes the path where the redis file is stored. The default storage location varies depending on the version, for example, redis in redis1.6. conf is stored in the root directory of redis, while redis in redis2.2.5. the conf file is stored in redis/src/. Therefore, the file storage location in the following explanation is inconsistent with the version, resulting in inconsistent file locations by default.

Ii. Installation

1) download redis

Currently, the redis version has reached 2.2.5, but our client uses jredis instead of Jedis. Therefore, for jredis, The redis version we downloaded cannot exceed 1.2.6, because instructions have been made on the official jredis Website: for example:

Therefore, if the client uses jredis, please download the redis version 1.2.6 license at this address (the most basic difference ).

2) Extract

Decompress the downloaded redis

3) install the C/C ++ compilation component (optional)

Apt-Get install build-essential

4) Compile

CD redis-2.2.5
Make

After the make command is executed, an executable file is generated under the current directory, including redis-server, redis-CLI, redis-benchmark, and redis-Stat. Their functions are as follows:

Redis-server: Daemon Startup Program of the redis Server

Redis-CLI: redis command line operation tool. You can also use Telnet to operate based on its plain text protocol.

Redis-benchmark: redis performance testing tool to test the read/write performance of redis in your system and your configuration

Redis-stat: redis status detection tool that can detect redis's current status parameters and latency

5) start the service

ØStart by default

After we successfully install redis, we can directly execute redis-server to run redis. At this time, it runs according to the default configuration (the default configuration is not even the background operation ).

First, go to the src directory of redis, and then run./redis-server to start

ØConfigure startup

If we want redis to run as required, we need to modify the configuration file, first put the redis configuration file. conf CP to the/etc directory, modify it to configure our server. For the configuration content, see section 6. Related configuration files and descriptions. After the configuration is complete, run the following command to start the server:

/...... /Redis-server /....... /Etc/redis. conf (started in the background) or /...... /Redis-server (non-Background startup)

ØCheck whether startup is successful
PS-Ef | grep redis (check whether the service is enabled in the background through the process). You can directly view the service if it is not started in the background.

ØClose service

Redis-cli Shutdown

ØSimple Test

Start the redis-cli tool that comes with redis for testing. Use set and get to save, obtain, and save local disks.

ØAdd to start

VI/etc/rc. d/rc. Local

Redis-server/etc/redis. conf

The redis port must be opened. Otherwise, the client cannot connect. The method is as follows:

VI/etc/sysconfig/iptables # You must have the modification permission.

Add a row:

# Redis
-A RH-Firewall-1-INPUT-M state -- state new-m tcp-p tcp -- dport 6379-J accept

Restart iptables after saving:

Service iptables restart (/sbin/included in path) or/etc/init. d/iptables restart

Iii. Description and modification of related configuration files

Ø/Etc/sysctl. conf

Add: VM. overcommit_memory = 1 refresh the configuration to make it take effect

Ø
/Proc/sys/Vm/overcommit_memory

If the memory is insufficient, you need to set the kernel parameters:

The overcommit_memory file specifies the kernel's memory allocation policy. The value can be 0, 1, or 2.
0 indicates that the kernel will check whether there is enough available memory for use by the process. If there is enough available memory, the memory application will be allowed; otherwise, the memory application will fail, and return the error to the application process.
1 indicates that the kernel allows all physical memory allocation regardless of the current memory status.
2. indicates that the kernel is allowed to allocate more memory than the total physical memory and swap space.

Ø/Etc/redis. conf

Redis. conf configuration options are as follows:
Whether daemonize is run by a later process. The default value is no.
If pidfile is run by a later process, you must specify a PID. The default value is/var/run/redis. PID.
Bind Host IP address. Default Value: 127.0.0.1 (note)
Port listening port. The default value is 6379.
Timeout timeout. The default value is 300 seconds)
Loglevel log record level, which has four optional values: Debug, verbose (default), notice, and warning
Logfile logging method. The default value is stdout.
Number of available databases in databases. The default value is 16 and the default value is 0.
Save indicates how many update operations are performed within a specified period of time, and the data is synchronized to the data file. This can be used with multiple conditions. For example, three conditions are set in the default configuration file.
Save 900 1 900 seconds (15 minutes) at least one key is changed
Save 300 10 300 seconds (5 minutes) at least 300 keys are changed
Save 60 10000 at least 10000 keys are changed within 60 seconds
Whether the data is compressed when rdbcompression is stored in the local database. The default value is yes.
Dbfilename: name of the local database file. The default value is dump. RDB.
Path for storing the Dir local database. The default value is ./
Slaveof when the local machine is a slave service, set the IP address and port of the master service (note)
Masterauth when the local machine is a slave service, set the master service connection password (comment)
Requirepass connection password (note)
Maxclients maximum number of client connections, no limit by default (note)
When maxmemory sets the maximum memory to reach the maximum memory setting, redis will first try to clear expired or expiring keys. After this method is processed, it will reach the maximum memory setting, no more write operations can be performed. (Note)
Whether appendonly records logs after each update operation. If it is not enabled, data may be lost for a period of time during power failure. Because redis synchronizes data files according to the Save conditions above, some data will only exist in the memory for a period of time. The default value is no.
Appendfilename: update the log file name. The default value is appendonly. aof (comment)
Appendfsync updates log conditions. There are three optional values. "No" indicates that data is cached and synchronized to the disk by the operating system. "Always" indicates that data is manually written to the disk by calling fsync () after each update operation. "everysec" indicates that data is synchronized once per second (default ).
Whether VM-enabled uses virtual memory. The default value is no.
VM-Swap-file: virtual memory file path. The default value is/tmp/redis. Swap. It cannot be shared by multiple redis instances.
VM-max-memory can only use the maximum memory value. If this value is exceeded, the virtual memory is used. In the swap mode, the memory is mainly used in the virtual memory. The default value is 0.

Iv. Test

ØJava client call Test

To establish a Java client test environment, the redis client first needs to call the dependent Java package. I used two methods for testing: 1. jredis 2. Jedis

I have written the test code for both of the above two clients. The differences are as follows:

Redis version: 2.5.5

Jredis: jredis directly throws an exception.

Jedis: it can store data to the server, but does not support the storage of sets and objects as values.

 

Redis version: 1.6

Jredis: It can be normally accessed, and objects and sets can be stored as values.

Jedis: it can store data to the server, but does not support the storage of sets and objects as values.

The two common defects mentioned above: High concurrency cannot be guaranteed (exceptions are thrown when I start more than 70 threads. Therefore, the jedispool and jedispoolconfig instances need to be pooled, otherwise, it is difficult to support high concurrency of large data volumes) and Stability of large data volumes.

Jredis and Jedis

Jredis http://code.google.com/p/jredis/

Jedis: https://github.com/xetorthio/jedis/downloads

 

In the image, 70 threads are enabled for the value:

 

Download the relevant code from the attachment.

 

ØTest the built-in tool

The following is a simple test. I prefer to save the corresponding data to the memory, then obtain the stored information from the memory, and finally dump the data stored in the memory to the hard disk.

Run the command to start the client under the src directory of redis (the premise is to start the server first. I am here to facilitate the test, but it is not started through the background. It is easy to observe through non-Background startup ).

Non-background server startup :/.... /Redis-Server

Start the client :/... /Redis-cli

Run the following command on the client: Set (save a key-Value Pair) key value, get (get a key-Value Pair) Key, for example:

 

The server displays the following information:

 

 

Run the Save command on the client, for example:


The server displays the following information:

 

The files saved to the hard disk are:

 

After opening the file, you can view the saved information.

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.