The Java operation of Redis (Jedis) in Redis notes is well understood

  • Syntax: PEXPIRE key milliseconds

  • Function: this command is similar to the execute command, but it sets the lifetime of the key in milliseconds

    (exhibit in seconds)

  • Available versions: > = 2.6.0

  • Time complexity: O(1)

  • Return value: set successfully, return 1. key does not exist or setting failed, return 0

PEXPIREAT instruction

  • Syntax: pexpireat key milliseconds timestamp

  • Function: this command is similar to the Expat command, but it sets the expired unix timestamp of the key in milliseconds (expat in seconds)

  • Available versions: > = 2.6.0

  • Return value: returns 1 if the lifetime setting is successful. When the key does not exist or the lifetime cannot be set, 0 is returned.

    (see exhibit command for more information)

TTL instruction

  • Syntax: TTL key

  • Function: returns the TTL (time to live) of a given key in seconds.

  • Available versions: > = 1.0.0

  • Return value:

    When the key does not exist, - 2 is returned.

    When the key exists but the remaining lifetime is not set, - 1 is returned.

    Otherwise, the remaining lifetime of the key is returned in seconds.

  • Note: before redis 2.8, when the key does not exist or the remaining lifetime of the key is not set, the command returns - 1.

PTTL instruction

  • Syntax: PTTL key

  • Function: this command is similar to the TTL command, but it returns the remaining lifetime of the key in milliseconds

    (TTL in seconds)

  • Available versions: > = 2.6.0

  • Return value: returns - 2 when the key does not exist. When the key exists but the remaining lifetime is not set, - 1 is returned.

  • Otherwise, the remaining lifetime of the key is returned in milliseconds.

  • Note: before Redis 2.8, when the key does not exist or the remaining lifetime of the key is not set, the command returns - 1.

RANDOMKEY instruction

  • Syntax: RANDOMKEY

  • Function: randomly return (not delete) a key from the current database.

  • Available versions: > = 1.0.0

  • Return value: returns a key when the database is not empty. When the database is empty, nil is returned.

RENAME instruction

  • Syntax: RENAME key newkey

  • Function: change the name of the key to newkey.

    When the key is the same as newkey or the key does not exist, an error is returned.

    When the newkey already exists, the RENAME command overwrites the old value.

  • Available versions: > = 1.0.0

  • Return value: it will prompt OK when the name change is successful, and an error will be returned when the name change fails.

TYPE instruction

  • Syntax: TYPE key

  • Function: returns the type of value stored by key.

  • Available versions: > = 1.0.0

  • Return value:

    none: key does not exist

    string: string

    list: list

    set: set

    zset: ordered set

    Hash: hash table

public class TestKeys {

    private Jedis jedis;

    @Before

    public void before() {

    	// Connect the virtual machine with redis service enabled

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

    }

    @After

    public void after() {

        jedis.close();

    }

    @Test

    public void testKeys() {

        // Delete a key

        jedis.del("name");

        // Delete multiple key s

        jedis.del("name", "age");



        // Set a key

        jedis.set("age", "hello");



        // Determine whether a key exists

        Boolean name = jedis.exists("name");

        System.out.println(name);



        // Set a key timeout

        Long age = jedis.expire("age", 100);

        System.out.println(age);



        // Get a key timeout

        Long ttl = jedis.ttl("age");

        System.out.println(ttl);



        // Get a key randomly

        String s = jedis.randomKey();



        // Modify key name

        jedis.rename("age", "newage");



        // View the type of the corresponding value

        String type1 = jedis.type("name");

        System.out.println(type1);

        String type2 = jedis.type("maps");

        System.out.println(type2);

    }

} 

Operation String related API

===================================================================================

|Command description|

| — | — |

|Set | set a key/value|

|get | obtain the corresponding value according to the key|

|mset | set multiple key value s at a time|

|mget | get the value of multiple key s at one time|

|getset | get the value of the original key and set the new value at the same time|

|strlen | obtain the length of the corresponding key storage value|

|append | appends the value of the corresponding key|

|getrange index 0 starts intercepting the content of value|

|setex | set the validity period of a key (seconds)|

|psetex | set the validity period of a key (MS)|

|setnx | no operation, no addition|

|msetnx atomic operation (no operation as long as one exists) | multiple key s can be set at the same time. As long as one exists, it will not be saved|

|decr | perform - 1 operation of numeric type|

|decrby | perform subtraction according to the provided data|

|incr | perform + 1 operation of numeric type|

|incrby | add according to the provided data|

|incrbyfloat | add floating point numbers according to the provided data|

public class TestString {

    private Jedis jedis;

    @Before

    public void before() {

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

    }

    @After

    public void after() {

        jedis.close();

    }

    @Test

    public void testString() {

        jedis.set("name", "zhenyu"); // set



        String name = jedis.get("name"); // get

        System.out.println(name);



        jedis.mset("content", "good person", "address", "Suzhou"); // mset



        List<String> mget = jedis.mget("name", "content", "address"); // mget

        mget.forEach(v -> System.out.println("v = " + v));



        String set = jedis.getSet("name", "yusael");

        System.out.println(set);



        // ...

    }

} 

API related to operation List

=================================================================================

|Command description|

| — | — |

|lpush | add a value to the head of a key list|

|lpushx | is the same as lpush, but the key must exist|

|rpush | add a value to the end of a key list|

|rpushx | is the same as rpush, but the key must exist|

|lpop | returns and removes the first element to the left of the list|

|rpop | returns and removes the first element to the right of the list|

|lrange | get the elements in a subscript interval|

|llen | get the number of list elements|

|lset | set the value of a specified index (the index must exist)|

|lindex | gets an element at a specified index position|

|lrem | delete duplicate elements|

|ltrim | keep the elements in a specific interval in the list|

|linsert | insert a new element before and after an element|

public class TestList {

    private Jedis jedis;

    @Before

    public void before() {

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

    }

    @After

    public void after() {

        jedis.close();

    }

    @Test

    public void testList() {



        jedis.flushDB(); // Clear current database



        jedis.lpush("names1", "1", "2", "3", "4", "5"); // lpush

        jedis.rpush("names1", "a", "b", "c", "d", "e"); // rpush



        List<String> names1 = jedis.lrange("names1", 0, -1);

        names1.forEach(v -> System.out.println("name = " + v));



        String name = jedis.lpop("names1"); // lpop rpop

        System.out.println(name); // 5



        System.out.println(jedis.llen("names1")); // llen



        // Insert a world on the left of 2 in names1

        Long linsert = jedis.linsert("names1", BinaryClient.LIST_POSITION.BEFORE, "2", "world");// linsert

        System.out.println("linsert: " + linsert);

        names1 = jedis.lrange("names1", 0, -1);

        names1.forEach(v -> System.out.println("name = " + v));



        // ...

    }

} 

API related to operation Set

=================================================================================

|Command description|

| — | — |

|sadd | adding elements to a collection|

|smembers | shows that all elements in the collection are out of order|

|Scar | returns the number of elements in the collection|

|spop | randomly returns an element and deletes the element in the collection|

|smove | moving elements from one collection to another must be of the same type|

|srem | delete an element from the collection|

|sismember | judge whether a collection contains this element|

|srandmember | random return element|

|sdiff | remove the same elements contained in other sets in the first set|

|sinter | finding intersection|

|sunion summation set|

public class TestSet {

    private Jedis jedis;

    @Before

    public void before() {

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

    }

    @After

    public void after() {

        jedis.close();

    }

    @Test

    public void testSet() {

        jedis.sadd("names","zhangsan", "lisi");



        Set<String> names = jedis.smembers("names");

        names.forEach(v -> System.out.println("v = " + v));



        Boolean sismember = jedis.sismember("names", "zhenyu");

        System.out.println(sismember);

    }

} 

Operating ZSet related API s

=================================================================================

|Command description|

| — | — |

|zadd | add an ordered collection element|

|zcard | returns the number of elements in the set|

|zrange ascending zrevrange descending | returns elements in a range|

|zrangebyscore | find elements in a range according to scores|

|zrank | return to ranking|

|zrevrank | reverse ranking|

Tags: Java Database Redis Back-end Programmer

Posted on Fri, 03 Sep 2021 21:35:02 -0400 by rob