springDataRedis

依赖

    <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");
}

//取出keys
@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);
}

//根据key取值
@Test

public void testGetValueByKey(){

Object object = redisTemplate.boundHashOps("namehash").get("b");

System.out.println(object);

}

//根据 KEY 移除值
@Test

public void testRemoveValueByKey(){

redisTemplate.boundHashOps("namehash").delete("c");

}
 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值