Redis Scripts

The Redis Scripts provide connectivity with a Redis Server instance using the ServiceStack.Redis library. To enable, install the ServiceStack.Redis NuGet Package, then register a Redis Client Manager in the IOC:

container.AddSingleton<IRedisClientsManager>(() => new RedisManagerPool(connString));

Then to enable in your scripts register RedisScripts:

var context = new ScriptContext { 
    ScriptMethods = {
        new RedisScripts()
    }
}.Init();

Your scripts are now able to use the available Redis Scripts.

redisCall

This is your main interface into Redis which utilizes the Custom Commands API to be able to execute any arbitrary Redis command. It can either be called with a string similar to commands sent with redis-cli, e.g:

{{ 'GET foo' |> redisCall }}

This works for most Redis commands but uses internal heuristics to determine where to split the command into the multiple arguments that redis-server natively understands. A more deterministic usage which doesn't rely on any heuristics is to pass an array of arguments:

{{ ['GET', 'foo'] |> redisCall }}

redisCall returns either a single object for redis commands returning single values or a List or nested List of objects for Redis commands returning complex Responses.

redisInfo

Returns the Redis Server Info from Redis Info command in a String Dictionary.

redisSearchKeys

Provides an efficient API for searching Redis Keys by utilizing Redis's non-blocking SCAN command, to fetch results and a server-side LUA script to populate the results with metadata. Results are returned in a typed List<RedisSearchResult>:

public class RedisSearchResult
{
    public string Id { get; set; }
    public string Type { get; set; }
    public long Ttl { get; set; }
    public long Size { get; set; }
}

redisSearchKeysAsJson

If preferred, you can access the search results as JSON and parse it on the client instead.

redisConnection

Returns the current Connection Info in an Object Dictionary containing: {{ host, port, db }}

redisToConnectionString

Converts an redisConnection Object Dictionary into a Redis Connection String.

Redis App Examples

The Sharp Apps below utilize Redis Scripts for all their Redis functionality:

Redis Vue

redis.web-app.io is a generic Redis Database Viewer that provides a human-friendly view of any Redis data Type, including a dedicated UI to create and populate any Redis data type which just utilizes the above Redis Scripts and a single Vue index.html App to power the Redis UI:

.NET Core Redis Viewer

Redis HTML

redis-html.web-app.io is a version of Redis UI built using just #Script and Redis methods where all functionality is maintained in a single index.html weighing under <200 LOC including HTML and JavaScript. It's a good example of how the declarative style of programming that #Script encourages a highly-readable code-base that packs a lot of functionality in a tiny foot print.

Server Generated HTML

It's not immediately obvious when running this locally since both #Script and Redis are super fast, but Redis HTML was developed as a traditional Website where all HTML is server-generated and every search box key stroke and click on search results performs a full-page reload. There's a slight sub-second delay that causes a noticeable flicker when hosted on the Internet due to network lag, but otherwise server-generated #Script Websites can enable a highly responsive UI (especially in Intranets) with great SEO and deep-linking and back-button support working as expected without the complexity of adopting a client-side JavaScript SPA framework and build-system.

Run against your local Redis instance

Since redis is published Gist Desktop App, you can run the above redis SharpApp's against your local redis instance with:

app open redis

Using open always downloads and runs the latest version, if you don't have an Internet connection you can later run any previous run Sharp Apps using run:

app run redis

As both are Sharp Apps it doesn't need any compilation so after running the Redis Web App locally you can modify the source code and see changes in real-time thanks to its built-in Hot Reloading support.

Populate redis with the Northwind database

You can use the northwind-data project to populate a Redis instance with Northwind test data by running:

dotnet run redis localhost:6379

See the Scripts API Reference for the full list of Redis scripts available.

made with by ServiceStack