I'm using the Redis client from ServiceStack. I have a Lua script that fills up a Lua table with results from several Redis calls. I want to return this table in some way. My thought was to use the method ExecLuaShaAsList from the client lib and in the lua script just do a "return myTable". It doesnt work, I always get an empty list back.
How to return the lua table to the redis client?
This the C# script I use with the Redis client:
using (var redisClient = GetPooledRedisClient())
{
var sha1 = redisClient.LoadLuaScript(luaBody);
List<string> theList = redisClient.ExecLuaShaAsList(sha1);
int listLength = theList.Count(); //listLength is always 0 for some reason
}
UPDATE AFTER TIPS FROM BELOW ANSWER
This is how LuaBody is created:
private string GetLuaScript(List<CatalogItem> categories, List<CatalogItem> products)
{
string categoriesToAggregate = string.Join("\",\"", categories.Select(c=>c.Name));
categoriesToAggregate = "\"" + categoriesToAggregate + "\"";
string csSearchResult = string.Join("\",\"", products.Select(c => c.Name));
csSearchResult = "\"" + csSearchResult + "\"";
StringBuilder sb = new StringBuilder();
sb.AppendLine("local categoriesToAggregate = {").Append(categoriesToAggregate).Append("} ");
sb.AppendLine("local csSearchResult = {").Append(csSearchResult).Append("} ");
sb.AppendLine("local result = {} ");
sb.AppendLine();
sb.AppendLine("for i=1,#categoriesToAggregate do ");
sb.AppendLine(" result[categoriesToAggregate[i]] = 0 ");
sb.AppendLine();
sb.AppendLine(" for j=1,#csSearchResult do ");
sb.AppendLine(" local fieldValue = redis.call('hget', 'asr:'..csSearchResult[j], categoriesToAggregate[i]) ");
sb.AppendLine(" if fieldValue then ");
sb.AppendLine(" result[categoriesToAggregate[i]] = result[categoriesToAggregate[i]] + fieldValue ");
sb.AppendLine(" end ");
sb.AppendLine(" end ");
sb.AppendLine("end ");
sb.AppendLine();
sb.AppendLine("return cjson.encode(result) ");
return sb.ToString();
}