Official website help documents are as follows
Installation
Download, extract and compile Redis with:
$ wget http://download.redis.io/releases/redis-3.0.2.tar.gz$ tar xzf redis-3.0.2.tar.gz$ cd redis-3.0.2$ make
The binaries that is now compiled is available in the src
directory. Run Redis with:
$ src/redis-server
You can interact with Redis using the built-in client:
$ src/redis-cliredis> set foo barOKredis> get foo"bar"
Of course, the premise is your Linux network, and has installed GCC and other c,c++ operating environment
And then you can integrate with spring.
Maven Plus
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>< Version>2.6.2</version><type>jar</type><scope>compile</scope></dependency >
Applicationcontext.xml separately plus
<!--properties File location--><bean id= "Annotationpropertyconfigurer" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "><property name=" Locations "> <list><value>classpath:config/properties/jdbc.properties</value><value>classpath: config/properties/common.properties</value><value>classpath:config/properties/log4j.properties< /value><value>classpath:config/properties/redis.properties</value></list></property ></bean>
<!--Configuring the Redis thread pool--><bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" ><!--< Property Name= "Maxidle" value= "${redis.pool.maxidle}"/> <property name= "maxactive" value= "${ redis.pool.maxActive} "/> <property name=" maxwait "value=" ${redis.pool.maxwait} "/>--><property name= "Testonborrow" value= "${redis.pool.testonborrow}"/></bean><!--connect to Redis--><bean id= " ConnectionFactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "P:host-name=" $ {redis.host} "p:port=" ${redis.port} "p:password=" ${redis.password} "p:pool-config-ref=" Poolconfig "/><!-- Redis calls required bean--><bean id= "redistemplate" class= "Org.springframework.data.redis.core.StringRedisTemplate" ><property name= "ConnectionFactory" ref= "ConnectionFactory"/></bean>
The redis.properties is configured as follows
redis.pool.maxtotal=100redis.pool.maxidle=20redis.pool.maxwait=1000redis.pool.testonborrow=trueredis.host= 10.13.3.101redis.port=6379redis.password=
The code is as follows
Package Com.kugou.security.service.impl;import Javax.annotation.resource;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;import Org.springframework.data.redis.serializer.redisserializer;import Org.springframework.stereotype.service;import Com.kugou.security.entity.sysuser;import Com.kugou.security.service.SysUserService; @Servicepublic class Sysuserserviceimpl implements Sysuserservice {@ResourceRedisTemplate <string, sysuser> redistemplate;public Boolean addUser (Final Sysuser Sysuser) {Boolean result = This.redisTemplate.execute (new rediscallback<boolean> () {@Overridepublic Boolean Doinredis (redisconnection connection) throws DataAccessException {redisserializer<string > serializer = Redistemplate.getstringserializer (); byte[] key = Serializer.serialize (Sysuser.getid ()); byte[] name = SerializEr.serialize (Sysuser.getnickname ()); return Connection.setnx (key, name);}}); return result;} public string Get (final String userId) {string result = Redistemplate.execute (new rediscallback<string> () {public S Tring Doinredis (redisconnection connection) throws DataAccessException {redisserializer<string> serializer = Redistemplate.getstringserializer (); byte[] key = Serializer.serialize (userId); byte[] value = Connection.get (key); if ( Value = = null) {return null;} String name = serializer.deserialize (value); return name;}}); return result;}}
Test as follows
Package Test.kugou;import Org.junit.runner.runwith;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.springjunit4classrunner;import Org.springframework.test.context.transaction.TransactionConfiguration, @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"Classpath:config/spring/applicationcontext.xml"}) @TransactionConfiguration ( TransactionManager = "TransactionManager", Defaultrollback = true) public class Basetest {public void TestA () { } }
Package Test.kugou.security.dao.service.impl;import Javax.annotation.resource;import Org.junit.Test;import Com.kugou.security.entity.sysuser;import Com.kugou.security.service.sysuserservice;import test.kugou.BaseTest; public class Testsysuserservice extends Basetest {@ResourceSysUserService sysuserservice; @Testpublic void Testredis () { Sysuser sysuser=new Sysuser (); Sysuser.setid ("AA"); Sysuser.setnickname ("Test"); Boolean result= This.sysUserService.addUser (Sysuser); SYSTEM.OUT.PRINTLN (result); String nickname=this.sysuserservice.get ("AA"); System.out.println (nickname);}}
Run successfully
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Redis installs and tests under Linux (cut in spring)