def get_posts_by_cid(cid, num=0): cache_key = 'cr_cid_%s' % cid posts = [pickle.loads(i) for i in r.lrange(cache_key, 0, -1)] if not posts: cr_list = Copyright.objects.filter(cid=cid) for cr in cr_list: post = Post.get(pid=cr.pid) post.roles = cr.roles posts.append(post) r.lpush(cache_key, pickle.dumps(post)) return posts[:num or -1]
def get_composers(self): cache_key = 'cr_pid_%s' % self.pid composers = [pickle.loads(i) for i in r.lrange(cache_key, 0, -1)] if not composers: cr_list = Copyright.objects.filter(pid=self.pid).all() for cr in cr_list: composer = Composer.get(cid=cr.cid) if composer: composer.role = cr.roles composers.append(composer) r.lpush(cache_key, pickle.dumps(composer)) return composers
def composers(self): """取出当前作品的所有作者""" # composers = [] cache_key = 'cr_pid_%s' % self.pid composers = [pickle.loads(i) for i in r.lrange(cache_key, 0, -1)] # if r.exists(cache_key): if not composers: cr_list = Copyright.filter(pid=self.pid).all() for cr in cr_list: composer = Composer.get(cid= cr.cid) if composer: composer.role = cr.roles composers.append(composer) r.lpush(cache_key,pickle.dumps(composer)) return composers