当前位置:网站首页>Basic use cases for jedis

Basic use cases for jedis

2022-06-10 16:31:00 € € - flying - ~ £

Jedis yes java Use in Redis A large class of commands , Each method in its class is associated with Redis Corresponding to the command of , The following cases help us understand Java In the operation Redis cache .

package com.wxl;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.Set;

public class TestPing {
    public static void main(String[] args) {
        normalOperation();
        System.out.println();
        transactionOperation();

    }

    /*
     The transaction operations 
     */
    private static void transactionOperation() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        jedis.flushAll();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello", "world");
        jsonObject.put("name", "Jack");
        // Open transaction 
        Transaction multi = jedis.multi();
        String result = jsonObject.toJSONString();

        try {
            multi.set("user1", result);
            multi.set("user2", result);

            multi.exec();// Perform transactions 
        } catch (Exception e) {
            multi.discard();// Give up the business 
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));
            jedis.close();
        }
    }

    private static void normalOperation() {
        //1、new Jedis  Object can 
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //jedis  all redis Command method corresponding to class 
        System.out.println(jedis.ping());

        // Common command tests 
        System.out.println(" Empty data " + jedis.flushDB());
        System.out.println(" Determine whether a key exists " + jedis.exists("username"));
        System.out.println(" New user name " + jedis.set("username", "Jack"));
        System.out.println(" New password " + jedis.set("password", "123456"));
        System.out.println(" All keys in the system are as follows :");
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);

        System.out.println(" Delete key password" + jedis.del("password"));
        System.out.println(" Judgment key password Whether there is " + jedis.exists("password"));
        System.out.println(" View key username The type of value stored :" + jedis.type("username"));
        System.out.println(" Random return key One of the spaces " + jedis.randomKey());
        System.out.println(" rename key:" + jedis.rename("username", "name"));
        System.out.println(" Take out the modified name:" + jedis.get("name"));
        System.out.println(" Search by index :" + jedis.select(0));
        System.out.println(" Delete all... In the currently selected database key:" + jedis.flushDB());
        System.out.println(" Return to the current database key Number of :" + jedis.flushDB());
        System.out.println(" Returns the key Number of :" + jedis.dbSize());
        System.out.println(" Delete all in all databases key:" + jedis.flushAll());

        jedis.close();// Close links 
    }
}

The operation effect is as follows :

原网站

版权声明
本文为[€ € - flying - ~ £]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021014150460.html