예제 #1
0
def get_entity(key_name, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    e = memcache.get(key=key_name, namespace=ent_type)
    if e:
        try:
            e = deserialize(e)
        except:
            logging.error(
                "Memcache_db: Unable to deserialize entity of type %s" %
                ent_type)
            e = None

    if not e:
        memcache.delete(key=key_name, namespace=ent_type)
        if ent_type == "Accounts":
            e = Accounts.get_by_key_name(key_name)
        elif ent_type == "Badges":
            e = Badges.get_by_key_name(key_name)
        elif ent_type == "BadgeInstance":
            e = BadgeInstance.get_by_key_name(key_name)
        elif ent_type == "BadgeImage":
            e = BadgeImage.get_by_key_name(key_name)
        elif ent_type == "Users":
            e = Users.get_by_key_name(key_name)
        elif ent_type == "TrophyCase":
            e = TrophyCase.get_by_key_name(key_name)
        elif ent_type == "Points":
            e = Points.get_by_key_name(key_name)
        elif ent_type == "Notifier":
            e = Notifier.get_by_key_name(key_name)
        elif ent_type == "Rank":
            e = Rank.get_by_key_name(key_name)
        elif ent_type == "PassPhrase":
            e = PassPhrase.get_by_key_name(key_name)
        elif ent_type == "Milestones":
            e = Milestones.get_by_key_name(key_name)
        elif ent_type == "Leaderboard":
            e = Leaderboard.get_by_key_name(key_name)
        else:
            raise Exception()
        if e:
            memcache.add(key=key_name,
                         value=str(serialize(e)),
                         namespace=ent_type)
    return e
예제 #2
0
def __batch_get_entity(key_names, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    es = memcache.get_multi(keys=key_names, namespace=ent_type)
    ents = []
    db_ents = {}
    for key in key_names:
        e = None
        if key in es:
            try:
                e = deserialize(e)
                ents.append(e)
            except Exception, ex:
                logging.error(
                    "Memcache_db: Unable to deserialize entity of type %s with %s"
                    % (ent_type, str(ex)))
                e = None
        if not e:
            # These puts are in a loop, making this function slow
            memcache.delete(key=key, namespace=ent_type)
            if ent_type == "Accounts":
                dbent = Accounts.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "Badges":
                dbent = Badges.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "BadgeInstance":
                dbent = BadgeInstance.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "BadgeImage":
                dbent = BadgeImage.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "Users":
                dbent = Users.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            elif ent_type == "TrophyCases":
                dbent = TrophyCases.get_by_key_name(key)
                ents.append(dbebt)
                db_ents[key] = serialize(dbent)
            else:
                raise Exception()
예제 #3
0
def __batch_get_entity(key_names, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  es = memcache.get_multi(keys=key_names, namespace=ent_type)
  ents = []
  db_ents = {}
  for key in key_names:
    e = None
    if key in es:
      try:
        e = deserialize(e) 
        ents.append(e)
      except Exception, ex:
        logging.error("Memcache_db: Unable to deserialize entity of type %s with %s"%(ent_type, str(ex)))
        e = None
    if not e:
      # These puts are in a loop, making this function slow
      memcache.delete(key=key, namespace=ent_type)
      if ent_type == "Accounts":
        dbent = Accounts.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "Badges":
        dbent = Badges.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "BadgeInstance":
        dbent = BadgeInstance.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "BadgeImage":
        dbent = BadgeImage.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "Users":
        dbent = Users.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      elif ent_type == "TrophyCases":
        dbent = TrophyCases.get_by_key_name(key)
        ents.append(dbebt)
        db_ents[key] = serialize(dbent)
      else:
        raise Exception()
예제 #4
0
def get_entity(key_name, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  e = memcache.get(key=key_name, namespace=ent_type)
  if e:
    try:
      e = deserialize(e)
    except:
      logging.error("Memcache_db: Unable to deserialize entity of type %s"%ent_type)
      e = None 

  if not e:
    memcache.delete(key=key_name, namespace=ent_type)
    if ent_type == "Accounts":
      e = Accounts.get_by_key_name(key_name)
    elif ent_type == "Badges":
      e = Badges.get_by_key_name(key_name)
    elif ent_type == "BadgeInstance":
      e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "BadgeImage":
      e = BadgeImage.get_by_key_name(key_name)
    elif ent_type == "Users":
      e = Users.get_by_key_name(key_name)
    elif ent_type == "TrophyCase":
      e = TrophyCase.get_by_key_name(key_name)
    elif ent_type == "Points":
      e = Points.get_by_key_name(key_name)
    elif ent_type == "Notifier":
      e = Notifier.get_by_key_name(key_name)
    elif ent_type == "Rank":
      e = Rank.get_by_key_name(key_name)
    elif ent_type == "PassPhrase":
      e = PassPhrase.get_by_key_name(key_name)
    elif ent_type == "Milestones":
      e = Milestones.get_by_key_name(key_name)
    elif ent_type == "Leaderboard":
      e = Leaderboard.get_by_key_name(key_name)
    else:
      raise Exception()
    if e:
      memcache.add(key=key_name,value=str(serialize(e)),namespace=ent_type)
  return e
예제 #5
0
def is_in_db(key_name, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  e = None
  if ent_type == "Accounts":
    e = Accounts.get_by_key_name(key_name)
  elif ent_type == "Badges":
    e = Badges.get_by_key_name(key_name)
  elif ent_type == "BadgeInstance":
    e = BadgeInstance.get_by_key_name(key_name)
  elif ent_type == "BadgeImage":
    e = BadgeInstance.get_by_key_name(key_name)
  elif ent_type == "Users":
    e = Users.get_by_key_name(key_name)
  elif ent_type == "TrophyCases":
    e = TrophyCase.get_by_key_name(key_name)
  else:
    raise Exception()

  if e: return True
  else: return False
예제 #6
0
def is_in_db(key_name, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    e = None
    if ent_type == "Accounts":
        e = Accounts.get_by_key_name(key_name)
    elif ent_type == "Badges":
        e = Badges.get_by_key_name(key_name)
    elif ent_type == "BadgeInstance":
        e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "BadgeImage":
        e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "Users":
        e = Users.get_by_key_name(key_name)
    elif ent_type == "TrophyCases":
        e = TrophyCase.get_by_key_name(key_name)
    else:
        raise Exception()

    if e: return True
    else: return False