依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
redis-config.properties
# Redis settings
# server IP
redis. host= 192.168 .100 .101
# server port
redis. port= 6379
# server pass
redis. pass=
# use dbIndex
redis. database= 0
#
redis. maxIdle= 300
#
redis. maxWait= 3000
#
redis. testOnBorrow= true
applicationContext-redis
< ? xml version= "1.0" encoding= "UTF-8" ? >
< beans xmlns= "http://www.springframework.org/schema/beans"
xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns: p= "http://www.springframework.org/schema/p"
xmlns: context= "http://www.springframework.org/schema/context"
xmlns: mvc= "http://www.springframework.org/schema/mvc"
xmlns: cache= "http://www.springframework.org/schema/cache"
xsi: schemaLocation= "http: / / www. springframework. org/ schema/ beans
http: / / www. springframework. org/ schema/ beans/ spring- beans. xsd
http: / / www. springframework. org/ schema/ context
http: / / www. springframework. org/ schema/ context/ spring- context. xsd
http: / / www. springframework. org/ schema/ mvc
http: / / www. springframework. org/ schema/ mvc/ spring- mvc. xsd
http: / / www. springframework. org/ schema/ cache
http: / / www. springframework. org/ schema/ cache/ spring- cache. xsd">
< context: property- placeholder location= "classpath*:properties/*.properties" / >
< ! -- redis 相关配置 -- >
< bean id= "poolConfig" class = "redis.clients.jedis.JedisPoolConfig" >
< property name= "maxIdle" value= "${redis.maxIdle}" / >
< property name= "maxWaitMillis" value= "${redis.maxWait}" / >
< property name= "testOnBorrow" value= "${redis.testOnBorrow}" / >
< / bean>
< bean id= "JedisConnectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p: host- name= "${redis.host}" p: port= "${redis.port}" p: password= "${redis.pass}" p: pool- config- ref= "poolConfig" / >
< bean id= "stringRedisTemplate" class = "org.springframework.data.redis.core.StringRedisTemplate" >
< property name= "connectionFactory" ref= "JedisConnectionFactory" / >
< / bean>
< bean id= "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate" >
< property name= "connectionFactory" ref= "JedisConnectionFactory" / >
< / bean>
< / beans>
字符串操作
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( locations= "classpath:spring/applicationContext-redis.xml" )
public class TestString {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void testSetValue ( ) {
stringRedisTemplate. boundValueOps ( "name" ) . set ( "zym" ) ;
}
@Test
public void testGetValue ( ) {
String s = stringRedisTemplate. boundValueOps ( "name" ) . get ( ) ;
System. out. println ( s) ;
}
@Test
public void testDelValue ( ) {
stringRedisTemplate. delete ( "name" ) ;
}
}
Set 类型操作
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( locations= "classpath:spring/applicationContext-redis.xml" )
public class TestSet {
@Autowired
private RedisTemplate< String, String> redisTemplate;
@Test
public void testSetValue ( ) {
redisTemplate. boundSetOps ( "nameset" ) . add ( "liming" ) ;
redisTemplate. boundSetOps ( "nameset" ) . add ( "liuhu" ) ;
redisTemplate. boundSetOps ( "nameset" ) . add ( "zhoujian" ) ;
}
@Test
public void testGetValue ( ) {
Set< String> members = redisTemplate. boundSetOps ( "nameset" ) . members ( ) ;
System. out. println ( members) ;
} }
@Test
public void testRemove ( ) {
redisTemplate. boundSetOps ( "nameset" ) . remove ( "zhoujian" ) ;
}
@Test
public void testDelValue ( ) {
redisTemplate. delete ( "nameset" ) ;
}
List 类型操作
@Test
public void testSetValue1 ( ) {
redisTemplate. boundListOps ( "namelist1" ) . rightPush ( "liubei" ) ;
redisTemplate. boundListOps ( "namelist1" ) . rightPush ( "guanyu" ) ;
redisTemplate. boundListOps ( "namelist1" ) . rightPush ( "zhangfei" ) ;
}
@Test
public void testGetValue1 ( ) {
List< String> list = redisTemplate. boundListOps ( "namelist1" ) . range ( 0 , 100 ) ;
System. out. println ( list) ;
}
@Test
public void testSetValue2 ( ) {
redisTemplate. boundListOps ( "namelist2" ) . leftPush ( "liubei" ) ;
redisTemplate. boundListOps ( "namelist2" ) . leftPush ( "guanyu" ) ;
redisTemplate. boundListOps ( "namelist2" ) . leftPush ( "zhangfei" ) ;
}
@Test
public void testGetValue2 ( ) {
List< String> list = redisTemplate. boundListOps ( "namelist2" ) . range ( 0 , 100 ) ;
System. out. println ( list) ; }
@Test
public void testSearchByIndex ( ) {
String s = redisTemplate. boundListOps ( "namelist1" ) . index ( 1 ) ;
System. out. println ( s) ;
}
@Test
public void testRemoveByIndex ( ) {
redisTemplate. boundListOps ( "namelist1" ) . remove ( 1 , "guanyu" ) ;
}
Hash 类型操作
@Test
public void testSetValue ( ) {
redisTemplate. boundHashOps ( "namehash" ) . put ( "a" , "tangseng" ) ;
redisTemplate. boundHashOps ( "namehash" ) . put ( "b" , "sunwukong" ) ;
redisTemplate. boundHashOps ( "namehash" ) . put ( "c" , "zhubajie" ) ;
redisTemplate. boundHashOps ( "namehash" ) . put ( "d" , "shaheshang" ) ;
}
@Test
public void testGetKeys ( ) {
Set< Object> s = redisTemplate. boundHashOps ( "namehash" ) . keys ( ) ;
System. out. println ( s) ;
}
@Test
public void testGetValues ( ) {
List< Object> values = redisTemplate. boundHashOps ( "namehash" ) . values ( ) ;
System. out. println ( values) ;
}
@Test
public void testGetValueByKey ( ) {
Object object = redisTemplate. boundHashOps ( "namehash" ) . get ( "b" ) ;
System. out. println ( object) ;
}
@Test
public void testRemoveValueByKey ( ) {
redisTemplate. boundHashOps ( "namehash" ) . delete ( "c" ) ;
}