Redis 在 Java 中的使用
转:http://blog.csdn.net/jiangtao_st/article/details/8256610
一、下载jar包
https://github.com/xetorthio/jedis/downloads
学习参考内容 :
http://blog.nosqlfan.com/html/3537.html
二、在spring 中的配置
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">- <property name="maxActive" value="50" />
- <property name="maxIdle" value="10" />
- <property name="maxWait" value="1000" />
- <property name="testOnBorrow" value="true"/>
- </bean>
- <bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg index="0" value="这里填你的reids服务器ip" />
- <constructor-arg index="1" value="6379" />
- </bean>
- <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
- <constructor-arg index="0" ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <ref bean="jedis.shardInfo" />
- </list>
- </constructor-arg>
- </bean>
三、在Java代码中的使用
publicclassRedis{
publicstaticvoidmain(Stringargs[]){
//连接redis服务
Jedisjedis=newJedis("192.168.0.234",6400);
//密码验证-如果你没有设置redis密码可不验证即可使用相关命令
jedis.auth("abcdefg");
//简单的key-value
存储
jedis.set("tn","thisuc");
System.out.println(jedis.get("tn"));
//在原有值得基础上添加,如若之前没有该key,则导入该key
//之前已经设定了redis对应"myredis",此句执行便会使redis对应"myredisyourredis"
jedis.append("redis","yourredis");
jedis.append("content","rabbit");
//mset
是设置多个key-value值 参数(key1,value1,key2,value2,...,keyn,valuen)
//mget
是获取多个key所对应的value值 参数(key1,key2,key3,...,keyn) 返回的是个
jedis.mset("name1","thisuc","name2","gaoxc","name3","lemon");
System.out.println(jedis.mget("name1","name2","name3"));
}
}