Using the Redis NoSql database with .NET Part 6: the hash data type in C#

Introduction

In the previous post we looked at the sorted set data type in Redis. A sorted set is not simply a set where the values are sorted. It is a container of unique strings where each member also has a score. The members in the sorted set are sorted by their scores. Sorted sets can be applicable for scores reached in a competition, the time it took to complete various activities, the energy lost during a physical exercise etc., i.e. anywhere where a member also has a numeric value attached to it. We also discussed the basics of persistence and how it is configured for the Windows Redis service through a configuration file.

In this post we’ll look at one remaining data type in Redis, namely hashes.

Read more of this post

How to create custom string formatters with C# .NET

.NET has a fairly large number of built-in string formatters that you can pass into the string.Format method. Here are some examples from the MSDN page about formatting:

String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                                city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                                (city.Item5 - city.Item3)/ (double)city.Item3);
String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
                                    "City", "Year", "Population", "Change (%)");
String.Format("{0,-10:C}", 126347.89m);         

The IFormatProvider and ICustomFormatter interfaces will provide you with the methods required to create your own formats.

Read more of this post

Using the Redis NoSql database with .NET Part 5: the sorted set data type

Introduction

In the previous post we looked at the set data type in Redis. Sets are similar to list but they only store unique strings. Duplicates are discarded and the items are case sensitive. This means that we can add “monday” and “MONDAY” in the same set. Another difference compared to lists is that sets are not guaranteed to hold the items in the same order as they were entered. Redis has commands to perform a couple of common set-related actions such as calculating the union, difference and intersection of two or more sets. We also looked at two techniques to extract the key names from the Redis database: KEY and SCAN where SCAN is a cursor based iterator and is the recommended method since it’s a lighter operation for the database.

In this post we’ll first talk a little bit about persistence to disk in Redis. Then we’ll move onto a discussion of the sorted set data type.

Read more of this post

Compressing and decompressing strings with BZip2 in .NET C#

There are times when you need to return a large text from a web service. The large text will then need to be handled by the recipient. In order to reduce the size of the message you can combine two simple techniques:

  • Compress the string value with a compression algorithm, such as BZip2
  • Base64 encode the resulting byte array

You will be able to send the base 64 encoded compressed string over the wire.

Read more of this post

Using the Redis NoSql database with .NET Part 4: key name searches and the set data type

Introduction

In the previous post we looked at the list data type in Redis. Lists are implemented as linked lists in Redis meaning that each item has a link to its immediate neighbours, i.e. the previous and the next items. It’s efficient to operate on the first or last item in a linked list: add to and retrieve from the head or tail of a linked list is very fast. Redis lists therefore lend themselves very well to queues and stacks. Index-based operations, e.g. getting the 3rd item in the list, are not as efficient. We then looked at a couple of list-related Redis commands such as LINSERT, RPOP and LRANGE. We also looked at how to add the Redis folder to the environment variables on Windows so that we can call upon the Redis executables without having to navigate to the folder in a command prompt.

In this post we’ll first look at how to retrieve the key names in our Redis database. Then we’ll continue our discussion of the Redis data types with sets.

Read more of this post

Modules in an F# project

F# supports grouping related code into namespaces and modules. Namespaces can only contain type declarations. They cannot contain values i.e. elements that are declared with the “let” keyword. Modules on the other hand can handle both types and values. Modules can also contain other modules, i.e. they can be nested.

Here’s a simple module with a type and a function:

Read more of this post

Changing the order of compilation in an F# project

If you start a new F# project in Visual Studio then the first source file you’ll get is Program.fs with a main function. Then say you add another source file called Domains.fs with the following namespace and type:

namespace com.mycompany.domains.book

type Book = {
    title: string;
    numberOfPages:int;
    author: string
} 
with member this.takesLongTimeToRead = this.numberOfPages > 500

The source file will at first land under Program.fs in the class hierarchy in Visual Studio:

Read more of this post

Using the Redis NoSql database with .NET Part 3: lists in Redis

Introduction

In the previous post we first installed the Redis server as a Windows service. It’s cumbersome having to execute redis-server.exe every time we want to communicate with the database so we’ll have it running as a service. Then we went on to explore the string data type in Redis. String is the most versatile data type since we can store pretty much anything as a string in one way or another: messages like “Hello”, JSON serialised custom objects, base 64 encoded byte arrays for e.g. file content, numbers etc. We looked at a couple of string-related commands like STRLEN or APPEND.

In this post we will first add Redis to the environment variables so that we can reach the executables without navigating to the Redis folder in the command prompt. Then we’ll look at how lists are implemented in Redis.

Read more of this post

Creating a grouped join on two sequences using the LINQ GroupJoin operator

The GroupJoin operator is very similar to the Join operator. I won’t repeat the things we discussed there, so I recommend you read that post first if you’re not familiar with that operator.

With GroupJoin we can not only join elements in two sequences but group them at the same time.

GroupJoin has the same signature as Join with one exception. The result selector in Join has the following type:

Func<TOuter, TInner, TResult>

Read more of this post

Using the Redis NoSql database with .NET Part 2: the Redis Windows service and some basic string commands

Introduction

In the previous post we introduced the topic of this series: Redis with .NET. We first went through some basic characteristics of Redis. It is a NoSql key-value store with in-memory storage and access by default. The records are stored by unique keys where each unique key has a value attached. It has many good features like fast data access, data replication, scripting, high-availability clusters, various data types and much more. Normally Redis is installed on Linux servers but we’ll go with the officially unsupported Windows 64-bit port maintained by Microsoft. That will fit most .NET developers out there who are the main target group of this series. We started the Redis server and client and tried three of the most basic Redis commands: GET, SET and DEL that vaguely correspond to SELECT, INSERT/UPDATE and DELETE statements in SQL.

In this post we will first install the Redis server as a Windows service and then test a couple of Redis commands related to strings.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.