def test_get_from_cache_called(self): self.assertEqual(cache.__contains__(self.address), True) location, is_from_cache = views.get_geocoding(self.address) self.assertEqual(cache.__contains__(self.address), True) self.assertNotEqual(location, None) self.assertEqual(is_from_cache, True)
def test_fetch_data_from_cache_city_list(self): self.assertEqual(cache.__contains__(self.full_address), False) location = views.city_match_from_cache(self.full_address) self.assertEqual(location.city, "delhi") self.assertEqual(cache.__contains__(self.full_address), False) cache.set(self.address, " ", timeout=0)
def send_new_message(self, message): message_data = { 'message_type': "message_notification", 'sender': message.sender.username, 'receiver': message.receiver.username, 'content': message.content, 'deleted': message.deleted, 'timestamp': str(message.timestamp) } # sending message to room async_to_sync(self.channel_layer.group_send)(self.room_group_name, { 'type': 'send_message', 'messages': message_data }) # checking if receiver is attached with the socket or not if cache.__contains__(message.receiver.username): # if attached print("\n\n", cache.get(message.receiver.username), "\n\n") if cache.get(message.receiver.username) != 'active_chat_user': async_to_sync(self.channel_layer.group_send)( message.receiver.username, { 'type': 'notify', 'notification': message_data })
def get_geocoding(location_name): location = Location() is_from_cache = False if cache.__contains__(location_name): is_from_cache = True location = cache.get(location_name) print("from cache: " + str(location)) else: location = city_match_from_cache(location_name) is_from_cache = True if location is None: # if query was not in the list of city in cache # get data from api is_from_cache = False location = get_from_api(location_name) cache.set(location_name, location, timeout=None) if location is None: # is data received from api has zero results, in case of invalid address return None, is_from_cache else: print("from api: " + str(location)) return location, is_from_cache
def details(request, id): if cache.__contains__(id): comment = Comments.objects.filter(post_id=id) #increment the view count of the blog inc_count = Posts.objects.filter(id=id).update( view_count=F("view_count") + 1) data_str = cache.get(id) data_dict = ast.literal_eval(data_str) class Object(object): pass a = Object() a.title = data_dict['title'] a.body = data_dict['body'] a.created_at = data_dict['created_at'] context = {'post': a, 'comments': comment} return render(request, 'posts/details.html', context) else: post = Posts.objects.get(id=id) print(str(post.created_at)) #(post.body) (post.created_at) comment = Comments.objects.filter(post_id=id) detail_dict = { "title": (post.title), "body": post.body, "created_at": str(post.created_at) } setkey = cache.set(id, json.dumps(detail_dict), timeout=CACHE_TTL) print(id) context = {'post': post, 'comments': comment} return render(request, 'posts/details.html', context)
def is_location_in_cache(location_name): if cache.__contains__(location_name): return True return False