def update_hits_value_cache(story_id): """ Update hits value of story cache Parameter: story ID """ story = api.get_story_object(int(story_id)) cache.set('story:' + str(story_id) + ':hits', str(story.hits), timeout=CACHE_TTL) return story.hits
def increase_hits_value_cache(story_id): """ +1 hits value of story cache and commit to DB periodically Parameter: story ID """ hits = cache.get('story:' + str(story_id) + ':hits') if hits == None: hits = update_hits_value_cache(story_id) hits = int(hits) + 1 cache.set('story:' + str(story_id) + ':hits', str(hits), timeout=CACHE_TTL) if hits % HITS_TO_COMMIT == 0: story = api.get_story_object(int(story_id)) story.hits += HITS_TO_COMMIT story.save() # Initialize cache cache.set('story:' + str(story.id) + ':hits', '0', timeout=CACHE_TTL) update_story_cache(story.id, 'hits', story.hits) return None