How to use the execute method in redistemplate

Time:2022-5-24

theme: vue-pro

This is the first day of my participation in the update challenge in November. For details:2021 last challenge

Only by reviewing the past can we know the new (review)

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

Execute provides the implementation of the redis method for basic operation, and provides the implementation of the basic 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 to use execute method correctly

execute(RedisCallback\

action)

public <T> T execute(RedisCallback<T> action)

When this method needs the parameters passed in, a rediscallback object instance is actually a callback method with successful redis connection. It is generally called in the following ways.

Operate the key values in the redis database through the redis link.

Code usage:

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

execute(RedisCallback\

action, boolean exposeConnection)

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

We can see that the method of expoconnection has the same parameter type, so what can we do with the above parameter? What does it do?

The exposeconnection parameter means whether to expose the connection. If it is true, the current connection object can be used in the callback function.

Code usage:

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

execute(RedisCallback\

action, boolean exposeConnection, boolean pipeline)

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

If you look at the source code of these methods, you will find that the core of the first three execute methods is actually this, because the first two methods will still call this method, but there are only two more parameter values.

As we have already said, pipeline means whether to open the pipeline. The pipeline is a link that can carry certain data.

Code usage:

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

execute(SessionCallback\

session)

public <T> T execute(SessionCallback<T> session)

This method is different because the parameters are different from the first three parameters. The previous callback function is rediscallback, and the callback of this method is the sessioncallback callback object.

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

be careful

It should be noted here that although the execute method is the underlying method, it does not mean that it must be used. In our daily development, we’d better use the redis method based on some specific data types encapsulated by the execute method, because the execute method is not as simple as other methods, but more prone to errors.

Summary

Today, I learned how to use several overloaded methods related to the execute of redistemplate. I don’t know what you have learned?