[project build six]babasport Redis usage instance.

Source: Internet
Author: User
Tags redis cluster

Redis was used in the project because it only involves the use of Redis to generate primary keys for the time being, so this article describes the Redis installation approach and the advantages of redis-generated primary keys, as well as the comparison of generating primary keys in several other ways.

1,redis Installation
First copy the Redis tar package to the root directory under Linux


Then unzip to the Redis folder: (use mkdir to create the Redis folder First)


The next step is to unpack the tar package into the Redis directory:

The directory structure after decompression:


Compiling: using the Make command

Installation:


After installing the Directory:


6379 directory structure: (this rdb file: redis database, temporarily do not control it, restart automatically generated After)


Directory structure under Bin:


Configure background Run: (copy The redis.conf file under the redis-3.0.0 directory to the 6379 directory, using the CP Command)


Edit redis.conf file (edit with vim command, modify daemonize to yes, meaning support background run)


Start Redis service: (start and stop Commands)

Client Side Connection server: (if remote connection:./bin/redis-cli-h 192.168.200.128-p 6379)

Command line Demo:


Such a redis will start to Complete.

2. Advantages of using REDIS to generate primary keys and comparison with other generated primary key methods
Redis Build ID
When using a database to generate an ID with insufficient performance requirements, we can try to use Redis to generate the ID. This relies primarily on redis being single-threaded, so it can also be used to generate globally unique IDS. Can be implemented using Redis atomic operations incr and Incrby.

You can use a Redis cluster to get higher Throughput. If there are 5 redis in a cluster. The values for each Redis can be initialized are 1,2,3,4,5, and the step size is 5. The IDs generated by each Redis are:

a:1,6,11,16,21

b:2,7,12,17,22

c:3,8,13,18,23

d:4,9,14,19,24

e:5,10,15,20,25

this, the random load to which machine ok, the future is difficult to make Changes. however, 3-5 servers are basically able to satisfy the device, you can get a different ID. however, the step size and initial value must be required beforehand. Using a Redis cluster can also be a single point of failure problem.

In addition, it is more appropriate to use Redis to generate a serial number starting from 0 per day. For example, the order number = date + The day from the growth number. You can generate a key in Redis every day and use INCR to Accumulate.

Advantages:

1) not dependent on the database, flexible and convenient, and performance is better than the Database.

2) The digital ID is a natural sort, which is helpful for paging or the need to sort the results.

Disadvantages:

1) If there is no redis in the system, new components need to be introduced to increase the complexity of the System.

2) requires a large amount of coding and Configuration.

The advantages of using int key:

1, need very small data storage space, only need 4 Byte.

2, Insert and UPDATE operations use INT for better performance than guids, so using int will improve the performance of your Application.

3, index and join operations, int is the best performance.

4, Easy to Remember.

5, support through the function to get the latest value, such as: scope_indentity ().

Disadvantages of using int key

1, if the operation of the table is often merged, the primary key duplication may Occur.

2, the use of INT data range is Limited. If there is a large amount of data, the range of values for int may be exceeded.

3, It is difficult to deal with distributed storage data Tables.

Advantages of using the GUID master Key:

1, It is Unique.

2, there is less chance of Duplication.

3, suitable for large amount of data in the insert and update operations.

4, cross-server Data merging is very convenient.

Disadvantages of using the GUID master Key:

1, storage space is large (in bytes), so it will take up more disk Size.

2, Difficult to Remember. Join operation performance is lower than int.

3. There is no built-in function to get the newly generated GUID primary key.

4. The GUID master key will be added to the other indexes on the table, thus reducing performance.


3. Demo using Redis in code
Java Interface Test Redis:



Spring and Redis consolidation:

Service Layer: (productserviceiml.java)

@Autowired
Private Jedis jedis;

2      public voidinsertproduct (product Product) {3         //Product Settings4         //ID self-growth is not a good way to use Redis build ID here5Long id = jedis.incr ("pno");6 Product.setid (id);7         8         //set default lower shelf9Product.setisshow (false);Ten         //default does not delete oneProduct.setisdel (false); a         //Time -Product.setcreatetime (NewDate ()); -          the         //Save the product, write in Mapper.xml to return the Self-growing primary key id, and then cascade to save the SKU table -         //but product will no longer return the primary key ID if it has already set the ID. - productdao.insertselective (product); -          +         //Inventory Multiple -          for(String color:product.getColors (). Split (",")) { +             //size a              for(String size:product.getSizes (). Split (",")){ atSKU SKU =NewSku (); -                 //Product ID This sets the return primary key ID in Mapper.xml - Sku.setproductid (product.getid ()); -                 //Color - Sku.setcolorid (long.parselong (color)); -                 //Market Price in Sku.setmarketprice (0f); -                 //Price to Sku.setprice (0f); +                 //Shipping - Sku.setdelivefee (10f); the                 //Purchase Restrictions *Sku.setupperlimit (188); $                 //sizePanax Notoginseng sku.setsize (size); -                 //Time theSku.setcreatetime (NewDate ()); +                 //Stock aSku.setstock (0); the skudao.insertselective (sku); +             } -         } $}

See this top uses Jdis to invoke the Redis service, and then use INCR to add 1 to pno (which can be set to pno in redis). Previously used are self-growing ids, which automatically returns the primary key ID to product after insert is completed in mapper.xml, and can be used directly with Product.getid () when cascading is Saved. Here's a look at the configuration of Redis and spring Consolidation.




Redis.xml Configuration File:

1 <Beansxmlns= "http://www.springframework.org/schema/beans"2 Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "http://www.springframework.org/schema/mvc"3 Xmlns:context= "http://www.springframework.org/schema/context"4 XMLNS:AOP= "http://www.springframework.org/schema/aop" 5 Xmlns:tx= "http://www.springframework.org/schema/tx"6 Xmlns:task= "http://www.springframework.org/schema/task"7 Xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo"8 xsi:schemalocation= "http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans-4.0.xsdTen Http://www.springframework.org/schema/mvc one http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd a Http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-4.0.xsd - HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP the http://www.springframework.org/schema/aop/spring-aop-4.0.xsd - Http://www.springframework.org/schema/tx - http://www.springframework.org/schema/tx/spring-tx-4.0.xsd - Http://www.springframework.org/schema/task + http://www.springframework.org/schema/task/spring-task-4.0.xsd - Http://code.alibabatech.com/schema/dubbo + http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> a          at         <!--Integrated Redis - -         <BeanID= "jdis"class= "redis.clients.jedis.Jedis"> -             <Constructor-argvalue= "192.168.200.128"Index= "0"type= "java.lang.String"/> -             <Constructor-argvalue= "6379"Index= "1"/> -         </Bean>         -          in </Beans>
View Code


The use of Redis is so much for the time being that we will continue to share More.






[project build six]babasport Redis usage instance.

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.