Skip to content

Commit

Permalink
fix silly cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-HAP committed Jul 14, 2020
1 parent e48b800 commit 6cb4676
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions workshop/03-defensive-programming/01-lookup-with-cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
import psycopg2
from psycopg2.extras import NamedTupleCursor

CACHE_EXISTS = set()
CACHE_NOT_EXISTS = set()
CACHE = {}

def connect(db_url):
return psycopg2.connect(db_url, cursor_factory=NamedTupleCursor)

def lookup_telno(conn, telno):
if telno in CACHE_EXISTS:
return 1, 1
elif telno in CACHE_NOT_EXISTS:
return 0, 1
if telno in CACHE:
return CACHE[telno], 1
else:
sql = """
select exists
Expand All @@ -28,13 +25,9 @@ def lookup_telno(conn, telno):
with conn.cursor() as cur:
cur.execute(sql, [telno])
res = cur.fetchone()

if res and res.telno_exists:
CACHE_EXISTS.add(telno)
return 1, 0
else:
CACHE_NOT_EXISTS.add(telno)
return 0, 0

CACHE[telno] = (res and res.telno_exists)
return CACHE[telno], 0

def validate_telnos(conn):
cache_hits = 0
Expand Down

0 comments on commit 6cb4676

Please sign in to comment.