From 4630de10d10085b9cd32c5a00e1618e5da3db884 Mon Sep 17 00:00:00 2001 From: Jennifer Hickey Date: Mon, 5 Aug 2013 16:07:14 -0700 Subject: [PATCH] Update scripting doc --- .../docbook/reference/redis-scripting.xml | 70 +++++++++++++++---- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/docs/src/reference/docbook/reference/redis-scripting.xml b/docs/src/reference/docbook/reference/redis-scripting.xml index 5155063b7..8a5c02e12 100644 --- a/docs/src/reference/docbook/reference/redis-scripting.xml +++ b/docs/src/reference/docbook/reference/redis-scripting.xml @@ -2,24 +2,68 @@
Redis Scripting Redis versions 2.6 and higher provide support for execution of Lua scripts through the eval and - evalsha commands. These operations as well as other script management operations are available on the - RedisConnection implementations. + evalsha commands. Spring Data Redis provides a high-level abstraction for script execution + that handles serialization and automatically makes use of the Redis script cache. - Future SDR versions may offer support for these scripting commands on a higher-level API, such as RedisTemplate. + Scripts can be run through the execute methods of RedisTemplate. RedisTemplate uses a configurable + ScriptExecutor to execute the provided script. By default, the ScriptExecutor takes care of + serializing the provided keys and arguments and deserializing the script result. This is done with the RedisTemplate key and value serializers. + There is an additional execute method that allows you to pass custom serializers for the script arguments and result. - Here's an example script execution: + The default ScriptExecutor optimizes performance by retrieving the SHA1 of the script and attempting first to + run evalsha, falling back to eval if the script is not yet present in the Redis script cache. - () { - public Long doInRedis(RedisConnection connection) throws DataAccessException { - return connection.eval("return 10", ReturnType.INTEGER, 0); + Here's an example that executes a common "check-and-set" scenario using a Lua script. This is an ideal use case for a Redis script, as + it requires that we execute a set of commands atomically and the behavior of one command is influenced by the result of another. + + + + + + + + + ... +]]> + + script; + + public boolean checkAndSet(String expectedValue, String newValue) { + return redisTemplate.execute(script, Collections.singletonList("key"), + expectedValue, newValue); } -});]]> +}]]> - The return values of eval and evalsha can be assigned to any type. However, they should - match the expected ReturnType passed to these methods. In the above example, we passed ReturnType.Integer, therefore - the return type of eval was a Long. Similarly, using ReturnType.VALUE will yield a byte[]. See the ReturnType javadoc - for details. + +-- checkandset.lua +local current = redis.call('GET', KEYS[1]) +if current == ARGV[1] +then + redis.call('SET', KEYS[1], ARGV[2]) + return true +end +return false + + + The XML above configures a DefaultRedisScript pointing to a file called checkandset.lua, which is + expected to return a boolean value. The script resultType should be one of Long, Boolean, List, or deserialized value type. It can also be null if the + script returns a throw-away status (i.e "OK"). It is ideal to configure a single instance of DefaultRedisScript in your application context to avoid + re-calcuation of the script's SHA1 on every script execution. + + + The checkAndSet method above then executes the configured RedisScript with the provided key and arguments and returns the result. + + Scripts can be executed within a SessionCallback as part of a transaction or pipeline. + See and for more information. + + The scripting support provided by Spring Data Redis also allows you to schedule Redis scripts for periodic execution using the Spring Task + and Scheduler abstractions. See the Spring Framework documentation for more details.
\ No newline at end of file