Skip to content

Commit 12e6114

Browse files
committed
Merge pull request #214 from kevin-montrose/scripting-docs
Update documentation for Scripting and Profiling
2 parents e1a31d9 + 5ac2faf commit 12e6114

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Docs/Scripting.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Scripting
2+
===
3+
4+
Basic [Lua scripting](http://redis.io/commands/EVAL) is supported by the `IServer.ScriptLoad(Async)`, `IServer.ScriptExists(Async)`, `IServer.ScriptExists(Async)`, `IDatabase.ScriptEvaluate`, and `IDatabaseAsync.ScriptEvaluateAsync` methods.
5+
These methods expose the basic commands neccessary to submit and execute Lua scripts to redis.
6+
7+
More sophisticated scripting is available through the `LuaScript` class. The `LuaScript` class makes it simpler to prepare and submit parameters along with a script, as well as allowing you to use
8+
cleaner variables names.
9+
10+
An example use of the `LuaScript`:
11+
12+
```
13+
const string Script = "redis.call('set', @key, @value)";
14+
15+
using (ConnectionMultiplexer conn = /* init code */)
16+
{
17+
var db = conn.GetDatabase(0);
18+
19+
var prepared = LuaScript.Prepare(Script);
20+
db.ScriptEvaluate(prepared, new { key = (RedisKey)"mykey", value = 123 });
21+
}
22+
```
23+
24+
The `LuaScript` class rewrites variables in scripts of the form `@myVar` into the appropriate `ARGV[someIndex]` required by redis. If the
25+
parameter passed is of type `RedisKey` it will be sent as part of the `KEYS` collection automatically.
26+
27+
Any object that exposes field or property members with the same name as @-prefixed variables in the Lua script can be used as a paramter hash to
28+
`Evaluate` calls. Supported member types are the following:
29+
30+
- int(?)
31+
- long(?)
32+
- double(?)
33+
- string
34+
- byte[]
35+
- bool(?)
36+
- RedisKey
37+
- RedisValue
38+
39+
40+
To avoid retransmitting the Lua script to redis each time it is evaluated, `LuaScript` objects can be converted into `LoadedLuaScript`s via `LuaScript.Load(IServer)`.
41+
`LoadedLuaScripts` are evaluated with the [`EVALSHA`](http://redis.io/commands/evalsha), and referred to by hash.
42+
43+
An example use of `LoadedLuaScript`:
44+
45+
```
46+
const string Script = "redis.call('set', @key, @value)";
47+
48+
using (ConnectionMultiplexer conn = /* init code */)
49+
{
50+
var db = conn.GetDatabase(0);
51+
var server = conn.GetServer(/* appropriate parameters*/);
52+
53+
var prepared = LuaScript.Prepare(Script);
54+
var loaded = prepared.Load(server);
55+
loaded.Evaluate(db, new { key = (RedisKey)"mykey", value = 123 });
56+
}
57+
```
58+
59+
All methods on both `LuaScript` and `LoadedLuaScript` have Async alternatives, and expose the actual script submitted to redis as the `ExecutableScript` property.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ Documentation
3838
- [Events](https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Events.md) - the events available for logging / information purposes
3939
- [Pub/Sub Message Order](https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/PubSubOrder.md) - advice on sequential and concurrent processing
4040
- [Where are `KEYS` / `SCAN` / `FLUSH*`?](https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/KeysScan.md) - how to use server-based commands
41+
- [Profiling](https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Profiling.md) - profiling interfaces, as well as how to profile in an `async` world
42+
- [Scripting](https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Scripting.md) - running Lua scripts with convenient named parameter replacement
4143

4244
Questions and Contributions
4345
---
4446

4547
If you think you have found a bug or have a feature request, please [report an issue][2], or if appropriate: submit a pull request. If you have a question, feel free to [contact me](https://github.com/mgravell).
4648

4749
[1]: http://msdn.microsoft.com/en-us/library/dd460717%28v=vs.110%29.aspx
48-
[2]: https://github.com/StackExchange/StackExchange.Redis/issues?state=open
50+
[2]: https://github.com/StackExchange/StackExchange.Redis/issues?state=open

0 commit comments

Comments
 (0)