This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021

To learn is to learn.

In the last article, we learned the concept of execute in RedisTemplate and understood the concept of execute.

Execute provides an implementation of the basic operation Redis method for other specific data type operation methods.

Today we are going to learn how to use the execute method in RedisTemplate, and provide the corresponding code implementation. Let’s learn together.

How do I use the execute method correctly

execute(RedisCallback<T> action)

public <T> T execute(RedisCallback<T> action)
Copy the code

This method requires an instance of the RedisCallback object passed in as the argument, which is actually a Redis callback method that connects successfully, and is typically called as follows.

Redis links to manipulate key values in the Redis database.

Code use:

redisTemplate.execute((connection) -> {
    return connection.del(new byte[][]{"redis-key"});
});
Copy the code

execute(RedisCallback<T> action, boolean exposeConnection)

public <T> T execute(RedisCallback<T> action, boolean exposeConnection)
Copy the code

As you can see, this method is the same as above, except that there is an extra Boolean parameter on exposeConnection, so what is this parameter? What does it do?

The exposeConnection parameter means whether to expose a Connection, and if true, the current connection object can be used in the callback function.

Code use:

redisTemplate.execute((connection) -> {
    return connection.del(new byte[][]{"redis-key"});
}, true);
Copy the code

execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline)

public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline)
Copy the code

If you look at the source code for these methods, you’ll see that this is the core of the first three execute methods, because the first two methods will still call this method, just with two more values.

Pipeline means whether or not to open a pipeline. Pipeline is a link that can hold certain data.

Code use:

redisTemplate.execute((connection) -> {
    return connection.del(new byte[][]{"redis-key"});
}, true, false);
Copy the code

execute(SessionCallback<T> session)

public <T> T execute(SessionCallback<T> session)
Copy the code

This method is different because the arguments are not the same as the first three. The previous callback was RedisCallback, and this method’s callback is the SessionCallback callback object.

Is a method that can pass in a SessionCallback callback object.

Pay attention to

It should be noted that although the execute method is a low-level method, it does not mean that it is necessary to use it. In daily development, it is better to use Redis method based on some specific data types encapsulated by the execute method, because the execute method is not as easy to use as other methods, but more prone to errors.

summary

RedisTemplate RedisTemplate RedisTemplate RedisTemplate RedisTemplate