Update pipeline and tx documentation
This commit is contained in:
43
docs/src/reference/docbook/reference/pipelining.xml
Normal file
43
docs/src/reference/docbook/reference/pipelining.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<section xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="pipeline">
|
||||
<title>Pipelining</title>
|
||||
<para>Redis provides support for <ulink url="http://redis.io/topics/pipelining">pipelining</ulink>, which involves sending multiple
|
||||
commands to the server without waiting for the replies and then reading the replies in a single step. Pipelining can improve performance
|
||||
when you need to send several commands in a row, such as adding many elements to the same List.</para>
|
||||
|
||||
<para>Spring Data Redis provides several <classname>RedisTemplate</classname> methods for executing commands in a pipeline. If you don't
|
||||
care about the results of the pipelined operations, you can use the standard <literal>execute</literal> method, passing <literal>true</literal>
|
||||
for the <literal>pipeline</literal> argument. The <literal>executePipelined</literal> methods will execute the provided <interfacename>RedisCallback</interfacename> or
|
||||
<interfacename>SessionCallback</interfacename> in a pipeline and return the results. For example:
|
||||
</para>
|
||||
|
||||
<programlisting language="java"><![CDATA[ //pop a specified number of items from a queue
|
||||
List<Object> results = stringRedisTemplate.executePipelined(new RedisCallback<Object>() {
|
||||
public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
|
||||
for(int i=0; i< batchSize; i++) {
|
||||
stringRedisConn.rPop("myqueue");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});]]></programlisting>
|
||||
|
||||
<para>The example above executes a bulk right pop of items from a queue in a pipeline. The <literal>results</literal> List contains all of the popped items.
|
||||
<classname>RedisTemplate</classname> uses its value, hash key, and hash value serializers to deserialize all results before returning, so the returned items in
|
||||
the above example will be Strings. There are additional <literal>executePipelined</literal> methods that allow you to pass a custom serializer for pipelined results.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Note that the value returned from the <interfacename>RedisCallback</interfacename> is required to be null, as this value is discarded in favor
|
||||
of returning the results of the pipelined commands.
|
||||
</para>
|
||||
|
||||
<note>An important change has been made to the <literal>closePipeline</literal> method of <interfacename>RedisConnection</interfacename> in version 1.1.
|
||||
Previously this method returned the results of pipelined operations directly from the connectors. This means that the data types often differed from those
|
||||
returned by the methods of <interfacename>RedisConnection</interfacename>. For example, <literal>zAdd</literal> returns a boolean indicating
|
||||
that the element has been added to the sorted set. Most connectors return this value as a long and Spring Data Redis performs the conversion. Another common
|
||||
difference is that most connectors return a status reply (usually the String "OK") for operations like <literal>set</literal>. These replies are typically discarded by Spring Data Redis.
|
||||
Prior to 1.1, these conversions were not performed on the results of <literal>closePipeline</literal>. If this change breaks your application, you can
|
||||
set <literal>convertPipelineAndTxResults</literal> to false on your <interfacename>RedisConnectionFactory</interfacename> to disable this behavior.
|
||||
</note>
|
||||
</section>
|
||||
@@ -11,12 +11,28 @@
|
||||
</para>
|
||||
|
||||
<programlisting language="java"><![CDATA[ //execute a transaction
|
||||
redisTemplate.execute(new SessionCallback<Object>() {
|
||||
public Object execute(RedisOperations operations) throws DataAccessException {
|
||||
List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
|
||||
public List<Object> execute(RedisOperations operations) throws DataAccessException {
|
||||
operations.multi();
|
||||
operations.opsForValue().set("key", "value");
|
||||
List<Object> results = operations.exec();
|
||||
return null;
|
||||
operations.opsForSet().add("key", "value1");
|
||||
// This will contain the results of all ops in the transaction
|
||||
return operations.exec();
|
||||
}
|
||||
});]]></programlisting>
|
||||
});
|
||||
System.out.println("Number of items added to set: " + txResults.get(0));
|
||||
]]></programlisting>
|
||||
|
||||
<para><classname>RedisTemplate</classname> will use its value, hash key, and hash value serializers to deserialize all results of <literal>exec</literal>
|
||||
before returning. There is an additional <literal>exec</literal> method that allows you to pass a custom serializer for transaction results.
|
||||
</para>
|
||||
|
||||
<note>An important change has been made to the <literal>exec</literal> methods of <interfacename>RedisConnection</interfacename> and <classname>RedisTemplate</classname>
|
||||
in version 1.1. Previously these methods returned the results of transactions directly from the connectors. This means that the data types often differed from those
|
||||
returned from the methods of <interfacename>RedisConnection</interfacename>. For example, <literal>zAdd</literal> returns a boolean indicating
|
||||
that the element has been added to the sorted set. Most connectors return this value as a long and Spring Data Redis performs the conversion. Another common
|
||||
difference is that most connectors return a status reply (usually the String "OK") for operations like <literal>set</literal>. These replies are typically discarded by Spring Data Redis.
|
||||
Prior to 1.1, these conversions were not performed on the results of <literal>exec</literal>. Also, results were not deserialized in <classname>RedisTemplate</classname>, so they
|
||||
often included raw byte arrays. If this change breaks your application, you can
|
||||
set <literal>convertPipelineAndTxResults</literal> to false on your <interfacename>RedisConnectionFactory</interfacename> to disable this behavior.
|
||||
</note>
|
||||
</section>
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<section id="redis:requirements">
|
||||
<title>Redis Requirements</title>
|
||||
<para>Spring Redis requires Redis 2.2 or above and Java SE 6.0 or above .
|
||||
<para>Spring Redis requires Redis 2.4 or above and Java SE 6.0 or above .
|
||||
In terms of language bindings (or connectors), Spring Redis integrates with <ulink url="http://github.com/xetorthio/jedis">Jedis</ulink>,
|
||||
<ulink url="http://github.com/alphazero/jredis">JRedis</ulink>,
|
||||
<ulink url="http://github.com/spullara/redis-protocol">SRP</ulink> and
|
||||
@@ -399,6 +399,8 @@
|
||||
|
||||
<xi:include href="redis-transactions.xml"/>
|
||||
|
||||
<xi:include href="pipelining.xml"/>
|
||||
|
||||
<xi:include href="redis-scripting.xml"/>
|
||||
|
||||
<section id="redis:support">
|
||||
|
||||
Reference in New Issue
Block a user