huskar_api/models/cache/__init__.py
def mget(cls, _ids, force=False, as_dict=False):
        """Query a list of objects by pks.

        :func:`~mget` will always use db slave, values will be stored
        to cache if cache misses.

        Use ``force=True`` to force load from db.

        :param _ids: a list of pks
        :param force: whether force to load from db
        :param as_dict: return dict or list
        :return: dict or list of objects
        """
        if not _ids:
            return {} if as_dict else []

        objs = {}
        if not force:
            # load from session
            if cls._db_session.identity_map:
                for i in _ids:
                    ident_key = identity_key(cls, i)
                    if ident_key in cls._db_session.identity_map:
                        objs[i] = cls._db_session.identity_map[ident_key]

            # load from cache
            if len(_ids) > len(objs):
                missed_ids = list(set(_ids) - set(objs))
                _objs = cls._from_cache(missed_ids, from_raw=True)
                objs.update(_objs)

        lack_ids = set(_ids) - set(objs)
        if lack_ids:
            pk = cls.pk_attribute()
            # we assume CacheMixin have pk, if not, bypass it.
            if pk:
                lack_objs = cls._db_session().using_bind('master').\
                    query(cls).filter(pk.in_(lack_ids)).all()
                if lack_objs:
                    cls.mset(lack_objs, nx=True)

                cls._statsd_incr("miss", len(lack_ids))

                objs.update({obj.pk: obj for obj in lack_objs})
            else:  # pragma: no cover
                logger.warn("No pk found for %s, skip %s" %
                            cls.__tablename__, lack_ids)

        # TODO hack to make mget return list
        return objs if as_dict else _dict2list(_ids, objs)
Query a list of objects by pks.

        :func:`~mget` will always use db slave, values will be stored
        to cache if cache misses.

        Use ``force=True`` to force load from db.

        :param _ids: a list of pks
        :param force: whether force to load from db
        :param as_dict: return dict or list
        :return: dict or list of objects
        
Query a list of objects by pks.
Query a list of objects by pks .
def mget(cls, _ids, force=False, as_dict=False):
        if not _ids:
            return {} if as_dict else []
        objs = {}
        if not force:
            if cls._db_session.identity_map:
                for i in _ids:
                    ident_key = identity_key(cls, i)
                    if ident_key in cls._db_session.identity_map:
                        objs[i] = cls._db_session.identity_map[ident_key]
            if len(_ids) > len(objs):
                missed_ids = list(set(_ids) - set(objs))
                _objs = cls._from_cache(missed_ids, from_raw=True)
                objs.update(_objs)
        lack_ids = set(_ids) - set(objs)
        if lack_ids:
            pk = cls.pk_attribute()
            if pk:
                lack_objs = cls._db_session().using_bind('master').\
                    query(cls).filter(pk.in_(lack_ids)).all()
                if lack_objs:
                    cls.mset(lack_objs, nx=True)
                cls._statsd_incr("miss", len(lack_ids))
                objs.update({obj.pk: obj for obj in lack_objs})
            else:  
                logger.warn("No pk found for %s, skip %s" %
                            cls.__tablename__, lack_ids)
        return objs if as_dict else _dict2list(_ids, objs)
def mget ( cls , _ids , force = False , as_dict = False ) : if not _ids : return { } if as_dict else [ ] objs = { } if not force : if cls . _db_session . identity_map : for i in _ids : ident_key = identity_key ( cls , i ) if ident_key in cls . _db_session . identity_map : objs [ i ] = cls . _db_session . identity_map [ ident_key ] if len ( _ids ) > len ( objs ) : missed_ids = list ( set ( _ids ) - set ( objs ) ) _objs = cls . _from_cache ( missed_ids , from_raw = True ) objs . update ( _objs ) lack_ids = set ( _ids ) - set ( objs ) if lack_ids : pk = cls . pk_attribute ( ) if pk : lack_objs = cls . _db_session ( ) . using_bind ( 'master' ) . query ( cls ) . filter ( pk . in_ ( lack_ids ) ) . all ( ) if lack_objs : cls . mset ( lack_objs , nx = True ) cls . _statsd_incr ( "miss" , len ( lack_ids ) ) objs . update ( { obj . pk : obj for obj in lack_objs } ) else : logger . warn ( "No pk found for %s, skip %s" % cls . __tablename__ , lack_ids ) return objs if as_dict else _dict2list ( _ids , objs )
Query a list of objects by pks.
Query a list of objects by pks .
"""Query a list of objects by pks.

        :func:`~mget` will always use db slave, values will be stored
        to cache if cache misses.

        Use ``force=True`` to force load from db.

        :param _ids: a list of pks
        :param force: whether force to load from db
        :param as_dict: return dict or list
        :return: dict or list of objects
        """ # load from session # load from cache # we assume CacheMixin have pk, if not, bypass it. # pragma: no cover # TODO hack to make mget return list
dict or list of objects
dict or list of objects
cls
_ids
a list of pks
a list of pks
force
whether force to load from db
whether force to load from db
as_dict
return dict or list
return dict or list
func
`~mget` will always use db slave, values will be stored
to cache if cache misses.

Use ``force=True`` to force load from db.
` ~mget ` will always use db slave values will be stored to cache if cache misses . Use ` ` force = True ` ` to force load from db .