redis - java 基本操作
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");
}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"));@AfterClass </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> tearDown(){ } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testString() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">-----添加数据---------- </span> jedis.set("name","xinxin"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向key-->name中放入了value-->xinxin </span> jedis.set("name","yueer"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果已经存在,覆盖掉原先的数据</span> jedis.setnx("name","yueyue"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果已经存在,不覆盖掉原先的数据</span>
jedis.append(</span>"name", " is my lover"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">拼接</span> System.out.println(jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">)); jedis.del(</span>"name"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除某个键</span> System.out.println(jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置多个键值对</span> jedis.mset("name","liuling","age","23","qq","476777XXX"<span style="color: rgba(0, 0, 0, 1)">); jedis.incr(</span>"age"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">进行加1操作</span> System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"<span style="color: rgba(0, 0, 0, 1)">)); jedis.decr(</span>"age"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">进行减1操作</span> System.out.println("减1操作 : " + jedis.get("age"<span style="color: rgba(0, 0, 0, 1)">)); jedis.flushDB(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清空数据</span> System.out.println(jedis.get("xingming"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置超时时间 单位 秒</span> jedis.setex("expirtime",1,"value"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"expirtime"<span style="color: rgba(0, 0, 0, 1)">)); Thread.sleep(</span>1200<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"expirtime"<span style="color: rgba(0, 0, 0, 1)">)); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testList() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{ jedis.flushDB(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清空数据</span> jedis.rpush("l_name","pan"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_name","teng"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">右端插入</span> jedis.lpush("l_name","姓名:"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">左端插入 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 再取出所有数据jedis.lrange是按范围取出, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 </span> List<String> values = jedis.lrange("l_name", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(values); System.out.println(values.get(</span>0<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数组长度 </span> System.out.println("数组长度: " + jedis.llen("l_name"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 排序</span> jedis.rpush("l_num","4"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_num","3"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_num","5"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_num","6"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"排序: " + jedis.sort("l_num")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">只能对数字排序 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改列表中单个值 </span> jedis.lset("l_name", 0, "name: "<span style="color: rgba(0, 0, 0, 1)">); values </span>= jedis.lrange("l_name", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"修改列表中的值:" +<span style="color: rgba(0, 0, 0, 1)"> values); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取列表指定下标的值 </span> System.out.println("指定下标值: " + jedis.lindex("l_name", 0<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除区间以外的数据 </span> jedis.ltrim("l_name", 0, 1<span style="color: rgba(0, 0, 0, 1)">); values </span>= jedis.lrange("l_name", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"删除区间以外的资源: " +<span style="color: rgba(0, 0, 0, 1)"> values); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 列表出栈 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println(jedis.lpop("l_name")); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">弹出左端的值</span> System.out.println(jedis.rpop("l_name")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">弹出右端的值</span> values = jedis.lrange("l_name", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"列表出栈: " +<span style="color: rgba(0, 0, 0, 1)"> values); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除指定元素</span> jedis.rpush("l_elements","pan"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_elements","pan"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_elements","zzz"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"l_elements","zzz"<span style="color: rgba(0, 0, 0, 1)">); jedis.lrem(</span>"l_elements",0,"pan"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除list中所有的值为pan的元素 第二个参数表示删除多少个,0表示所有</span> values = jedis.lrange("l_elements", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"删除元素: " +<span style="color: rgba(0, 0, 0, 1)"> values); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testSet() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{ jedis.flushDB(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清空数据</span> jedis.sadd("myset", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "2"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "3"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "4"<span style="color: rgba(0, 0, 0, 1)">); Set</span><String> setValues = jedis.smembers("myset"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(setValues); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除noname </span> jedis.srem("myset", "4"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"删除Set 元素:" + jedis.smembers("myset"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断2是否存在于 set中</span> System.out.println(jedis.sismember("myset", "4"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">遍历所有元素</span> Set<String> redisSet = jedis.smembers("myset"<span style="color: rgba(0, 0, 0, 1)">); Iterator it </span>=<span style="color: rgba(0, 0, 0, 1)"> redisSet.iterator(); System.out.println(</span>"遍历map"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){ System.out.println(it.next()); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">集合求 交并差</span>
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(</span>"集合求交集:" + jedis.sinter("myset","myset2"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"集合求并集:" + jedis.sunion("myset","myset2"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"集合求差集myset - myset2: " + jedis.sdiff("myset","myset2"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"集合求差集myset2 - myset: " + jedis.sdiff("myset2","myset"<span style="color: rgba(0, 0, 0, 1)">)); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSortSet(){ jedis.flushDB(); jedis.zadd(</span>"sort_set",1,"first"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",4,"fourth"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",2,"second"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",3,"third"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",5,"fifth"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",6,"sixth"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",7,"seventh"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",8,"eighth"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",8,"eighth2"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",9,"nineth"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"sort_set",0,"zero"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"有序集合: " + jedis.zrange("sort_set",0,-1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除集合中的元素</span> jedis.zrem("sort_set","third"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"有序集合删除元素: " + jedis.zrange("sort_set",0,-1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">统计有多少元素</span> System.out.println("有序集合元素个数: " + jedis.zcard("sort_set"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取某一范围内的元素</span> System.out.println("根据起始结束索引获取元素: " + jedis.zrange("sort_set",0,1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">起始索引 结束索引 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">统计权重范围内的元素个数</span> System.out.println("统计权重范围内的元素个数: " + jedis.zcount("sort_set",7,8<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查看元素的权重</span> System.out.println("查看元素的权重: " + jedis.zscore("sort_set","eighth"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//
}
@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(</span>"user"<span style="color: rgba(0, 0, 0, 1)">, map); jedis.hset(</span>"user", "address","BeiJing_HaiDian"<span style="color: rgba(0, 0, 0, 1)">); List</span><String> list = jedis.hmget("user", "name","pwd","address"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"name=" + list.get(0<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"password=" + list.get(1<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"address=" + list.get(2<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除键值对</span> jedis.hdel("user", "pwd"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有key</span> System.out.println("获取hashs中所有的key:"+jedis.hkeys("user"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"获取hashs中所有的value:"+jedis.hvals("user"<span style="color: rgba(0, 0, 0, 1)">)); }
}
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(</span>"jedis","jedis_1"<span style="color: rgba(0, 0, 0, 1)">); jedis_2.set(</span>"jedis","jedis_2"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">会覆盖jedis_1写的值</span> System.out.println("jedis_1获取jedis:" + jedis_1.get("jedis"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"jedis_2获取jedis:" + jedis_2.get("jedis")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">两个获取的是一样的,即存在多线程问题 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接新的redis实例 可以证实两实例互不影响。</span> Jedis jedis_3 = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("192.168.91.234", 6610<span style="color: rgba(0, 0, 0, 1)">); jedis_3.auth(</span>"123456"<span style="color: rgba(0, 0, 0, 1)">); jedis_3.set(</span>"jedis","jedis_3"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"jedis_3获取jedis:" + jedis_3.get("jedis")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取的是jedis_3</span> }</pre>
多数据库测试
1 @Test 2 public void testManyDB(){ 3 Jedis jedis_1 = new Jedis("192.168.91.234", 6379); 4 Jedis jedis_2 = new Jedis("192.168.91.234", 6379); 5 jedis_1.auth("123456"); 6 jedis_2.auth("123456"); 7 jedis_1.select(0); 8 jedis_2.select(1); 9 10 jedis_1.set("jedis","jedis_1"); 11 jedis_2.set("jedis","jedis_2"); //不会覆盖 jedis_1 写的值 12 System.out.println("jedis_1 获取 jedis:" + jedis_1.get("jedis")); 13 System.out.println("jedis_2 获取 jedis:" + jedis_2.get("jedis")); //两个获取的是不一样的 14 15 jedis.auth("123456"); 16 jedis.set("jedis","jedis_0"); 17 System.out.println("jedis 获取 jedis:" + jedis.get("jedis")); 18 System.out.println("jedis_1 获取 jedis:" + jedis_1.get("jedis")); //变为了 0 说明默认选取的是 0 库 19 }
发布与订阅:
1 import redis.clients.jedis.JedisPubSub; 2 3 /** 4 * 5 * @author panteng 6 * 7 */ 8 public class Suber extends JedisPubSub { 9 10 /** 11 * 当有人在订阅的频道发布消息时,触发此函数 12 * arg0 - 订阅的频道名(即 key 值) 13 * arg1 - 发布者发布的消息 14 */ 15 @Override 16 public void onMessage(String arg0, String arg1) { 17 // TODO Auto-generated method stub 18 System.out.println("============onMessage==========" + "channel:" + arg0 + "msg:" + arg1); 19 20 } 21 22 @Override 23 public void onPMessage(String arg0, String arg1, String arg2) { 24 // TODO Auto-generated method stub 25 System.out.println("============onPMessage=========="); 26 27 } 28 29 @Override 30 public void onPSubscribe(String arg0, int arg1) { 31 // TODO Auto-generated method stub 32 System.out.println("============onPSubscribe=========="); 33 } 34 35 /** 36 * 取消订阅的时候 执行此函数 37 */ 38 @Override 39 public void onPUnsubscribe(String arg0, int arg1) { 40 // TODO Auto-generated method stub 41 System.out.println("============onPUnsubscribe=========="); 42 } 43 44 /** 45 * 当该订阅者订阅频道时,执行此函数 46 * arg0 - 订阅的频道名(即 key 值) 47 */ 48 @Override 49 public void onSubscribe(String arg0, int arg1) { 50 // TODO Auto-generated method stub 51 System.out.println("============onSubscribe==========" + "arg0" + arg0 + "arg1:" + arg1); 52 } 53 54 @Override 55 public void onUnsubscribe(String arg0, int arg1) { 56 // TODO Auto-generated method stub 57 System.out.println("============onUnsubscribe=========="); 58 } 59 60 }
1 @Test 2 public void testPubSub(){ 3 jedis.auth("123456"); 4 //客户端订阅频道 会被阻塞,合适释放??? 5 jedis.subscribe(suber, "successCount","allCount"); // 可以同时订阅多个频道 6 System.out.println("==== 已取消订阅 ==="); 7 } 8 @Test 9 public void testPubSubAssit(){ 10 jedis.auth("123456"); 11 jedis.set("successCount","100"); 12 jedis.publish("successCount","this is msg1"); 13 jedis.publish("successCount","this is msg2"); 14 //取消订阅 15 jedis.psubscribe(suber, "successCount","allCount"); // 可以同时订阅多个频道 16 jedis.publish("successCount","this is a msg3"); 17 18 19 }