<Web> How to add a Redis service to a WEB project

Source: Internet
Author: User
Tags download redis install redis

Objective

This two-day project used Redis, and I was preparing to see it myself, so I learned it from the beginning.

Since to use, explain what has been done to Redis, what are the features, what are the benefits of the project, need to cache what things and so on have been known, so we directly engaged in:

How to get Redis

Install Redis,linux under wget to GitHub source code, and then directly make on the line, relatively simple.

I use it under Windows, So to say: to the redisserver from the Redis website connected to the past download Redis-2.8.19.zip, after decompression run Redis-server.exe can, and then open Redis-cli.exe can be used. Generally this is used only to test the connection and view the monitor.

How to use Redis on the Java side

In the Java project, Redis provides a number of client libraries, the Jedis I use, which encapsulates all operations on the Redis database.

In general Web applications, we also use spring, which is handy because spring itself provides support for Redis--spring-data-redis, which can be viewed on the website and has an example.

Specific steps:

1. Introduce pom dependency (if you don't use MAVEN, introduce yourself to the jar pack)

<Dependencies>    <Dependency>        <groupId>Org.springframework.data</groupId>        <Artifactid>Spring-data-redis</Artifactid>        <version>1.5.0.RELEASE</version>    </Dependency></Dependencies>

At the same time will Jedis also rely on, because Spring-data-redis bottom is the use of Jedis:

<Dependency>            <groupId>Redis.clients</groupId>            <Artifactid>Jedis</Artifactid>            <version>2.6.0</version>            <type>Jar</type>            <Scope>Compile</Scope>        </Dependency>

2. Configure the Redistemplate instance in the container

<BeanID= "Jedisconnfactory"class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory"P:use-pool= "true"/><!--redis Template Definition -<BeanID= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate"P:connection-factory-ref= "Jedisconnfactory"/>

Note: This is a direct copy of the official website, I put it directly in the XML, will be error, even the introduction of the name of the P-space. Then I tried to write the P attribute to the property, there is no error, but in the later use of the key and value will appear similar to "\XAC\XED\X00\X05T\X00\TB" this situation, The query learned that this is because Spring-data-redis needs to store the data in Jedis, which needs to be serialized, and then stored by Jedis to the database, the problem is serialized, it will also serialize the type information of key and value, This is obviously not the result we want.

So Redistemplate provides a serialization class for key and value, so I'm going to do this:

<BeanID= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate">        < Propertyname= "ConnectionFactory"ref= "Jedisconnfactory"/>        < Propertyname= "Keyserializer">            <Beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/>        </ Property>        < Propertyname= "Hashkeyserializer">            <Beanclass= "Org.springframework.data.redis.serializer.GenericToStringSerializer"/>        </ Property>        < Propertyname= "ValueSerializer">            <Beanclass= "Org.springframework.data.redis.serializer.GenericToStringSerializer"/>        </ Property>        < Propertyname= "Hashvalueserializer">            <Beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/>        </ Property>    </Bean>

But the middle of the two serialization class or error, no parameterless constructor, so ready to use the constructor injection, but the parameters required to inject is a object.class type, I do not know how to inject this type, to engage or simply do not XML, directly write configuration class:

@Configuration Public classredisconfig {@Bean jedisconnectionfactory jedisconnectionfactory () {return Newjedisconnectionfactory (); } @Bean redistemplate<string, object>redistemplate () {Finalredistemplate<string, object> template =NewRedistemplate<>();        Template.setconnectionfactory (Jedisconnectionfactory ()); Template.setkeyserializer (NewStringredisserializer ()); Template.sethashkeyserializer (NewGenerictostringserializer<> (Object.class)); Template.setvalueserializer (NewGenerictostringserializer<> (Object.class)); returntemplate; }}

This can be counted (note the scan package, of course).

With Redistemplate, you can write a specific implementation of the business class, Redistemplate basic support for all redis operations, such as:

@Component Public classSpringredisclientimplImplementsspringredisclient {@AutowiredPrivateRedistemplate<string, object>template; @AutowiredPrivatejedisconnectionfactory jedisconnectionfactory; @Override Public voidSetkey (string key, String value) {Template.opsforvalue (). Set (key, value); } @Override PublicObject GetKey (String key) {returnTemplate.opsforvalue (). get (key); } @Override Public voidincr (String key) {Template.opsforvalue (). Increment (Key,1); } @Override Public voidLpush (string key, String value) {template.opsforlist (). Leftpush (key, value); } @Override Public BooleanCheckkey (String key) {returnTemplate.haskey (key); } @Override PublicObject LIndex (String key) {returnTemplate.opsforlist (). Index (key, 0); } @Override PublicLong llength (String key) {returntemplate.opsforlist (). Size (key); } @Override Publicstring Lpop (String key) {return(String) template.opsforlist (). Leftpop (key); } @Override PublicSet<string>Getkeys (String pattern) {returnTemplate.keys (pattern); } @Override Public voidFlushall () {Jedis Jedis=jedisconnectionfactory.getshardinfo (). Createresource ();        Jedis.flushall ();    Jedis.close (); }}

Finally, start Redis-server.exe (here to pay attention to start this, and then start the Web project, otherwise not start, reason unknown), if you are familiar with Redis, you can configure the redis.windows.conf file yourself. In fact, I'm not too familiar with the configuration in the Conf file, especially persistent to disk. So in my project, I was saving the data to MySQL every day and emptying Redis.

Recommended

To use the appropriate method of redistemplate, you should be familiar with its role, and a Redis command reference is recommended here.

  

  

<Web> How to add a Redis service to a WEB project

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.