Sunday 9 July 2017

Ecmascript 6 Modules in Javascript

Javascript is a programming (or scripting) language known for its wide use. It is mature by the fact that it is used for many years since its creation in the mid 90s. But at the same time Javascript or just JS is immature by the fact that it lacks a lot of features built into the language. This is an observed by the large number of different libraries to add features to JS that programmers using other programming languages take for granted. For example, modules and classes are not something easy to create with JS.
The good thing is that JS is finally evolving in large steps now with Ecmascript 6 or ES6 with a common standard that many browsers vendors can agree upon. There are different ways to run JS code with ES6 features. One can use a transpiler such as Babel that will rewrite the JS code with ES6 scripts into to compatible ES2015 syntax which more browsers support. Or one can use a javascript library that contains polyfills and fills to support ES6 code in browsers. The sample code in this article has been tested with Internet Explorer 11 and is available as a Plunk. The following url contains the running demo:
Plunk with ES6 modules

First in this sample a class called Employee is created. This uses ES6 new class feature.

        export class Employee {

 
 
 constructor (name) {
  //console.log("Constructor of Employee");
  this._name = name; 
 }

 get name() {
  return this._name;
 }

 doWork(){
  return `${this.name} is working`;
 }
}

ES6 classes can export a class and then be imported in other classes and build up modules. The employee class also uses ES6 template string feature. Note that the backtick quote is used and ${..} is used to refer to variables. The next class then imports the Employee class. Note that you must change url here to match your Plunk in the running editor to adjust the session url that Plunkr uses to give unique urls. ES6 can support static urls of course.

import {Employee} from "http://run.plnkr.co/gIljKiTbjZ0IOX8b/Employee.js"
export class Company {


 hire(...names) {
  this.employees = names.map(n => new Employee(n));
 }

 doWork(){
  console.log(this.employees);
  $("body").append("<ul>");
  for (let e of this.employees){
   console.log(e);
   $("body").append(`<li style='list-style-type:circle'>${e.doWork()}` + "</li>");
  }
  $("body").append(">/ul<");
  return 1;
 }
}

Again the class keyword is used to define a class and then exported to be used in another class or module with the export keyword. To import the Employee class the import keyword. The Company class above uses the rest operator (...) to allow passing in an arbitrary number of elements. Then the map operator built into Arrays in ES6 is used to return a mapped array. The use of the let operator is used and also the of operator of iterable collections.
Finally the sample HTML code below is used to define the use of an ES6 module. Here the script tag with the tag module is used.


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
        <script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>
        <script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        
    </head>
    <body bgcolor="teal" style="font-family:Verdana, Courier,Helvetica, Arial">
        
        <h1 style="color:yellow;font-family:Arial">ES6 module demo</h1>
        <script type="module">
        import {Company} from "http://localhost/babeldemo/src/Company.js"
        var c = new Company();
        c.hire("Tim", "Tom", "Betty", "Maggie");
        c.doWork();
        </script>
        
    </body>
</html>

Note that Traceur is a good option to test out ES6 features. In production, using a transpiler such as Babel to create a ES2015 compatible JS code is probably much better since there is a performance cost with Traceur. The reason Traceur is used in this sample is to show how an old browser such as Internet Explorer 11 can run ES6 code with a Javascript library such as Traceur.

To see a compatibility matrix of which browsers supports which ES6, see the Kangax table at the following url:
https://kangax.github.io/compat-table/es6/
With the help of Traceur more browsers can run more feature of ES6 and you can test out and use ES6 in your development or production environments and structure your code with classes and modules for example. Make note that the current state of Javascript module loaders of browsers will be improved in the future. Until then, additional frameworks such as AMD or Require.Js can be used to support module loading in more complex scenarios.

Sunday 14 May 2017

Redis Image Cache Provider

Caching images in web sites and web applications is possible to achieve using many methods. This article presents an image cache provider to use in MVC applications using Redis as a caching server.

First off, create a new MVC web application and add the Nuget package RedisImageCacher (created by me):
RedisImageCacher

Install-Package RedisImageCacher

This Nuget package will also add in the dependencies for libraries ServiceStack.Redis and Microsoft.Web.RedisOutputCacheProvider.


Next off, one will need a Redis server. It is possible to install a local Redis server in Windows using the binaries here:

Redis 2.6 Windows (32/64 bits) on GitHub from MSOpenTech.


Tips on how to use Redis client and server and code in this article is written in another article of mine:
ASP.NET and Redis

Download Redis 2.6 and start it up on your local developer box. Start the server from a command-line for example :

C:\redis26\redis-server

Start a Redis client also from the commandline e.g. :
C:\redis26\redis-cli



  <caching>
      <outputCache defaultProvider="MyRedisOutputCache">
        <providers>
          <!-- Either use 'connectionString' and provide all parameters as string OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
          <!-- 'databaseId' and 'applicationName' can be used with both options. -->
          <!--
          <add name="MyRedisOutputCache" 
            host = "127.0.0.1" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            databaseId = "0" [number]
            applicationName = "" [String]
            connectionTimeoutInMilliseconds = "5000" [number]
            operationTimeoutInMilliseconds = "1000" [number]
            connectionString = "<Valid StackExchange.Redis connection string>" [String]
            loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
            loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
          />
          -->
          <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->
          <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
          <!-- 'databaseId' and 'applicationName' can be used with both options. -->
          <!--
          <add name="MyRedisOutputCache" 
            host = "127.0.0.1" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            databaseId = "0" [number]
            applicationName = "" [String]
            connectionTimeoutInMilliseconds = "5000" [number]
            operationTimeoutInMilliseconds = "1000" [number]
            connectionString = "<Valid StackExchange.Redis connection string>" [String]
            settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String]
            settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String]
            loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
            loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
            redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
          /> -->
          <add name="MyRedisOutputCache" type="Microsoft.Web.Redis.RedisOutputCacheProvider" host="localhost" accessKey="" applicationName="RedisCache" port="6379" ssl="false" />
        </providers>
      </outputCache>
    </caching>
    



It is also possible to control how long time Image Data is to be kept in the cache in seconds. In web.config:
  <add key="RedisImageCacheTimeout" value="10" />



This sets a timeout of caching image data to 10 seconds for example. It is possible to change this of course, using 600 will cache image data for 10 minutes for example.


To cache an image, the following razor code show an example of its use:
 <img src="@Url.Action("ShowImage", "Images", new { id = Url.Encode("Croatia6-jpg") })" width="500" />
Using the syntax "filename"-"extension" like "myfile1-jpg" and using the embedded controller ImageController and action method ShowImage will also cache the image to Redis. A parameter of id is provided to the file to cache and load or retrieve from cache directly. This will be taken care of by the embedded images controller in the library. It is possible to use different image file formats, such as JPG, GIF, PNG and BMP.


Inside redis-cli Redis Client we can see that loading the image in the example will add a cache item in Redis:
redis 127.0.0.1:6379> keys * 1) "RedisCache/IMAGEBANK/Content/Images/Croatia6.jpg"

Note that images must be put into the Content/Images folder of your MVC solution! A configuration of changing this can be added later to the library. We can also ask Redis how long the cache item will exist until it expires and is removed by using TTL (Time-To-Live):

redis 127.0.0.1:6379> ttl "RedisCache/IMAGEBANK/Content/Images/Croatia6.jpg" (integer) 2


Note that TTL will show -1 if the cache item is expired. Redis client got many other handful commands to control the Redis memory cache, see Redis documentation here: Redis documentation The way the RedisImageCacher is saving data is actually done very generic and can be extended to support other data and cache this. For implementation details, see: ASP.NET and Redis


To download the source code for the RedisImageCacher and Demo library a download Mercurial repository is available on Bitbucket here:

RedisImageCacher Bitbucket repo


Or if you have Mercurial (hg) installed, from a command-line issue:
hg clone https://bitbucket.org/toreaurstad/redisimagecacher

Friday 14 April 2017

AsyncStateMachine in .NET

This article will present a custom AsyncStateMachine that shows how it is possible to create as async awaitable state machine manually. Let's first consider an easy async-await example and implement it using a custom async state machine instead of the default one in .NET. The purpose is to glance more into the inner workings of async. The following code is what is going to be implemented.

   class Program
    {
        static void Main(string[] args)
        {
            try
            {
                CallFooAsync();
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(string.Join(",",
                    ae.InnerExceptions.ToList().Select(e => e.Message).ToArray()));
            }

            Console.WriteLine("Hit any key to continue..");
            Console.ReadKey();
        }

        public static async void CallFooAsync()
        {
            int foo = await FooAsync();
            Console.WriteLine(foo);
        }

        static async Task<int> FooAsync()
        {
            await Task.Delay(5000);
            return 42;
        }


The Main method of the console application is not allowed to have async modifier, so it was necessary to introduce an extra method here. Next off, here is an example of an AsyncStateMachine at its simplest form. It is implemented as a struct.

        struct FooAsyncStateMachine : IAsyncStateMachine
        {

            private int state; 

            public AsyncTaskMethodBuilder<int> methodBuilder;
            private TaskAwaiter awaiter;

            public void MoveNext()
            {
                //State machine
                try
                {
                    if (state == 0)
                    {
                        awaiter = Task.Delay(millisecondsDelay: 5000).GetAwaiter();
                        if (awaiter.IsCompleted)
                        {
                            state = 1;
                            goto state1;
                        }
                        else
                        {
                            state = 1;
                            methodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
                        }
                        return;
                    }

                    state1:
                    if (state == 1)
                    {                     
                        methodBuilder.SetResult(42);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    methodBuilder.SetException(ex);
                    return;
                }
            }

            public void SetStateMachine(IAsyncStateMachine stateMachine)
            {
                methodBuilder.SetStateMachine(stateMachine);
            }

        }

The IAsyncStateMachine interface has a method called void MoveNext(). This method is called when the async method starts up. This method can be called multiple times. It contains a state variable to control which step is next. The AsyncMethodBuilder is used to control the result returned from the async method. The method SetResult sets the result returned and the method SetException inside a catch sets an exception, if encountered. Thet method SetStateMachine sets the statemachine struct as the state machine struct itself and is also part of the IAsyncStateMachine. Now that the AsyncStateMachine is defined, it is time to kick it off.

 static Task FooAsync2()
        {
            var stateMachine = new FooAsyncStateMachine();
            stateMachine.methodBuilder = AsyncTaskMethodBuilder<int>.Create();
            stateMachine.methodBuilder.Start(ref stateMachine);
            return stateMachine.methodBuilder.Task;
        }

The AsyncMethodBuilder inside the IAsyncStateMachine is using the Create method and Start method and returns a Task. In this article the async state machine is hard coded and not made general. Of course in .NET the AsyncStateMachine used is far more complex. But creating an async state machine yourself is a nice introduction to taking a deeper look into what async really is. Async-await is rewriting your code and could have been implemented in earlier versions of .NET. Async-await not changes the functionality of your code, but the way it is executed. It provides a way to halt a method and free up the thread, for example the UI thread and run code in the background, then continue later on again. It also allow parallel code.

Resources: