Caching in multi thread application – it’s not that simple

When developing multi user applications like website we potentially dealing with lots of concurrent requests. And naturally we want to cache the results of expensive methods:

public int GetUsersCount ()
{
    var cache_key = "UsersCount";
 
    var users_count_obj = this.cache.Get (cache_key);
    if (users_count_obj != null)
        return (int) users_count_obj;
 
    var users_count = GetUsersCountFromDb ();
 
    this.cache.Add (cache_key,
                    users_count,
                    new CachingParameters (TimeSpan.FromMinutes (15)));
 
    return users_count;
}

This method uses Rocks.Caching.ICacheProvider that I blogged before about. Basically, the we write cache code like this:

  1. Try get result from cache
  2. If the result is not null return it
  3. Otherwise calculate the result
  4. Add the result to the cache
  5. Return the result

Now, it works for most of the time until we starting to get a lot of concurrent requests for the same method. The problem is point 2 and 3.

Continue reading

Rocks.Caching – An abstraction over cache with bonuses

I’ve recently published source code of one my libraries that I use in many home and production projects. It’s called Rocks.Caching and it’s part of my “Rocks” libraries collection that targeted to gather all my best practices in different features implementations. You can also find it on nuget.

Continue reading

Think twice before applying DDD in your web application

Domain Driven Design is the thing most of us (developers) stumble upon at some point of their career. There is quite number of books and posts about this topic and a couple of big communities. Developers starting exploring DDD work (probably from Eric Evan “Blue Book”) and many of them became convinced how good this DDD thing is. And they are right – DDD is awesome but just like any tool it’s awesome only in some circumstances and only for specific kind of work. Just like poor knowledge can lead to bad smelly code, applying complex methodologies without knowing their cons can make the code even less maintainable.

Continue reading

Repository – you mean data access service?

First post on my blog. Well, it has to start somewhere, isn’t it?

you-keep-using-that-wordThere are a number of articles about how you should design you web application. And in many of them there is a recommendation to invest in building repository for data access. And that’s where I think many new developers got wrong impression about what exactly the repository is.


Continue reading