Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/postgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from psycopg2.extras import NamedTupleCursor
import sentry_sdk

from cache import CompressedJSONCache
from overpass import ZOOM_DEFAULT


Expand All @@ -23,11 +24,17 @@ class PostgisClient:
The server is assumed to already be populated, including having the
soundscape_tile function installed.
"""
def __init__(self, server):
def __init__(self, server, cache_dir, cache_days, cache_size):
self.server = server
self.cache = CompressedJSONCache(cache_dir, cache_days, cache_size)

@sentry_sdk.trace
async def query(self, x, y):
response = await self.cache.get(f"{x}_{y}", lambda: self.uncached_query(x, y))
return response

@sentry_sdk.trace
async def uncached_query(self, x, y):
try:
async with aiopg.connect(self.server) as conn:
async with conn.cursor(cursor_factory=NamedTupleCursor) as cursor:
Expand Down
4 changes: 2 additions & 2 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
logger=logging.getLogger(__name__)

# workaround for aiohttp on WIndows (https://stackoverflow.com/a/69195609)
# workaround for aiohttp on Windows (https://stackoverflow.com/a/69195609)
import sys, asyncio
if sys.version_info >= (3, 8) and sys.platform.lower().startswith("win"):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Expand Down Expand Up @@ -53,7 +53,7 @@ def backend_client(backend_url, user_agent, cache_dir, cache_days, cache_size):
backend_url, user_agent, cache_dir, cache_days, cache_size
)
elif url_parts.scheme in ('postgis', 'postgres'):
return PostgisClient(backend_url)
return PostgisClient(backend_url, cache_dir, cache_days, cache_size)
else:
raise ValueError("Unrecognized protocol %r" % url_parts.scheme)

Expand Down