Following up on this discussion on ASP.NET:
http://rubyforge.org/pipermail/ironruby-core/2010-February/006225.html
We are considering hosting IronRuby in our application (ASP.NET
website & a couple windows services). I am looking for any guidance
around which objects should be cached, and what should be created on
the fly. The responses from the previous thread seemed to suggest it
is safe to cache the ScriptEngine. I’m wondering where else I might
want to cache, and where I would NOT want to cache (if an object is
not thread safe, and shouldn’t be used to fulfill multiple
simultaneous requests).
Specifically, we are using IronRuby to provide some customization to
the application. For (simplified) example, suppose we wanted to
provide the ability to customize a greeting. I want to store the
script that generates the greeting in the web.config (either the
script itself, or the path to the script).
My theory is that I can create a CompiledCode instance during
application startup, and then execute the compiled code with different
scopes for each request (to provide request-specific data). Is this
the proper/recommended way of executing the same script with different
data within an application? Is it safe to called CompiledCode.Execute
from multiple threads simultaneously?
Sample code that shows what I plan to do:
// Perform all these steps in global.asax application_start
var scriptFromConfigFile = @“”“Hi #{ctx.first_name}
#{ctx.last_name}”“”; // read from web.config appSettings
var scriptEngine = IronRuby.Ruby.CreateEngine();
//GreetingScript is cached as a singleton so that it is available to
requests
var GreetingScript =
scriptEngine.CreateScriptSourceFromString(scriptFromConfigFile).Compile();
// during each individual web request, change the context object
passed to the script
var scriptScope = GreetingScript.Engine.CreateScope();
scriptScope.SetVariable(“ctx”, new {FirstName=“John”, LastName=“Doe”});
var greeting = GreetingScript.Execute(scriptScope);
Console.WriteLine(greeting);