Taswar Bhatti
The synonyms of software simplicity
golang

Below is a VSCode with Golang video on mine on channel9 that I recorded at Channel 9 Studio, Redmond at Microsoft Campus (Seattle).
Its a short introduction of using VSCode with Go Lang and how to debug in go lang using VSCode.

Channel9 Video

Taswar @ Channel9 Studio


Hope you enjoy.
Taswar

Redis

Redis List Datatype are similar in C# world as LinkeList e.g LinkedList. Just like in LinkedList in C#, Redis can store items at Head or Tail of a List. The nice thing about them is that the insertion speed is just O(1), but do note that if you have a large list, the reading speed goes down since it sequentially traverses through the items (i.e walking down the chain). One could also say its an advantage since Redis was made to write faster than read, and LinkedList are great data type for such things. List are ideal candidate for logs, since the write speed is more important than read when dealing with logs.

Redis List Datatype – Operations

  • LPUSH: Prepends the values to the left of the list O(1).
  • RPUSH: Prepends the values to the right of the list O(1).
  • LPUSHX: Prepends the values to the left of the list if key exist O(1).
  • RPUSHX: Prepends the values to the right of the list if key exist O(1).
  • LINSERT: Inserts a value in the list after position calculated from left O(N).
  • LSET: Sets the value of an element in a list based on index O(N).
  • LRANGE: Gets the sub list of elements based on start and end position O(S+E).
  • LTRIM: Deletes the elements outside the range specified O(N), where N is the length.
  • RPOP: Removes the last element O(1).
  • LREM: Removes the element at the index point O(N) , where N is the length.
  • LPOP: Removes the first element of the list O(1).
  • LINDEX: Gets the element from the list based on the index O(N) where N is the length of traverse.
  • LLEN: This command gets the length of the list O(1).
  • RPOPLPUSH: Operates on two lists source and destination list, takes the last element on the source list and push it to the first element of the destination list O(1).

C# code using Redis List Datatype

So this covers the basic usage of Redish List Datatype, in the next blog post I will cover using Sets Datatype.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/5.RedisList/RedisList

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
Redis

Redis Hash Datatype are similar in C# world as Dictionary e.g Dictionary. Just like in C# redis stores map of attributes using key value pair. One thing to note is in Redis a Hash both the field name and the value are strings. Therefore, a Hash Datatype is a mapping of a string to a string.

Memory Optimized

Redis Hashes are memory-optimized. Their optimization is based on the hash-max-ziplist-entries and hash-max-ziplist-value configurations. Internally in Redis, a Hash can be a ziplist or a hash table. So what does it mean for a ziplist? Basically it is designed to be memory efficient dually linked list and integers are stored as real integers rather than a sequence of characters.

Speed

Something to note is a hashtable in Redis has a constant time lookup, the down side is it is not memory optimized, while the ziplist is memory optimized but the lookup time are not constant. To read more on Redis Memory Optimization please view Redis documentation.

So which one is used when?

  • The ziplist is by default when the number of fields do not exceed the configuration ones in hash-max-ziplist-entries
  • The hashtable is used when a the size or any of its values exceeds

Redis Hash Datatype – Operations

  • HSET: Sets the value of a field for a key O(1).
  • HGET: Gets the value of a field for a key O(1).
  • HLEN: Gets the number of fields for the key O(1).
  • HMGET: Gets the values for the fields for a key O(N), where N is the number of fields and O (1) if N is small.
  • HMSET: Sets multiple values for respective fields for a key O(N), where N is the number of fields and O (1) if N is small.
  • HGETALL: Gets all the values and fields for a key O(1).
  • HKEYS: Gets all the fields in the Hash for the key O(1).
  • HEXISTS: Checks for the existence of a field for a key O(1).
  • HVALS: Gets all the values in the Hash for the key O(N), where N is the number of fields and O(1) if N is small.
  • HSETNX: Sets the value against the field for the key provided the field does not exist O(1).
  • HDEL: Deletes the fields for a key O (N), where N is the number of fields and O(1) if N is small.
  • HINCRBY: Increments the value (provided the value is an integer) of a field for a key O(1).
  • HINCRBYFLOAT: Increments the value if value is a float of a field for a key O(1).

C# code using Redis Hash Datatype

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/4.RedisHashes

So this covers the basic usage of Redish Hash Datatype, in the next blog post I will cover using List Datatype.

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
Redis

Continuing on with our Redis for .NET Developer, the Redis String Datatype is also used to store integers, float and other utility commands to manipulate strings.

Redis String Datatype using Integers

In Redis float and integers are represented by string. Redis parses the string value as an integer and the neat thing about them is when you do operations on strings they are atomic. When there are multiple clients issuing INCR against the same key, it will never enter into a race condition.

  • INCR key : Increment the value by one O(1)
  • INCRBY key value: Increments the value by the given value (integer) O(1)
  • DECR key: Decrements the value by one O(1)
  • DECRBY key value: Decrements the value by the given value (integer) O(1)
  • APPEND key value: Concatenate the existing integer with the new integer O(1)
  • INCRBYFLOAT key value : Increment the float value by given value (float) O(1)

C# Code using integer and float

For source code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/3.RedisStringsAsInt

For our next blog post, I will cover more functionality and other datatype in Redis.

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
Redis

Continuing our series on Redis for .NET Developer, we will be looking into the basic string datatype that Redis provides. First to be clear, Redis is more than just a string cache or to store your serialized objects, it is a data structure server, but nevertheless the basic understanding of using strings in Redis will help us learn the foundation of using Redis.

Strings Data Type

String types are the basic data types in Redis. Strings in Redis can store integers, images, files, strings and serialized objects, since Redis strings are byte arrays that are binary safe and have a maximum size of 512MB.

Sets and Gets

The getters and setters are the basic commands that one can use to set or get values in Redis. For single key-value there are GET, SET, SETNX, GETSET

  • GET key: This key gets the value O(1)
  • SET key “value”: This key sets a value for a key O(1)
  • SETNX key “value”: If key does Not eXist this will sets a value against it O(1)
  • GETSET key “value”: Get the old value and sets a new value O(1)

For multi key-value there are MGET, MSET, MSETNX

  • MGET key1 key2: Gets all the corresponding values of keys O(N)
  • MSET key1 “value1” key2 “value2”: Atomic operation, so all given keys are set at once O(N)
  • MSETNX key1 “value1” key2 “value2”: Sets the given keys to their respective values if Not eXist O(N)

Example of using single key-value in C#

Below is an example of single key-value using C# StackExchange.Redis

Example of using multiple key-value in C#

Below is an example of multi key-value using C# StackExchange.Redis

Serialization of objects into Redis with C#

One can use Json.NET serialization of a C# object into Redis. There is also a StackExchange.Redis.Extensions Nuget package that allows one to serialize objects with protobuf, jil, etc. For more details please look at https://github.com/imperugo/StackExchange.Redis.Extensions.
In the example below we will Newton.Json to serialize a User C# object

For our next blog post, I will cover other functionality that the string datatype has in Redis, and also how Redis can treat strings as integer also.

For source code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/2.RedisString

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
Redis

In the second blog post series “Redis for .NET Developer” I will show how we will use C# to connect to Redis.
We will be using StackExchange.Redis library to connect to Redis. One can download StackExchange. Redis through NuGet.

StackExchange.Redis is a high performance general purpose redis client for .NET languages (C# etc).

1. Let’s Download our Nuget package, one can use the command line like below or use Visual Studio (I am using VS2015).

Search for redis in your nuget window.

Nuget Redis

Nuget Redis

Once installed you will see in your output windows.

Nuget Redis Result

Nuget Redis Result

Now that the nuget package is installed, we can build a C# console app that will connect to your redis server.
Below is a sample code to connect to localhost of your redis.

The above code will allow you to connect to Redis and store a string key “testKey” with a value of “testValue”.

For better modulation of Redis it is recommended to store ConnectionMultiplexer as a static singleton in your application.

Below is example of a RedisStore that stores the ConnectionMultiplexer as a static Lazy loaded singleton.

Now our previous code would something like

In my next blog post I will cover the data structures that Redis provides.

For code please visit https://github.com/taswar/RedisForNetDevelopers

For previous Redis topics

  1. Intro to Redis for .NET Developers
Redis

What is Redis

Redis (REmote DIctionay Server) is a very popular open-source, networked, in-memory, key-value data store, sometimes referred to as a data structure server which also comes with optional durability. Redis is well known for high performance, flexibility, provides a rich set of data structures, and a simple straightforward API. The programming language Redis is written in is ANSI C.

The development of Redis has been sponsored by Pivotal since May 2013; before that, it was sponsored by VMware. According to the monthly ranking by DB-Engines.com, Redis is the most popular key-value store. Redis officially runs on Linux and not officially supported on Windows but MS Open Tech has been working with the Redis community to build a production-ready Windows port of Redis, including 64-bit support, an installer for Windows Azure, NuGet support, and much more.

In this series of post I will introduce Redis and it basic functionality using C# and node.js sample code.

To start off lets try to get Redis running on our Windows machine first. We will need to download the MSI from MS Open Tech, as of today July 3rd 2015 the latest version provided by MSOpenTech is 2.8.21

Installation

Launch the msi and follow through the process of the install. Screenshot are attached below

If you wish to run the latest version 3.0.2 line of redis then you will need to install it on a linux system. The following commands shows how to install redis on a Linux System (Ubunutu)

Validating installation

Once installed on windows or linux we can use the redis-cli to verify if redis is running.
On windows launch your prompt and go to C:\Program Files\Redis
run redis-cli.exe

Run these commands, to set and get values

The end result should look something like this.

Redis Prompt

Redis Prompt

If you are having issues with connecting to redis, check out if redis is running, use the redis-server.exe to verify.

Summary

We have learned how to install redis in part 1 series of Redis for .NET Developers. in the upcoming post, I will go through the more details of redis.

idea

Dear Readers,

I am looking to write on some topics but have not decided on which one would be the best, I wanted your input on what you as reader would like to be covered. I have google survey below, please select one of the topics or if you like other topics to be covered please fill in the other section.

Thanks.
Taswar

Grunt

Sorry for not blogging for a while, have been busy with joining a new company. I wanted to finish up the blog post series with Running Grunt to test your JavaScript application.

In this post I will cover using Jasmine and Phantom.js. I have been a big fan of phantomjs a headless browser to test your web, its super fast and lightweight. In fact I did a local (Ottawa) JavaScript talk on it in 2012.

Jasmine

Jasmine is a BDD test framework for Javascript, we will be using to to test our app. Let’s get started and install jasmine to use in our express 4 application, again we will use npm or visual studio npm installer to install it.

npmjasmine

Jasmine npm install

Next we will write a simple JavaScript client code, let’s call it client.js script that we will perform the test on.

We will now add an spec file for it to test the JavaScript client.js file we just created.

The spec file is where we define our test, now that we have created the spec we can add our grunt task

We can now run our grunt task using Task Runner or by command line

The output would be something like

grunt jasmine output

grunt jasmine output

Now that we have Jasmine and grunt working, lets create a JQuery plugin so that we can test Jasmine with JQuery.
In order to do so we need to use bower. If you need a tutorial on bower you can look at my old post.
But first lets install bower using npm or by the npm tool in Visual Studio

Bower NPM Install

Bower NPM Install

Let’s now use bower to install jquery, jasmine-jquery plugin and we will save it to our dev environment.

I will reuse one of my plugin for jquery that I created that does locale sorting. The code base is small enough for us to test, since it sorts elements based on locale of the text. Lets place the file in our javascript folder so that Grunt can pick it up, lets call it localeSort.js

We also need to let our Gruntfile to know about our “vendor” jasmine and jquery plugin, in order to run the task.

We will also create a spec file and created a folder called fixtures and placed a html file sampleSort.html in there which contains a unordered list with fruit names, please look at jsfiddle for input

The result of our jasmine test would show.

Jamine Jquery Test

Jasmine Jquery Test

Summary

So this concludes our Grunt series, we have gone through the journey of using Grunt with Visual Studio, here were all the topics that were covered. Hope you enjoyed it.

  1. Using Grunt – The Javascript task runner
  2. Using Grunt – Visual Studio Grunt Launcher
  3. Using Grunt – Visual Studio 2013 Task Runner Explorer
  4. Using Grunt – Basic Tutorial
  5. Using Grunt – Copying Files
  6. Using Grunt – less and css minifcation
  7. Using Grunt – Javascript Uglify and Concat
RR Donnelley

Yesterday, was the last day at RR Donnelley Language Solutions for me, it was great working with an excellent team at RR Donnelley, but the time has come after 5 years working in Language Solutions to move and advance my career.
I will be moving to a new position as System Architect and be working on Security related software. I am excited and also expecting challenging time ahead. I will be still be blogging more on Node/JavaScript/MS .NET and will probably include more Security, Authentication, Performance related articles, so stay tuned.

UA-4524639-2