August, 2010 Archives

31
Aug

Outsource your caching with memcached

by Mikael Lundin in Programming

I’m currently working on a large business system for a client where there are several caching situations, because the service that delivers the data is too slow, the database is too slow or we don’t want to hit the source for the data all too often.

I’ve been hand rolling most of these cache layers myself. I don’t like the idea of using ASP.NET caching for non ASP.NET applications.

Memcached

Always when I hit the same problem the third time, I start looking for a common solution. When I was about to write my third caching implementation, I made a google search and found out that I probably should move the cache outside the application pool into a seperate service. Memcached is a service that I’ve heard of before, but I haven’t really tried it out.

Memcached is

  • a key/value store
  • hip and cool because it’s NoSQL
  • available as a windows service from NorthScale with a simple .NET client API

How does it work?

Download and install the service from NorthScale. You’ll get a user interface where you can analyze status of your cache service. Notice all the stuff about clustering which is cool, but probably only usable if you’re Facebook or Twitter.

Before you start writing code you should create a bucket, where you want to store your data. If I understood this correctly, buckets are just there to separate one type of cached data from another type of cached data.

Your own project

Add references to the Enyim.Caching.dll and Northscale.Store.dll.

You will need to add configuration to tell NorthScaleClient where to find memcached service.

<configuration>
	<configSections>
		<section name="northscale" type="NorthScale.Store.Configuration.NorthScaleClientSection, NorthScale.Store" />
	</configSections>
	<northscale>
		<servers bucket="default" userName="Administrator" password="password">
			<add uri="http://localhost:8080/pools/default" />
		</servers>
		<socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:00:10" />
	</northscale>
</configuration>

Now you can get and set data from the cache like this.

public string GetData(string key)
{
    // "memcached" is the configuration section name
    // "default" is the bucket name
    var cache = new NorthScaleClient("northscale", "default");

    // Get data from key, returns null if key is not set in cache
    var result = cache.Get<string>(key);
    if (result == null)
    {
        // Since cache did not exist, get from real source
        result = service.GetData();

        // Input data found to cache
        // Add result to cache (will not overwrite any value)
        // "key" - the key to store it under
        // "result" - the value to store
        // TimeSpan.FromDays(1) - let it expire in 24 hours
        cache.Store(StoreMode.Add, key, result, TimeSpan.FromDays(1));
    }

    return result;
}

Easy huh! Now, go out there and cache the world!

28
Aug

StarCraft 2 crashes on Connect to Battle.net

by Mikael Lundin in Other

I told you about my problems with the StarCraft II Loader recently. After solving that problem I didn’t get very far, until I reached next one.

The game will crash pressing the connect button.

After trying all kind of things I found out that my NOD32 Antivirus software was the cause. Or rather, the problem is that the StarCraft client does not play well with antivirus. Here’s another report with Comodo Internet Security.

Temporarily disable NOD32 to play StarCraft II

  1. Right click on the NOD32 icon in your task bar, and select Open Window.
  2. Press Setup and select “Temporarily disable Antivirus and antispyware protection”

Now you should be able to connect to battle.net and play the campaign. You might think that these issues should have been taken care of during the beta testing. Anyway, I’ll get back to you if I run into any more problems.

28
Aug

StarCraft II requires a patch. Would you like to download and install the patch now?

by Mikael Lundin in Other

So, I finally got around buying, and installing this StarCraft 2. I couldn’t resist being part of the talk-the-talk at work.

Anyhow, it took me 1 week before I had enough time to push the “Start Game” button, and then I met a loop with the error message “StarCraft II requires a patch. Would you like to download and install the patch now?”. Very frustrating.

The reason is that there is a patch available, but the downloader does not recognize your internet/proxy settings. This is easily fixed.

  1. Open up Internet Explorer. Go to Tools – > Internet Options.
  2. Go to connections tab and press the button [LAN Settings]
  3. Make sure that “Automatically detect settings” is unchecked. The problem could also be located around your Proxy server settings, if you’re using a proxy server.

Enjoy the game!

23
Aug

Project Euler #020

by Mikael Lundin in F#, Project Euler

n! means n × (n − 1) × … ××× 1

Find the sum of the digits in the number 100!

let factorial n : bigint = List.fold (*) 1I [1I .. n]

let sum (n:bigint) =
    n |> string |> Seq.fold (fun acc x -> acc + System.Int32.Parse(x.ToString())) 0

sum (factorial 100I)

If you consider using bigint to be cheating, here’s a solution that will calculate it with the use of lists instead.

let rec plan overflow l =
    match l with
    | head :: tail ->
        let column = head + overflow
        if column > 9 then
            let next_overflow = column / 10 |> float |> System.Math.Truncate |> int
            (column - next_overflow * 10) :: plan next_overflow tail
        else
            column :: plan 0 tail
    | _ -> if overflow > 0 then
              (overflow % 10) :: plan ((overflow - (overflow % 10)) / 10) []
           else
              []

let add (l1 : int list) (l2 : int list) =
    l1 |> List.map2 (fun i1 i2 -> i1 + i2) l2 

let prod (l1 : int list) (l2 : int list) =
    let tenths i = 10.0 ** (i |> float) |> int
    l2 |> List.mapi (fun i x -> l1 |> List.map (fun y -> x * y * tenths (-1 + l2.Length - i)))
    |> List.fold (fun acc l -> add acc l) (List.init l1.Length (fun i -> 0))
    |> List.rev
    |> plan 0 // from outer space
    |> List.rev

let to_list n =
    let rec calc x =
        if x > 0 then
            (x % 10) :: calc ((x - (x % 10)) / 10)
        else
            []
    calc n |> List.rev

[2..100] |> List.map (fun x -> to_list x) |> List.fold (fun acc y -> prod acc y) [1] |> List.sum
21
Aug

Project Euler #019

by Mikael Lundin in F#, Project Euler

You are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

let months = [31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31]

[0..99]
    |> List.map (fun x -> months) |> List.concat // all months consecutive
    |> List.mapi (fun i x -> if i = 1 && ((i - 1) / 12) % 4 = 0 then x + 1 else x) // leap year
    |> List.fold (fun (acc, result) x -> if (acc + x) % 7 = 6 then (acc + x, result + 1) else (acc + x, result)) (0,0) // Sunday bloody sunday