Search code examples
pythondjangomemcachedtornado

Django memcached inconsistency


So, this problem is rather elaborate. I'll try to explain as best I can. Any help would be extremely appreciated. Thanks in advance!

I'm building an application that uses tornadio2 + Memcached, and am finding some perplexing things when trying to pull from cache.

For starters, here is my broadcast_to_players() function:

def broadcast_to_players(self, g_id, event, message, update_secret=False, dont_update_self=False):
    the_game = cache.get(g_id)
    for player in the_game.player_list:
        if update_secret == True:
            secret = self.get_secret()
            message['k'] = secret
            player.secret_key = secret
        if dont_update_self == True:
            if not self.session_id == player.session_id:
                single_session = self.session.server._sessions.get(player.session_id)
                single_session.conn.emit(event, message)
        else:
            single_session = self.session.server._sessions.get(player.session_id)
            single_session.conn.emit(event, message)
    cache.set(g_id, the_game)

    ## FOR TESTING PURPOSES ##
    if update_secret == True:
        other_game = cache.get(g_id)
        for player in other_game.player_list:
            print "player.secret_key in broadcast_to_players = %s" % player.secret_key

As you can see, it grabs the_game from cache, does stuff to it, modifies a message, updates the players in my player_list, and sends the message. Then, it sets the game in cache. Afterward, (for testing purposes) I grab it again and test to see if the secret_key values of each player made it in the cache. And they do.

However, when I try to grab the same game from cache again, it says the secret_key = None

def check_player(self, g_id, key):
    the_game = cache.get(g_id) #I'm able to 
    the_player = None
    for player in the_game.player_list:
        if player.secret_key == key:
            the_player = player
    return the_player

The game is being cached, but the player_list isn't being updated in the Game model. Here are my Game and Player models:
http://pastebin.com/t66qgeeb - Game model
http://pastebin.com/7FjpeSLx - Player model

As you can see, the Player model has a many-to-many relationship to Game, through User. Also, I have many non-persisting variables (for the game being played) that I keep in my Django model classes. My guess is it might be something to do with the fact that I'm grabbing all the players associated with my game, putting them in player_list, and then trying to update them? And memcached doesn't do that deep of a copy? I don't know, and I've been racking my brain trying to figure it out. Any help at all would be much appreciated. Thanks!


Solution

  • Ok. Let's move point by point:

    1. Don't put big objects in memcached - you will have high chance of miss.
    2. In this case - split keys. You must have one key for game, one key player list, and keys for each player in the game.
    3. Store player in memcache by key player_<g_id>_<key> - then you will be able to get player easily.

    Let's put it all together:

    def broadcast_to_players(self, g_id, event, message, update_secret=False, dont_update_self=False):
        the_game = cache.get(g_id)
        player_list = cache.get('game_%s_players' % g_id) #Getting list of players ids
        for player_key in player_list:
            player = cache.get('game_%s_%s' % (g_id, player_key))
            if update_secret == True:
                secret = self.get_secret()
                message['k'] = secret
                player.secret_key = secret
                player.save() #We are storing secret in db, right?
                cache.set('game_secret_%s_%s' % (g_id, secret), player_key) #Making player availiable in game by secret
                cache.set('player_%s_secret' % (player_key, ), secret) #Making player secret available by player id
            if dont_update_self == True:
                if not self.session_id == player.session_id:
                    single_session = self.session.server._sessions.get(player.session_id)
                    single_session.conn.emit(event, message)
            else:
                single_session = self.session.server._sessions.get(player.session_id)
                single_session.conn.emit(event, message)
        cache.set(g_id, the_game)
    
    def check_player(self, g_id, key):
        return cache.get('game_secret_%s_%s' % (g_id, key))
    

    It's only idea, how you should think, when working with key/value storages.