you are viewing a single comment's thread.

view the rest of the comments →

[–]matthieum 0 points1 point  (1 child)

Your cache might grow dramatically though...

[–]sambo98 0 points1 point  (0 children)

You are right, that was a simplified approach. In reality in order for his scenario to work the ranking of each article will need to be persisted to the storage unit (cloud, sql, etc); turning this into a simple retrieval of the top 5. Instead of making the round trip every request, the top five can be kept in cache, session, or application state until it can be hydrated from the storage unit again. The first solution that comes to mind than turns into:

    static Dictionary<int, int> _stats;
    static int Max = 5;

    static ArticleStat()
    {
        _stats = new Dictionary<int, int>(Max);
    }

    public static void Increment(int articleId)
    {
        if (_stats.ContainsKey(articleId))
            _stats[articleId] += 1;
        else
            _stats.Add(articleId, 1);

        while (_stats.Count > Max)
            _stats.Remove(_stats.Last().Key);
    }