因为前一段时间看过redis的一些介绍,当时觉得人家讲的很好,我就标记了下,没想到一天浏览器换了,东西丢了,现在想找都找不到了,无语中,决定记录一下
redis教程: http://www.redis.net.cn/tutorial/3511.html
首先为什么要用redis: http://www.2cto.com/database/201701/552665.html
redis是一个开源的使用c编写、支持网络、可基于内存支持可持久化的日志型、key-value数据库,提供多语言的API接口,通常用来作为分布式系统的缓存服务。
redis的五种数据类型: http://blog.csdn.net/wang379275614/article/details/47172469
这是人家的图片哈,hash是唯一一个二维的.
每种类型常用的数据命令: http://www.cnblogs.com/liuconglin/p/5847568.html
java api中例子: 参考自:http://www.cnblogs.com/liuling/p/2014-4-19-04.html
记得把自己的server端启动起来哈
junit用的是4.1的版本,刚开始的3.7的版本@Before是没有的
package redis;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class TestMap {
private Jedis jedis;
@Before
public void setup(){
jedis = new Jedis("127.0.0.1");
System.out.println("Connection to server sucessfully");
}
@Test
public void testString(){
jedis.set("name","xinxin");
System.out.println(jedis.get("name"));
jedis.append("name", " is my lover");
System.out.println(jedis.get("name"));
jedis.del("name");
System.out.println(jedis.get("name"));
jedis.mset("name","liuling","age","23","qq","1159");
jedis.incr("age");
System.out.println(jedis.get("name")+"-"+jedis.get("age")+"-"+jedis.get("qq"));
}
@Test
public void testMap(){
Map<String,String> map = new HashMap<String,String>();
map.put("name", "shilanlan");
map.put("age", "18");
map.put("qq", "188");
jedis.hmset("user", map);
List<String> rsmap = jedis.hmget("user","name","age","qq");
System.out.println(rsmap);
jedis.hdel("user", "age");
System.out.println(jedis.hmget("user", "age"));
System.out.println(jedis.hlen("user"));
System.out.println(jedis.exists("user"));
System.out.println(jedis.hkeys("user"));
System.out.println(jedis.hvals("user"));
Iterator<String> iter=jedis.hkeys("user").iterator();
while(iter.hasNext()){
String key=iter.next();
System.out.println(key+":"+jedis.hmget("user", key));
}
}
@Test
public void testSet(){
jedis.sadd("users", "xiaoma");
jedis.sadd("users", "xinxin");
jedis.sadd("users", "xiaohong");
jedis.sadd("users", "xinming");
jedis.sadd("users", "xinzhang");
jedis.sadd("users", "who");
jedis.srem("users", "who");
System.out.println(jedis.smembers("users"));
System.out.println(jedis.sismember("users", "who"));
System.out.println(jedis.srandmember("users"));
System.out.println(jedis.scard("users"));
}
@Test
public void testList(){
jedis.del("java framework");
System.out.println(jedis.lrange("java framework", 0, -1));
jedis.lpush("java framework", "spring");
jedis.lpush("java framework", "struts");
jedis.lpush("java framework", "hibernate");
System.out.println(jedis.lrange("java framework", 0, -1));
jedis.del("java framework");
jedis.lpush("java framework", "春天");
jedis.lpush("java framework", "struts");
jedis.lpush("java framework", "hibernate");
System.out.println(jedis.lrange("java framework", 0, -1));
}
}