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
7 changes: 4 additions & 3 deletions ch8/cache_aside/cache_aside.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sqlite3
import sys
from pathlib import Path

import redis
Expand All @@ -19,7 +18,7 @@ def get_quote(quote_id: str) -> str:
with sqlite3.connect(DB_PATH) as db:
cursor = db.cursor()
res = cursor.execute(
f"SELECT text FROM quotes WHERE id = {quote_id}"
f"SELECT text FROM quotes WHERE id = {quote_id}" # nosec
).fetchone()
if not res:
return "There was no quote stored matching that id!"
Expand All @@ -31,13 +30,15 @@ def get_quote(quote_id: str) -> str:

# Add to the cache
key = f"{CACHE_KEY_PREFIX}.{quote_id}"
cache.set(key, quote, ex=60)
cache.set(key, quote, ex=60) # type: ignore
out.append(f"Added TO CACHE, with key '{key}'")
else:
out.append(f"Got '{quote}' FROM CACHE")

if out:
return " - ".join(out)
else:
return ""


def main():
Expand Down
7 changes: 3 additions & 4 deletions ch8/cache_aside/populate_db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# pip install redis
# pip install faker
# pip3 install redis
# pip3 install faker

import sqlite3
import sys
from pathlib import Path
from random import randint

Expand Down Expand Up @@ -38,7 +37,7 @@ def add_quotes(quotes_list):
cursor = db.cursor()

for quote_text in quotes_list:
quote_id = randint(1, 100)
quote_id = randint(1, 100) # nosec
quote = (quote_id, quote_text)

cursor.execute(
Expand Down