Redis introduction,

Source: Internet
Author: User

Redis introduction,

Redis Introduction
Redis is short for REmote DIctionary Server. It stores data in a DIctionary structure (key-value Pair structure, other applications are allowed to read and write contents in the dictionary through the TCP protocol. Therefore, redis is a key-value storage system, or a key-value database.

Redis is developed in C language and provides client libraries in dozens of different programming languages. We can also operate redis in java. The storage structure in this dictionary form is significantly different from the storage structure in the form of two tables in common MySQL and other relational databases. Data stored in relational databases is stored in structured data Redis dictionary structure (k-v), allowing developers to directly map data in programs to Redis, the storage of data in Redis is very similar to that in the program. Another advantage of using Redis is that it provides a very convenient operation method for different data types, such as using the collection type to store Article tags, redis can perform set operations such as intersection and Union operations on tags. Redis stores data in memory, so it can provide efficient reading. Therefore, redis databases are also called non-relational databases (Nosql ). Mysql, oracle, and other databases are called relational databases. There are no advantages or disadvantages of these two types of databases, depending on the specific application scenarios.

Redis memory storage and persistence all data in the Redis database are stored in the memory. Because the memory read/write speed is much faster than the hard disk, Redis has obvious advantages over other hard disk-based databases in terms of performance. On a general laptop, edis can read and write more than 100,000 key values in one second. There are also problems with storing data in the memory. For example, data in the memory will be lost after the program exits. However, Redis provides persistence support, that is, data in the memory can be asynchronously written to the hard disk without affecting the service.

Steps:
1. Download the Jedis dependent jar package
2. Create a test class in the maven Project
3. Call the Jedis API

/*
* Test connection and Interpolation
* Note: 1. Disable the firewall of the virtual machine or open a specified port, such as 6379.
*/
@ Test
Public void testConnect_Set (){
Jedis jedis = new Jedis ("192.168.234.231", 6379 );
Jedis. set ("name", "tom ");

Jedis. close ();
}

/*
* The test uses Jedis to implement Redis sharding. The underlying layer uses the consistent hash algorithm.
* Therefore, it can better meet the monotonicity and balance of data.
*
*/
@ Test
Public void testShard (){
JedisPoolConfig config = new JedisPoolConfig ();

List <JedisShardInfo> serverList = new ArrayList <> ();

ServerList. add (new JedisShardInfo ("192.168.234.231", 6379 ));
ServerList. add (new JedisShardInfo ("192.168.234.231", 6380 ));

ShardedJedisPool pool = new ShardedJedisPool (config, serverList );

ShardedJedis jedis = pool. getResource ();

For (int I = 0; I <1000; I ++ ){
Jedis. set (I + "", I + "");
}
// After the jedis connection is used up, return the jedis connection to the pool.
Pool. returnResource (jedis );

}

}

 

1/** 2 * test using Jedis to operate redis Service 3 * @ author 4*5 */6 public class redis {7/* 8 * test connection and interpolation 9 * Note: 1. disable the firewall of the VM or open the specified port on behalf of the VM, such as 637910 */11 @ Test12 public void testConnect_Set () {13 Jedis jedis = new Jedis ("192.168.234.231", 6379); 14 jedis. set ("name", "tom"); 15 16 jedis. close (); 17} 18 19/* 20 * test using Jedis to implement Redis sharding. The underlying layer is implemented using consistent hash algorithms. 21 * so it can better satisfy the monotonicity and balance of data 22*23 */24 @ Test25 public void testShard () {26 JedisPoolConfig config = new JedisPoolConfig (); 27 28 List <JedisShardInfo> serverList = new ArrayList <> (); 29 30 serverList. add (new JedisShardInfo ("192.168.234.231", 6379); 31 serverList. add (new JedisShardInfo ("192.168.234.231", 6380); 32 33 ShardedJedisPool pool = new ShardedJedisPool (config, serverList); 34 35 ShardedJedis jedis = pool. getResource (); 36 37 for (int I = 0; I <1000; I ++) {38 jedis. set (I + "", I + ""); 39} 40 // After the jedis connection is used up, return it to pool 41. returnResource (jedis); 42 43} 44 45}

 

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.