kopia lustrzana https://github.com/cirospaciari/socketify.py
remove json stringify from caches examples
rodzic
4c7f868188
commit
98f855e46c
|
@ -1,5 +1,4 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
from .memory_cache import MemoryCache
|
from .memory_cache import MemoryCache
|
||||||
|
|
||||||
# 2 LEVEL CACHE (Redis to share amoung worker, Memory to be much faster)
|
# 2 LEVEL CACHE (Redis to share amoung worker, Memory to be much faster)
|
||||||
|
@ -50,10 +49,10 @@ class TwoLevelCache:
|
||||||
#always check cache first
|
#always check cache first
|
||||||
cached = self.get(key)
|
cached = self.get(key)
|
||||||
if cached != None:
|
if cached != None:
|
||||||
return json.loads(cached)
|
return cached
|
||||||
result = await executor(*args)
|
result = await executor(*args)
|
||||||
if result != None:
|
if result != None:
|
||||||
self.set(key, json.dumps(result))
|
self.set(key, result)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
# the lock wasn't acquired
|
# the lock wasn't acquired
|
||||||
pass
|
pass
|
||||||
|
@ -67,5 +66,5 @@ class TwoLevelCache:
|
||||||
if result == None:
|
if result == None:
|
||||||
cache = self.get(key)
|
cache = self.get(key)
|
||||||
if cache != None:
|
if cache != None:
|
||||||
return json.loads(cache)
|
return cache
|
||||||
return result
|
return result
|
|
@ -2,7 +2,6 @@ from socketify import App
|
||||||
import redis
|
import redis
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
from helpers.twolevel_cache import TwoLevelCache
|
from helpers.twolevel_cache import TwoLevelCache
|
||||||
|
|
||||||
#create redis poll + connections
|
#create redis poll + connections
|
||||||
|
@ -20,13 +19,17 @@ async def get_pokemon(number):
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(f'https://pokeapi.co/api/v2/pokemon/{number}') as response:
|
async with session.get(f'https://pokeapi.co/api/v2/pokemon/{number}') as response:
|
||||||
pokemon = await response.text()
|
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 def get_original_pokemons():
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(f'https://pokeapi.co/api/v2/pokemon?limit=151') as response:
|
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()
|
pokemons = await response.text()
|
||||||
return json.loads(pokemons)
|
return pokemons
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
Ładowanie…
Reference in New Issue