java中使用redis学习

写一点小屁程序,熟悉下java中使用redis.其中要使用的redis的Jar文件,jedis-2.1.0-sources.jar和jedis-2.5.2.jar,前者是源码,可以不用,但是为了看到源码,还是加进去了。微笑。跑程序前需要启动redis,本人下载的是windows版本的redis,至于怎么启动安装redis,这里就不介绍了,网上很多资料可以参考。代码如下:

package com.redis;


import java.util.Iterator;
import java.util.List;
import java.util.Set;


import org.junit.Test;


import redis.clients.jedis.Jedis;


public class RedisTest {


private static Jedis jedis = null;
static {
if(jedis == null) {
jedis = new Jedis("localhost");
System.out.println("Connection ok");
}
else {
System.out.println("still be there~");
}
}

@Test
public void fun1() {
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server successfully!");
System.out.println("Sever is running:" + jedis.ping());
}

@Test
public void fun2() {
Jedis jedis = new Jedis("127.0.0.1");
System.out.println("Connection to server successfully!");
jedis.set("huadian", "ajax");
// jedis.sadd("salesorder", new String [] {"a", "b", "c", "d"});
System.out.println("Stored String in redis: " + jedis.get("huadian"));
}

@Test
public void fun3() {
jedis.lpush("uisf", "Redis");
jedis.lpush("uisf", "Mongodb");
jedis.lpush("uisf", "Mysql");
jedis.lpush("uisf", "Oracle");
List<String> list = jedis.lrange("uisf", 0, 5);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored String in redis::" + list.get(i));

}

@Test
public void fun4() {
Set<String> s =  jedis.keys("*");
Iterator<String> it = s.iterator();
while(it.hasNext()) {
System.out.println("Stored values ::" + it.next());
}


}

展开阅读全文

没有更多推荐了,返回首页