示例#1
0
def fetch_current_user_items(cls, user_id, ref_ids, key='topic_id'):
    if not ref_ids:
        return {}

    prefix = cls.generate_cache_prefix('get')

    def gen_key(tid):
        return prefix + '-'.join(map(str, [tid, user_id]))

    def get_key(key):
        return key.lstrip(prefix).split('-')[0]

    rv = cache.get_dict(*[gen_key(tid) for tid in ref_ids])
    missed = {i for i in ref_ids if rv[gen_key(i)] is None}
    rv = {get_key(k): rv[k] for k in rv}
    if not missed:
        return rv

    to_cache = {}
    q = cls.cache.filter_by(user_id=user_id)
    for item in q.filter(getattr(cls, key).in_(missed)):
        rv[str(getattr(item, key))] = item
        to_cache[gen_key(getattr(item, key))] = item

    cache.set_many(to_cache, CACHE_TIMES['get'])
    return rv
示例#2
0
文件: topic.py 项目: 343829084/zerqu
def fetch_current_user_items(cls, user_id, ref_ids, key='topic_id'):
    if not ref_ids:
        return {}

    prefix = cls.generate_cache_prefix('get')

    def gen_key(tid):
        return prefix + '-'.join(map(str, [tid, user_id]))

    def get_key(key):
        return key.lstrip(prefix).split('-')[0]

    rv = cache.get_dict(*[gen_key(tid) for tid in ref_ids])
    missed = {i for i in ref_ids if rv[gen_key(i)] is None}
    rv = {get_key(k): rv[k] for k in rv}
    if not missed:
        return rv

    to_cache = {}
    q = cls.cache.filter_by(user_id=user_id)
    for item in q.filter(getattr(cls, key).in_(missed)):
        rv[str(getattr(item, key))] = item
        to_cache[gen_key(getattr(item, key))] = item

    cache.set_many(to_cache, CACHE_TIMES['get'])
    return rv
示例#3
0
文件: base.py 项目: valley51/zerqu
    def get_dict(self, idents):
        if not idents:
            return {}

        mapper = self._only_full_mapper_zero('get')
        if len(mapper.primary_key) != 1:
            raise NotImplemented

        prefix = mapper.class_.generate_cache_prefix('get')
        keys = {prefix + str(i) for i in idents}
        rv = cache.get_dict(*keys)

        missed = {i for i in idents if rv[prefix + str(i)] is None}

        rv = {k.lstrip(prefix): rv[k] for k in rv}

        if not missed:
            return rv

        pk = mapper.primary_key[0]
        missing = self.filter(pk.in_(missed)).all()
        to_cache = {}
        for item in missing:
            ident = str(getattr(item, pk.name))
            to_cache[prefix + ident] = item
            rv[ident] = item

        cache.set_many(to_cache, CACHE_TIMES['get'])
        return rv