public class Testredis {
private Jedis jedis;
@Before //Junit注解
public void setup() {
//连接redis服务器,192.168.0.100:6379
//jedis对象实例化两种方式
jedis = new Jedis("192.168.0.100", 6379);
jedis = RedisClient.getResource();
//权限认证
jedis.auth("admin");
}
/**
* redis存储字符串
*/
@Test
public void testString() {
//-----添加数据----------
jedis.flushDB();
jedis.set("name","annie");//向key-->name中放入了value-->
System.out.println(jedis.get("name")); //执行结果:xinxin
jedis.setnx("name", "bill"); //存在时不写入
System.out.println(jedis.get("name"));
jedis.append("name", " is my a girl"); //value的拼接
System.out.println(jedis.get("name"));
jedis.del("name"); //删除某个键
System.out.println(jedis.get("name"));
//设置过期时间,单位秒
jedis.setex("sql",2, "MySQL");
System.out.println(jedis.get("sql"));
try {
Thread.sleep(3000);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.get("sql"));
//设置多个键值对
jedis.mset("name","annie","age","22","tel","1517252**");
jedis.incr("age"); //进行加1操作
//mget返回list集合
System.out.println(jedis.mget("name","age","tel"));
}
/**
* jedis操作List
*/
@Test
public void testList(){
//先移除所有的内容
jedis.del("framework");
System.out.println(jedis.lrange("framework",0,-1));
//从framework头部存放三条数据
jedis.lpush("framework","spring");
jedis.lpush("framework","struts");
jedis.lpush("framework","hibernate");
//第一个是key,第二个是起始位置,第三个是结束位置,-1表示取得所有
System.out.println(jedis.lrange("framework",0,-1));
System.out.println(jedis.lrange("framework",0,2)); //包含2下标
//jedis.llen("framework"); //获取列表长度
jedis.del("framework");
//从framework尾部存放三条数据
jedis.rpush("framework","spring");
jedis.rpush("framework","struts");
jedis.rpush("framework","hibernate");
System.out.println(jedis.lrange("framework",0,-1));
//linsert在key 对应list的特定位置之前或之后添加字符串元素
//eg:linsert mylist3 before "world" "there"
//redis 127.0.0.1:6379> lrange mylist3 0 -1
1) "hello"
2) "there"
3) "world"
//获取列表长度
System.out.println(jedis.llen("sql"));
//排序
//System.out.println();
jedis.lset("sql", 0, "oracle");
//获取下标为某list下标为0的值
System.out.println(jedis.lindex("sql", 0));
//删除数据
//lrem 从key 对应 list 中删除count个和value相同的元素
//eg: lrem mylist5 2 "hello"
//count>0:按从头到尾的顺序删除 count<0从尾到头;count=0:全部删除
System.out.println(jedis.lrem("sql", 1,"MongoDB"));
System.out.println(jedis.lindex("sql", 1));
jedis.rpush("sql", "s");
//删除区间以外的
System.out.println(jedis.ltrim("sql", 0, 1));
System.out.println(jedis.rpush("sql", "mongodb"));
//判断是否存在,存在则弹栈
System.out.println(jedis.rpushx("sql", "mongodb"));
//弹出尾元素
jedis.rpop("sql");
//弹出头元素
jedis.lpop("sql");
//尾部进栈
jedis.rpush("sql", "mysql");
//头部进栈
jedis.lpush("sql", "linput");
}
/**
* jedis操作Set
*/
@Test
public void testSet(){
jedis.sadd("sql", "mysql");
jedis.sadd("sql", "redis");
jedis.sadd("sql", "mongodb");
jedis.sadd("sql", "oracle");
Setsql=jedis.smembers("sql");
System.out.println(sql);
//移除指定元素
jedis.srem("sql", "redis");
//输出全部set的值
Setsqls=jedis.smembers("sql");
System.out.println(sqls);
//判断set是否存在
System.out.println(jedis.sismember("sql", "mysql"));
//返回集合元素总数
System.out.println(jedis.scard("sql"));
jedis.sadd("sql2", "mysql");
jedis.sadd("sql2", "redis");
jedis.sadd("sql2", "sql");
//交集
System.out.println(jedis.sinter("sql","sql2"));
//并集
System.out.println(jedis.sunion("sql","sql2"));
//差集
System.out.println(jedis.sdiff("sql2","sql"));
//差集
System.out.println(jedis.sdiff("sql","sql2"));
}
public void testSortedSet(){
Jedis jedis =RedisClient.getResource();
jedis.flushDB();
jedis.zadd("sql", 22, "redis");
jedis.zadd("sql", 23, "mysql");
jedis.zadd("sql", 2, "mongodb");
jedis.zadd("sql", 1, "oracle");
//从小到大
System.out.println(jedis.zrange("sql", 0, -1));
//从大到小
System.out.println(jedis.zrevrange("sql", 0, -1));
//统计所有的个数
System.out.println(jedis.zcard("sql"));
//查询元素的下标
System.out.println(jedis.zscore("sql", "11"));
//统计10-30下标的元素个数
System.out.println(jedis.zcount("sql", 10, 30));
}
/**
* redis操作Map
*/
@Test
public void testMap() {
jedis.flushDB();
Map user=new HashMap();
user.put("name", "小明");
user.put("age", "10");
user.put("gender", "man");
user.put("like", "basketball");
jedis.hmset("user", user);
//获取key为name的元素
System.out.println(jedis.hmget("user", "name"));
//统计元素个数
System.out.println(jedis.hlen("user"));
//是否存在
System.out.println(jedis.exists("user"));
//获取所有的key
System.out.println(jedis.hkeys("user"));
//获取所有的value
System.out.println(jedis.hvals("user"));
for(Map.Entry user1:user.entrySet()){
System.out.println(user1.getKey()+":"+user1.getValue());
}
//获取map中符合key的值
System.out.println(jedis.hmget("user", "name","age","gender"));
//删除指定的key
System.out.println(jedis.hdel("user", "name"));
//判断指定的key是否存在
System.out.println(jedis.hexists("user", "name"));
//某值增加如果不存在创建一个新的key-value
System.out.println(jedis.hincrBy("user", "key", 123456));
System.out.println(jedis.hincrBy("user", "key", 22));
//如果map的key存在不更新value并返回0
System.out.println(jedis.hsetnx("user", "name", "123"));
//另一种遍历方式
Iterator iter=jedis.hkeys("user").iterator();
while (iter.hasNext()){
String key = iter.next();
System.out.println(key+":"+jedis.hmget("user",key));
}
}
}
文章标题:Jedis常用操作总结笔记
文章分享:
http://chengdu.cdxwcx.cn/article/ihsphp.html