import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import junit.framework.TestCase; import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import redis.clients.jedis.Jedis; public class TestRedis extends TestCase {
private Jedis jedis;
@BeforeClass
public void setUp()throws Exception{
//连接redis服务器,192.168.0.100:6379
jedis = new Jedis("192.168.91.234", 6379);
//权限认证
//jedis.auth("admin");
} @AfterClass
public void tearDown(){ } @Test
public void testString() throws Exception{
//-----添加数据----------
jedis.set("name","xinxin"); //向key-->name中放入了value-->xinxin
jedis.set("name","yueer"); // 如果已经存在,覆盖掉原先的数据
jedis.setnx("name","yueyue"); // 如果已经存在,不覆盖掉原先的数据 jedis.setnx("xingming","zhangdongyue"); // 如果不存在则增加此键值对
Boolean xingIs = jedis.exists("xing"); // 判断key值是否存在
System.out.println("xing is exists : " + xingIs);
System.out.println(jedis.get("name"));
System.out.println(jedis.get("xingming")); 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","476777XXX");
jedis.incr("age"); //进行加1操作
System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"));
jedis.decr("age"); //进行减1操作
System.out.println("减1操作 : " + jedis.get("age")); jedis.flushDB(); //清空数据
System.out.println(jedis.get("xingming"));
//设置超时时间 单位 秒
jedis.setex("expirtime",1,"value");
System.out.println(jedis.get("expirtime"));
Thread.sleep(1200);
System.out.println(jedis.get("expirtime"));
} @Test
public void testList() throws Exception{
jedis.flushDB(); //清空数据
jedis.rpush("l_name","pan");
jedis.rpush("l_name","teng"); //右端插入
jedis.lpush("l_name","姓名:"); //左端插入
// 再取出所有数据jedis.lrange是按范围取出,
// 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有
List<String> values = jedis.lrange("l_name", 0, -1);
System.out.println(values);
System.out.println(values.get(0)); // 数组长度
System.out.println("数组长度: " + jedis.llen("l_name")); // 排序
jedis.rpush("l_num","4");
jedis.rpush("l_num","3");
jedis.rpush("l_num","5");
jedis.rpush("l_num","6");
System.out.println("排序: " + jedis.sort("l_num")); //只能对数字排序 // 修改列表中单个值
jedis.lset("l_name", 0, "name: ");
values = jedis.lrange("l_name", 0, -1);
System.out.println("修改列表中的值:" + values); // 获取列表指定下标的值
System.out.println("指定下标值: " + jedis.lindex("l_name", 0)); // 删除区间以外的数据
jedis.ltrim("l_name", 0, 1);
values = jedis.lrange("l_name", 0, -1);
System.out.println("删除区间以外的资源: " + values);
// 列表出栈
//System.out.println(jedis.lpop("l_name")); //弹出左端的值
System.out.println(jedis.rpop("l_name")); //弹出右端的值
values = jedis.lrange("l_name", 0, -1);
System.out.println("列表出栈: " + values); //删除指定元素
jedis.rpush("l_elements","pan");
jedis.rpush("l_elements","pan");
jedis.rpush("l_elements","zzz");
jedis.rpush("l_elements","zzz");
jedis.lrem("l_elements",0,"pan"); //删除list中所有的值为pan的元素 第二个参数表示删除多少个,0表示所有
values = jedis.lrange("l_elements", 0, -1);
System.out.println("删除元素: " + values);
} @Test
public void testSet() throws Exception{
jedis.flushDB(); //清空数据
jedis.sadd("myset", "1");
jedis.sadd("myset", "2");
jedis.sadd("myset", "3");
jedis.sadd("myset", "4"); Set<String> setValues = jedis.smembers("myset");
System.out.println(setValues); // 移除noname
jedis.srem("myset", "4");
System.out.println("删除Set 元素:" + jedis.smembers("myset"));
//判断2是否存在于 set中
System.out.println(jedis.sismember("myset", "4"));
//遍历所有元素
Set<String> redisSet = jedis.smembers("myset");
Iterator it = redisSet.iterator();
System.out.println("遍历map");
while(it.hasNext()){
System.out.println(it.next());
}
//集合求 交并差
Jedis jedis2;
jedis2 = new Jedis("192.168.91.234", 6379);
jedis2.sadd("myset2","2");
jedis2.sadd("myset2","3");
jedis2.sadd("myset2","6");
jedis2.sadd("myset2","7"); System.out.println("集合求交集:" + jedis.sinter("myset","myset2"));
System.out.println("集合求并集:" + jedis.sunion("myset","myset2"));
System.out.println("集合求差集myset - myset2: " + jedis.sdiff("myset","myset2"));
System.out.println("集合求差集myset2 - myset: " + jedis.sdiff("myset2","myset")); }
@Test
public void testSortSet(){
jedis.flushDB();
jedis.zadd("sort_set",1,"first");
jedis.zadd("sort_set",4,"fourth");
jedis.zadd("sort_set",2,"second");
jedis.zadd("sort_set",3,"third");
jedis.zadd("sort_set",5,"fifth");
jedis.zadd("sort_set",6,"sixth");
jedis.zadd("sort_set",7,"seventh");
jedis.zadd("sort_set",8,"eighth");
jedis.zadd("sort_set",8,"eighth2");
jedis.zadd("sort_set",9,"nineth");
jedis.zadd("sort_set",0,"zero"); System.out.println("有序集合: " + jedis.zrange("sort_set",0,-1));
//删除集合中的元素
jedis.zrem("sort_set","third");
System.out.println("有序集合删除元素: " + jedis.zrange("sort_set",0,-1));
//统计有多少元素
System.out.println("有序集合元素个数: " + jedis.zcard("sort_set"));
//取某一范围内的元素
System.out.println("根据起始结束索引获取元素: " + jedis.zrange("sort_set",0,1)); //起始索引 结束索引
//统计权重范围内的元素个数
System.out.println("统计权重范围内的元素个数: " + jedis.zcount("sort_set",7,8));
//查看元素的权重
System.out.println("查看元素的权重: " + jedis.zscore("sort_set","eighth"));
//
}
@Test
public void testHashMap(){
jedis.flushDB();
Map <String,String> map = new HashMap<String,String>();
map.put("name", "panteng");
map.put("pwd", "123456");
map.put("age", "24"); jedis.hmset("user", map);
jedis.hset("user", "address","BeiJing_HaiDian"); List<String> list = jedis.hmget("user", "name","pwd","address"); System.out.println("name=" + list.get(0));
System.out.println("password=" + list.get(1));
System.out.println("address=" + list.get(2));
//删除键值对
jedis.hdel("user", "pwd");
//获取所有key
System.out.println("获取hashs中所有的key:"+jedis.hkeys("user"));
System.out.println("获取hashs中所有的value:"+jedis.hvals("user"));
}
}
jeis API:
1、设置key的超时时间 expire(String key, int seconds)
| expireAt(String key, long unixTime)
2、清空所有key flushAll() 清空所有数据库中的key,flushDB()
清空当前数据库的key
3、判断hash中的一个key是否存在 hexists(String key, String field)
4、获取哈希表中所有的键值对 hgetAll(String key)
返回值: Map<String,String>
5、将一个key从当前库移动到另一个库 move(String key, int dbIndex)
6、给key重新命名 rename(String oldkey, String newkey)
7、给key重新命名 renamenx(String oldkey, String newkey)
如果新的key已经存在,那么就会命名失败
8、切换数据库 select(int index)
redis多实例:
启动第二个redis实例,将redis.conf 复制一份,修改里面的端口号,然后 使用这个配置文件启动redis : src/redis-server redis2.conf
客户端操作情况:
@Test
public void testManyCase(){
Jedis jedis_1 = new Jedis("192.168.91.234", 6379);
Jedis jedis_2 = new Jedis("192.168.91.234", 6379);
jedis_1.auth("123456");
jedis_2.auth("123456"); jedis_1.set("jedis","jedis_1");
jedis_2.set("jedis","jedis_2"); //会覆盖jedis_1写的值
System.out.println("jedis_1获取jedis:" + jedis_1.get("jedis"));
System.out.println("jedis_2获取jedis:" + jedis_2.get("jedis")); //两个获取的是一样的,即存在多线程问题 //连接新的redis实例 可以证实两实例互不影响。
Jedis jedis_3 = new Jedis("192.168.91.234", 6610);
jedis_3.auth("123456");
jedis_3.set("jedis","jedis_3");
System.out.println("jedis_3获取jedis:" + jedis_3.get("jedis")); //获取的是jedis_3
}
多示例测试
多数据库测试
@Test
public void testManyDB(){
Jedis jedis_1 = new Jedis("192.168.91.234", 6379);
Jedis jedis_2 = new Jedis("192.168.91.234", 6379);
jedis_1.auth("123456");
jedis_2.auth("123456");
jedis_1.select(0);
jedis_2.select(1); jedis_1.set("jedis","jedis_1");
jedis_2.set("jedis","jedis_2"); //不会覆盖jedis_1写的值
System.out.println("jedis_1获取jedis:" + jedis_1.get("jedis"));
System.out.println("jedis_2获取jedis:" + jedis_2.get("jedis")); //两个获取的是不一样的 jedis.auth("123456");
jedis.set("jedis","jedis_0");
System.out.println("jedis获取jedis:" + jedis.get("jedis"));
System.out.println("jedis_1获取jedis:" + jedis_1.get("jedis")); //变为了0 说明默认选取的是0库
}
多数据库测试
发布与订阅:
import redis.clients.jedis.JedisPubSub; /**
*
* @author panteng
*
*/
public class Suber extends JedisPubSub { /**
* 当有人在订阅的频道发布消息时,触发此函数
* arg0 - 订阅的频道名(即key值)
* arg1 - 发布者发布的消息
*/
@Override
public void onMessage(String arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println("============onMessage==========" + "channel:" + arg0 + " msg:" + arg1); } @Override
public void onPMessage(String arg0, String arg1, String arg2) {
// TODO Auto-generated method stub
System.out.println("============onPMessage=========="); } @Override
public void onPSubscribe(String arg0, int arg1) {
// TODO Auto-generated method stub
System.out.println("============onPSubscribe==========");
} /**
* 取消订阅的时候 执行此函数
*/
@Override
public void onPUnsubscribe(String arg0, int arg1) {
// TODO Auto-generated method stub
System.out.println("============onPUnsubscribe==========");
} /**
* 当该订阅者订阅频道时,执行此函数
* arg0 - 订阅的频道名(即key值)
*/
@Override
public void onSubscribe(String arg0, int arg1) {
// TODO Auto-generated method stub
System.out.println("============onSubscribe==========" + "arg0" + arg0 + " arg1:" + arg1);
} @Override
public void onUnsubscribe(String arg0, int arg1) {
// TODO Auto-generated method stub
System.out.println("============onUnsubscribe==========");
} }
Suber.class
@Test
public void testPubSub(){
jedis.auth("123456");
//客户端订阅频道 会被阻塞,合适释放???
jedis.subscribe(suber, "successCount","allCount"); // 可以同时订阅多个频道
System.out.println("====已取消订阅===");
}
@Test
public void testPubSubAssit(){
jedis.auth("123456");
jedis.set("successCount","100");
jedis.publish("successCount","this is msg1");
jedis.publish("successCount","this is msg2");
//取消订阅
jedis.psubscribe(suber, "successCount","allCount"); // 可以同时订阅多个频道
jedis.publish("successCount","this is a msg3"); }
test