The use of Redis is very extensive, its advantages are fast, support rich data types, support things operation, such as session cache, full page cache (FPC), queue, etc., greatly reduce the burden of the database.
1. Install and download Redis
Website: https://github.com/MicrosoftArchive/redis/releases, after successful installation, turn on Redis service.
2. Importing the JAR Package
Commons-pool.jar jedis.jar spring-data-redis.jar fastjson.jar Aopalliance.jar
Note: The version of the Jedis and Commons-pool two jar packages has a corresponding relationship, note the reference when you want to use pairing, otherwise it will be an error.
3. Configuring Redis Files
Redis.properties:
redis.hostname=127.0 . 0.1 redis.port =6379 redis.timeout =15000 redis.usepool =true redis.maxtotal =32 redis.maxidle =20 redis.minidle =10 redis.minevictableidletimemillis =300000 redis.numtestsperevictionrun =3 Span style= "COLOR: #000000" >redis.timebetweenevictionrunsmillis =60000
Spring-redis.xml:
<context:property-placeholder location="classpath:redis.properties"ignore-unresolvable="true"/> <!--Redis Single-point configuration-<bean id="Jedispoolconfig" class="Redis.clients.jedis.JedisPoolConfig"> <property name="Maxtotal"Value="${redis.maxtotal}"></property> <property name="Maxidle"Value="${redis.maxidle}"></property> <property name="Minidle"Value="${redis.minidle}"></property> <property name="Minevictableidletimemillis"Value="${redis.minevictableidletimemillis}"></property> <property name="Numtestsperevictionrun"Value="${redis.numtestsperevictionrun}"></property> <property name="Timebetweenevictionrunsmillis"Value="${redis.timebetweenevictionrunsmillis}"></property> <property name="Testonborrow"Value="true"></property> </bean> <bean id="jedisconnectionfactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"Destroy-method="Destroy"> <property name="Database"Value="5"></property> <property name="Poolconfig" ref="Jedispoolconfig"></property> <property name="HostName"Value="${redis.hostname}"></property> <property name="Port"Value="${redis.port}"></property> <property name="Timeout"Value="${redis.timeout}"></property> <property name="Usepool"Value="true"></property> </bean> <bean id="jedistemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="ConnectionFactory" ref="jedisconnectionfactory"></property> </bean> <bean id="Redisutil" class="Com.util.RedisUtil"> <property name="redistemplate" ref="jedistemplate"></property> </bean>
4. Fill in the following information in the Spring master configuration file:
<beanclass="Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="Systempropertiesmodename"Value="System_properties_mode_override"/> <property name="Ignoreresourcenotfound"Value="true"/> <property name="Locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean><import resource="Spring-redis.xml"/>
5. Creating a Tool Class (Redisutil)
Package Com.util;import Java.io.unsupportedencodingexception;import java.util.list;import Org.springframework.dao.dataaccessexception;import org.springframework.data.redis.connection.RedisConnection; Import Org.springframework.data.redis.core.rediscallback;import Org.springframework.data.redis.core.RedisTemplate;/** * * <p>title:redis Operation class </p> * Basic method of <p>description:redis operation </p> * <p>company:yangcnye </p>*/ Public classRedisutil<t> { PrivateFinalStaticString Rediscode ="Utf-8"; protectedRedistemplate<string, t>redistemplate; PublicRedistemplate<string, t>getredistemplate () {returnredistemplate; } Public voidSetredistemplate (redistemplate<string, t>redistemplate) { This. redistemplate =redistemplate; } /** * string Add * @param key * @param value * @param livetime*/ Public void Set(string key, String value,Longlivetime) { Try { This.Set(Key.getbytes (Rediscode), Value.getbytes (Rediscode), livetime); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } }}
5. Test if the cache is successful, create a controller
Package Com.controller;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.requestparam;import Com.alibaba.fastjson.json;import Com.util.RedisUtil;/** * Redis Cache Use * * @author Yangcnye **/@Controller @requestmapping ("/redis") Public classRediscontroller {Private Static inti =1; Private Static intj =1; Private Static intK =0; Private StaticFinalintL =0; @AutowiredPrivateRedisutil<object>Redis; @RequestMapping (Value="/redistest", method =requestmethod.post) PublicString Redistest (@RequestParam ("Description"String description) {System. out. println (description); Redis.Set("Num", json.tojsonstring (description),6000); return "Success"; }}
The above is based on the spring project Redis simple application, follow-up, Welcome to exchange.
The spring framework-based Redis cache