本文整理汇总了Python中canvas.cache_patterns.CachedCall类的典型用法代码示例。如果您正苦于以下问题:Python CachedCall类的具体用法?Python CachedCall怎么用?Python CachedCall使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CachedCall类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: posts
def posts(request, payload={}, short_id=None):
"""
Posts endpoint of the example.com public api
Request with an id parameter:
/public_api/posts/1qkx8
POST JSON in the following format:
POST /public_api/posts/
{"ids":["1qkx8","ma6fz"]}
"""
Metrics.api_comment.record(request)
ids = payload.get('ids')
if short_id and not ids:
try:
comment = Comment.details_by_id(long_id(short_id), promoter=PublicAPICommentDetails)
(comment,) = CachedCall.multicall([comment])
return comment.to_client()
except (ObjectDoesNotExist, util.Base36DecodeException):
raise ServiceError("Post not found")
elif ids:
ids = [long_id(x) for x in set(ids)]
calls = [Comment.details_by_id(id, ignore_not_found=True, promoter=PublicAPICommentDetails) for id in ids]
comments = CachedCall.multicall(calls, skip_decorator=True)
return {'posts': [x.to_client() for x in comments if x]}
示例2: search_stamps
def search_stamps(request, query, start):
"""
Searches the special "stamps" group for stamps that match the search query.
Returns {comments: [list of comment details]}
"""
qs = query
try:
start = int(start)
except TypeError:
raise ServiceError('Invalid "start" parameter.')
stamps = models.Category.objects.get(name="stamps")
if qs:
ids = [x for x in models.Comment.objects.filter(category=stamps).filter(
Q(reply_text__icontains=qs) | Q(title__icontains=qs)
).values_list('id', flat=True)]
ids = ids[start:start+32]
comments = models.Comment.curated.exclude(reply_content__id=None).in_bulk(ids)
details = CachedCall.multicall([comments[id].details for id in ids if id in comments])
else:
comments = models.Comment.curated.filter(category=stamps).exclude(reply_content__id=None).order_by('-id')
comments = comments[start:start+32]
details = CachedCall.queryset_details(comments)
return {'comments': details}
示例3: _make_activities
def _make_activities(self, activity_ids, earlier_than=None, later_than=None):
from apps.activity.models import Activity, LegacyActivity
def filter_by_ts(query):
if earlier_than is not None:
query = query.filter(timestamp__lt=earlier_than)
if later_than is not None:
query = query.filter(timestamp__gt=later_than)
return query
activity_ids = [int(id_) for id_ in activity_ids]
activities = Activity.objects.filter(id__in=activity_ids).order_by('-timestamp')
activities = filter_by_ts(activities)
activities = CachedCall.queryset_details(activities)
if len(activities) < len(activity_ids):
legacy_ids = set(activity_ids) - set(int(activity['id']) for activity in activities)
legacy_activities = LegacyActivity.objects.filter(id__in=legacy_ids).order_by('-timestamp')
legacy_activities = filter_by_ts(legacy_activities)
legacy_activities = CachedCall.queryset_details(legacy_activities)
activities.extend(legacy_activities)
ret = []
for activity_data in activities:
try:
ret.append(self._activity_types[activity_data['activity_type']](activity_data))
except KeyError as e:
continue
return ret
示例4: test_force_returns_new_value
def test_force_returns_new_value(self):
fun = CB(retvalue=1)
cc = CachedCall('key', fun)
cc()
fun.retvalue = 2
self.assertEqual(2, cc.force())
示例5: test_force_updates_cached_value
def test_force_updates_cached_value(self):
fun = CB(retvalue=1)
cc = CachedCall('key', fun)
cc()
fun.retvalue = 2
cc.force()
self.assertEqual(2, cc())
示例6: test_skip_decorator
def test_skip_decorator(self):
def decorator(self):
raise Exception
fun = CB()
cc = CachedCall('key', fun, decorator=decorator)
self.assertEquals(fun.retvalue, cc(skip_decorator=True))
self.assertEquals(fun.retvalue, cc.force(skip_decorator=True))
示例7: test_queryset_details
def test_queryset_details(self):
comments = [create_comment(reply_content=create_content()) for _ in xrange(10)]
details1 = CachedCall.multicall([cmt.details for cmt in comments])
queryset = Comment.objects.filter(id__in=[cmt.id for cmt in comments])
details2 = CachedCall.queryset_details(queryset)
self.assertEquals(details1, details2)
示例8: test_multicall_half_cached_returns_results
def test_multicall_half_cached_returns_results(self):
funs = [CB(retvalue=n) for n in [1,2,3,4,5]]
calls = [CachedCall("key_%s" % e, fun) for e,fun in enumerate(funs)]
# Uncached
self.assertEquals([1,2,3], CachedCall.multicall(calls[:3]))
# Half cached
self.assertEquals([1,2,3,4,5], CachedCall.multicall(calls))
self.assertEquals([1] * len(funs), [fun.called for fun in funs])
示例9: api_monster_details
def api_monster_details(request, short_id, payload={}):
view_data = CommentViewData(request, short_id)
main_comment = view_data.op_comment
replies = [Comment.details_by_id(cid) for cid in view_data.reply_ids]
has_replies = len(replies) > 0
(
(main_comment,),
replies
) = CachedCall.many_multicall(
[main_comment],
replies,
)
treplies = []
made_bottom = False
for reply in replies:
cur = reply.to_client()
if reply.real_author == request.user.username:
cur['current_user_authored'] = made_bottom = True
treplies.append(cur)
ctx = {
'top': main_comment,
'bottoms': treplies,
'current_user_made_bottom': made_bottom,
'current_user_made_top': main_comment.real_author == request.user.username,
'start_content': Content.all_objects.get(id=Content.SMALL_DRAW_FROM_SCRATCH_PK).details(),
}
return ctx
示例10: test_inprocess_cached_prevents_multiple_fetches_in_multicall
def test_inprocess_cached_prevents_multiple_fetches_in_multicall(self):
funs = [CB(retvalue=n) for n in [1,2,3,4,5]]
calls = [CachedCall("key_%s" % e, fun) for e,fun in enumerate(funs)]
# Uncached
self.assertEquals([1,2,3], CachedCall.multicall(calls[:3]))
# Remove some of the backing stores
cache.delete('key_0')
cache.delete('key_1')
CachedCall.inprocess_cache.delete('key_2')
# 1,2 in-process only, 3 in redis only, 4,5 uncached
self.assertEquals([1,2,3,4,5], CachedCall.multicall(calls))
self.assertEquals([1] * len(funs), [fun.called for fun in funs])
示例11: gallery_comments
def gallery_comments(quest, offset='top', direction='next', force_comment=None, viewer=None,
include_reactions=True):
"""
Returns comments, pagination. Each comment is itself a dict.
"""
if force_comment is not None:
newer_comments = QuestComment.objects.filter(parent_comment=quest, id__gt=force_comment.id).order_by('id').values_list('id', flat=True)
try:
offset = list(newer_comments[:knobs.COMMENTS_PER_PAGE / 2])[-1]
except IndexError:
offset = force_comment.id
pagination = Paginator(_exclude_flagged(_all_gallery_comments(quest), viewer), knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction)
comments = pagination.items
promoter = None if include_reactions else QuestCommentGalleryDetails
comments = CachedCall.queryset_details(comments, promoter=promoter)
add_viewer_has_starred_field(comments, viewer=viewer)
if force_comment is not None and force_comment.id not in [cmt['id'] for cmt in comments]:
if force_comment.visibility != Visibility.CURATED:
raise Http404()
comments.append(force_comment.details())
comments = sorted(comments, key=lambda cmt: -cmt['id'])
if viewer is not None and viewer.is_authenticated():
following = viewer.following_ids()
for comment in comments:
comment.user.viewer_is_following = comment.user.id in following
return comments, pagination
示例12: from_queryset
def from_queryset(cls, comments):
bottoms, tops = CachedCall.many_multicall([cmt.details for cmt in comments],
[cmt.thread.op.details for cmt in comments])
tiles = []
for bottom, top in zip(bottoms, tops):
tile = cls(bottom, top)
tiles.append(tile)
return tiles
示例13: get_from_comment
def get_from_comment(cls, comment):
candidates = Comment.public.in_bulk_list([comment.id] + comment.top_replies[:10])
candidates = [post for post in candidates if post.reply_content]
candidates = sorted(candidates, key=lambda comment: -comment.get_score()[0])
thread = [comment] + candidates[:5]
(posts,) = CachedCall.many_multicall([post.details for post in thread])
return cls(*posts)
示例14: starred_comments_gallery
def starred_comments_gallery(user, offset='top', direction='next'):
stars = CommentSticker.objects.filter(user=user).order_by('-timestamp')
pagination = Paginator(stars, knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction)
comments = CachedCall.multicall([QuestComment.details_by_id(id_)
for id_
in pagination.items.values_list('comment_id', flat=True)])
return comments, pagination
示例15: from_queryset_with_viewer_stickers
def from_queryset_with_viewer_stickers(cls, viewer, comments):
bottoms, tops = CachedCall.many_multicall([cmt.details for cmt in comments],
[cmt.thread.op.details for cmt in comments])
tiles = []
for bottom, top in zip(bottoms, tops):
tile = cls(bottom, top)
tile.viewer_sticker = Comment.get_sticker_from_user_for_comment_id(bottom.id, viewer)
tiles.append(tile)
return tiles