Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That looks nicer, but you changed the code; The original code would not call cache.set_user when the data already was in the cache.

You would need something in-between like:

    # Returns a cached user object either by their user_id or by their username.
    def get_cached_user(user_id = nil, username = nil)
        user =  cache.get_user_by_id(user_id)        ||
                cache.get_user_by_username(username)
        if not user:
            user = db.get_user_by_id(user_id)        || 
                   db.get_user_by_username(username) ||
                   raise ValueError('User not found')            
            cache.set_user(user, user.id, user.username)
        return user
    end


I knew someone would catch that. :)

It's dependent on your infrastructure and demands, of course, but I probably wouldn't bother optimizing that unless cache sets were prohibitively expensive.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: