IT序号网

Redis的Java客户端Jedis知识解答

flyfish 2021年06月13日 数据库 143 0

先决条件

  • jdk
  • idea/eclipse
  • Jedis所需要的jar包
    • Commons-pool-1.6.jar
    • Jedis-2.1.0.jar
  • Jedis常用操作
    • 测试连通性
package cn.lonsun.staticcenter.controller; 
 
import redis.clients.jedis.Jedis; 
 
public class RedisController{ 
    public static void main(String[] args) { 
        //连接本地jedis 
        Jedis jedis = new Jedis("127.0.0.1",6379); 
        //查看服务是否运行 打出pong表示成功 
        System.out.println("connection is ok==============>"+jedis.ping()); 
    } 
 
}
  • 5+1(一个key、五大数据类型)
 public static void main(String[] args)  
  { 
  
     Jedis jedis = new Jedis("127.0.0.1",6379); 
     //key 
     Set<String> keys = jedis.keys("*"); 
     for (Iterator iterator = keys.iterator(); iterator.hasNext();) { 
       String key = (String) iterator.next(); 
       System.out.println(key); 
     } 
     System.out.println("jedis.exists====>"+jedis.exists("k2")); 
     System.out.println(jedis.ttl("k1")); 
     //String 
     //jedis.append("k1","myreids"); 
     System.out.println(jedis.get("k1")); 
     jedis.set("k4","k4_redis"); 
     System.out.println("----------------------------------------"); 
     jedis.mset("str1","v1","str2","v2","str3","v3"); 
     System.out.println(jedis.mget("str1","str2","str3")); 
     //list 
     System.out.println("----------------------------------------"); 
     //jedis.lpush("mylist","v1","v2","v3","v4","v5"); 
     List<String> list = jedis.lrange("mylist",0,-1); 
     for (String element : list) { 
       System.out.println(element); 
     } 
     //set 
     jedis.sadd("orders","jd001"); 
     jedis.sadd("orders","jd002"); 
     jedis.sadd("orders","jd003"); 
     Set<String> set1 = jedis.smembers("orders"); 
     for (Iterator iterator = set1.iterator(); iterator.hasNext();) { 
       String string = (String) iterator.next(); 
       System.out.println(string); 
     } 
     jedis.srem("orders","jd002"); 
     System.out.println(jedis.smembers("orders").size()); 
     //hash 
     jedis.hset("hash1","userName","lisi"); 
     System.out.println(jedis.hget("hash1","userName")); 
     Map<String,String> map = new HashMap<String,String>(); 
     map.put("telphone","13811814763"); 
     map.put("address","atguigu"); 
     map.put("email","abc@163.com"); 
     jedis.hmset("hash2",map); 
     List<String> result = jedis.hmget("hash2", "telphone","email"); 
     for (String element : result) { 
       System.out.println(element); 
     } 
     //zset 
     jedis.zadd("zset01",60d,"v1"); 
     jedis.zadd("zset01",70d,"v2"); 
     jedis.zadd("zset01",80d,"v3"); 
     jedis.zadd("zset01",90d,"v4"); 
      
     Set<String> s1 = jedis.zrange("zset01",0,-1); 
     for (Iterator iterator = s1.iterator(); iterator.hasNext();) { 
       String string = (String) iterator.next(); 
       System.out.println(string); 
     } 
  
           
  }
  • 事务提交

    1.日常

     

 public static void main(String[] args) { 
        //连接本地jedis 
        Jedis jedis = new Jedis("127.0.0.1",6379); 
        //查看服务是否运行 打出pong表示成功 
        System.out.println("connection is ok==============>"+jedis.ping()); 
 
        //监控key,该操作关闭事务 
        /*3 
        jedis.watch("serialNum"); 
        jedis.set("serialNum","##########"); 
        jedis.unwatch();*/ 
        Transaction transaction = jedis.multi(); 
        Response<String> response = transaction.get("serialNum"); 
        transaction.set("serialNum","s002"); 
        response=transaction.get("serialNum"); 
        transaction.lpush("list3","a"); 
        transaction.lpush("list3","b"); 
        transaction.lpush("list3","c"); 
 
        transaction.exec(); 
        //2 transaction.discard(); 
        System.out.println("serialNum***********"+response.get()); 
 
    }

    2.加锁

    

public boolean transMethod() { 
        //连接本地jedis 
        Jedis jedis = new Jedis("127.0.0.1",6379); 
        //查看服务是否运行 打出pong表示成功 
        System.out.println("connection is ok==============>"+jedis.ping()); 
        int balace;//可用余额 
        int debt;//欠额 
        int amtToSubstract=10;//实刷额度 
 
        jedis.watch("balace"); 
        balace=Integer.parseInt(jedis.get("balace")); 
        if(balace<amtToSubstract){//额度不够 
            jedis.unwatch(); 
            System.out.println("modify"); 
            return false; 
        }else{ 
            System.out.println("***********transaction"); 
            Transaction transaction = jedis.multi(); 
            transaction.decrBy("balace",amtToSubstract); 
            transaction.incrBy("debt",amtToSubstract); 
            transaction.exec(); 
            balace = Integer.parseInt(jedis.get("balance")); 
            debt = Integer.parseInt(jedis.get("debt")); 
 
            System.out.println("*******" + balace); 
            System.out.println("*******" + debt); 
            return true; 
 
 
        }
  • 主从复制
public static void main(String[] args) throws InterruptedException  
  { 
     Jedis jedis_M = new Jedis("127.0.0.1",6379); 
     Jedis jedis_S = new Jedis("127.0.0.1",6380); 
      
     jedis_S.slaveof("127.0.0.1",6379); 
      
     jedis_M.set("k6","v6"); 
     Thread.sleep(500); 
     System.out.println(jedis_S.get("k6")); 
  }

发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Redis的复制(Master/Slave)知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。