Skip to content

theolivenbaum/RedSharper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RedSharper

RedSharper allows you to write C# code that will execute directly on the Redis server.

It does that by transpiling the C# code to Lua.

var res = await client.Execute((cursor, argv, keys) =>
            {
                var count = cursor.Get(keys[0]).AsInt();
                var toAdd = (int) argv[0];

                for (var i = 0; i < count; i++)
                {
                    var key = keys[0] + "_" + i;
                    var currentValue = cursor.Get(key).AsLong() ?? 0;
                    cursor.Set(key, currentValue + toAdd);
                }

                return RedResult.Ok;
            }, new RedisValue[] {5}, new RedisKey[] {"countKey"});
local num = tonumber(redis.call('get', KEYS[1]));
local num2 = tonumber(ARGV[1]);
local i = 0;
while i<num do
    local key = tostring(KEYS[1].."_")..i;
    local num3 = (tonumber(redis.call('get', key)) or 0);
    redis.call('set', key, num3+num2)
    i = i+1
end
return { ok = 'OK' };

Why?

When we execute Lua scripts from C#, we lose a lot of the advanges that the C# compiler and the IDE offer, such as auto completion, compile-time error checking, debugging, and many more.

RedSharper aims to mitigate these issues.

TODO List

  • Add more Redis commands
  • Add ability to create and manipulate lists/dictionaries
  • Support more of C#'s syntax
    • ForEach
    • Switch/Case
    • Custom methods (?)
    • Custom types (structs) (?)
  • Some refactoring
  • Document
  • Write proper unit tests
  • Add debugging support

Source Guide

Demo

RedSharper.Demo is a demo project that utilizes the library.

C# Code Decompilation

RedSharper uses ILSpy to decompile compiled lambda function to a C# syntax tree.
See the CSharp folder.

RedIL

RedIL is an intermidiate language that is created from C# code, and later compiled to Lua, and potentially other targets in the future (See "Future Plans").

Lua Compilation

Lua is written by traversing the RedIL using an IRedILVisitor.
See the Lua folder.

Dependencies

Future Plans

  • Transpiling C# Code directly to C code that will run as a Redis module.

Disclaimer

This library is still at it's early stage of development and not meant to be production ready yet. Use at your own risk.

About

Redis Scripting in Native C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.9%
  • Lua 0.1%