java中的redis常用操作

MLXY 2020-01-21

常规操作

public class TestReidsCommon {

?

    public static void main(String[] args) {

        // 创建连接

        Jedis jedis = new Jedis("192.168.120.129", 6379);

        // 设置密码 如果没有密码可以不设置

        jedis.auth("123456");

        // key

        Set<String> keys = jedis.keys("*");

        for (Iterator<String> 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<String> 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", "15902738715");

        map.put("address", "whsxt");

        map.put("email", "");

        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<String> iterator = s1.iterator(); iterator.hasNext();) {

            String string = (String) iterator.next();

            System.out.println(string);

        }

        jedis.close();

    }

}

2.3,事务提交

常规操作

 

public class TestReidsTranaction {

?

    public static void main(String[] args) {

        // 创建连接

        Jedis jedis = new Jedis("192.168.120.129", 6379);

        // 设置密码 如果没有密码可以不设置

        jedis.auth("123456");

         //监控key,如果该动了事务就被放弃

         /*3

         jedis.watch("serialNum");

         jedis.set("serialNum","s#####################");

         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());

        jedis.close();

?

    }

?

}

?

加锁操作

 

public class TestReidsTranactionLock {

?

    public boolean transMethod() {

        // 创建连接

        Jedis jedis = new Jedis("192.168.120.129", 6379);

        // 设置密码 如果没有密码可以不设置

        jedis.auth("123456");

        int balance;// 可用余额

        int debt;// 欠额

        int amtToSubtract = 10;// 实刷额度

?

        jedis.watch("balance");

        // jedis.set("balance","5");//此句不该出现,讲课方便。模拟其他程序已经修改了该条目

        balance = Integer.parseInt(jedis.get("balance"));

        if (balance < amtToSubtract) {

            jedis.unwatch();

            System.out.println("modify");

            jedis.close();

            return false;

        } else {

            System.out.println("***********transaction");

            Transaction transaction = jedis.multi();

            transaction.decrBy("balance", amtToSubtract);

            transaction.incrBy("debt", amtToSubtract);

            transaction.exec();

            balance = Integer.parseInt(jedis.get("balance"));

            debt = Integer.parseInt(jedis.get("debt"));

            System.out.println("*******" + balance);

            System.out.println("*******" + debt);

            jedis.close();

            return true;

        }

    }

?

    /**

     * 通俗点讲,watch命令就是标记一个键,如果标记了一个键, 在提交事务前如果该键被别人修改过,那事务就会失败,这种情况通常可以在程序中 重新再尝试一次。

     * 首先标记了键balance,然后检查余额是否足够,不足就取消标记,并不做扣减; 足够的话,就启动事务进行更新操作,

     * 如果在此期间键balance被其它人修改, 那在提交事务(执行exec)时就会报错, 程序中通常可以捕获这类错误再重新执行一次,直到成功。

     */

    public static void main(String[] args) {

        TestReidsTranactionLock test = new TestReidsTranactionLock();

        boolean retValue = test.transMethod();

        System.out.println("main retValue-------: " + retValue);

    }

?

}

 
 2.4,主从复制

 

public class TestReidsMS {

?

    public static void main(String[] args) throws InterruptedException {

        // 创建连接

        Jedis jedis_M = new Jedis("192.168.120.129", 6379);

        Jedis jedis_S = new Jedis("192.168.120.129", 6380);

?

        jedis_S.slaveof("127.0.0.1", 6379);

?

        jedis_M.set("k6", "v6");

?

        Thread.sleep(500);

        System.out.println(jedis_S.get("k6"));

        jedis_M.close();

        jedis_S.close();

    }

?

}

相关推荐