From 98f855e46c9f39f090d0e33322ffe9e85f5a59c0 Mon Sep 17 00:00:00 2001 From: Ciro Date: Thu, 2 Jun 2022 17:59:33 -0300 Subject: [PATCH] remove json stringify from caches examples --- tests/examples/helpers/twolevel_cache.py | 7 +++---- tests/examples/http_request_cache.py | 9 ++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/examples/helpers/twolevel_cache.py b/tests/examples/helpers/twolevel_cache.py index 9927fd9..a526b37 100644 --- a/tests/examples/helpers/twolevel_cache.py +++ b/tests/examples/helpers/twolevel_cache.py @@ -1,5 +1,4 @@ import asyncio -import json from .memory_cache import MemoryCache # 2 LEVEL CACHE (Redis to share amoung worker, Memory to be much faster) @@ -50,10 +49,10 @@ class TwoLevelCache: #always check cache first cached = self.get(key) if cached != None: - return json.loads(cached) + return cached result = await executor(*args) if result != None: - self.set(key, json.dumps(result)) + self.set(key, result) except Exception as err: # the lock wasn't acquired pass @@ -67,5 +66,5 @@ class TwoLevelCache: if result == None: cache = self.get(key) if cache != None: - return json.loads(cache) + return cache return result \ No newline at end of file diff --git a/tests/examples/http_request_cache.py b/tests/examples/http_request_cache.py index ad22b51..d747a6e 100644 --- a/tests/examples/http_request_cache.py +++ b/tests/examples/http_request_cache.py @@ -2,7 +2,6 @@ from socketify import App import redis import aiohttp import asyncio -import json from helpers.twolevel_cache import TwoLevelCache #create redis poll + connections @@ -20,13 +19,17 @@ async def get_pokemon(number): async with aiohttp.ClientSession() as session: async with session.get(f'https://pokeapi.co/api/v2/pokemon/{number}') as response: pokemon = await response.text() - return json.loads(pokemon) + #cache only works with strings/bytes/buffer + #we will not change nothing here so no needs to parse json + return pokemon async def get_original_pokemons(): async with aiohttp.ClientSession() as session: async with session.get(f'https://pokeapi.co/api/v2/pokemon?limit=151') as response: + #cache only works with strings/bytes/buffer + #we will not change nothing here so no needs to parse json pokemons = await response.text() - return json.loads(pokemons) + return pokemons ###