Skip to content

Commit

Permalink
Add shared caching to get_users (#924)
Browse files Browse the repository at this point in the history
* Add shared caching to get_users

* Polish

* Fix typo
  • Loading branch information
piazzatron committed Oct 13, 2020
1 parent eef638a commit 86a67dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
3 changes: 1 addition & 2 deletions discovery-provider/src/queries/get_playlists.py
Expand Up @@ -78,8 +78,7 @@ def get_unpopulated_playlists():
if current_user_id:
playlists = list(filter(
lambda playlist: (not playlist["is_private"]) or playlist["playlist_owner_id"] == current_user_id,
playlists)
)
playlists))

# retrieve playlist ids list
playlist_ids = list(map(lambda playlist: playlist["playlist_id"], playlists))
Expand Down
1 change: 0 additions & 1 deletion discovery-provider/src/queries/get_tracks.py
@@ -1,6 +1,5 @@
import logging # pylint: disable=C0302

from flask.globals import request
from src.models import AggregatePlays, Track, User
from src.utils import helpers
from src.utils.db_session import get_db_read_replica
Expand Down
47 changes: 17 additions & 30 deletions discovery-provider/src/queries/get_users.py
@@ -1,45 +1,36 @@
import logging # pylint: disable=C0302
from sqlalchemy import asc

from flask.globals import request
from src import exceptions
from src.models import User
from src.utils import helpers
from src.utils.db_session import get_db_read_replica
from src.queries.query_helpers import populate_user_metadata, paginate_query
from src.utils.redis_cache import extract_key, use_redis_cache
from src.queries.get_unpopulated_users import get_unpopulated_users

logger = logging.getLogger(__name__)

UNPOPULATED_USER_CACHE_DURATION_SEC = 10

def make_cache_key(args):
cache_keys = {}

if args.get("id"):
ids = args.get("id")
ids = map(str, ids)
ids = ",".join(ids)
cache_keys["user_id"] = ids

if args.get("handle"):
handle = args.get("handle")
cache_keys["handle"] = handle

if args.get("wallet"):
wallet = args.get("wallet")
cache_keys["wallet"] = wallet

key = extract_key(f"unpopulated-user:{request.path}", cache_keys.items())
return key

def get_users(args):
users = []
current_user_id = args.get("current_user_id")

db = get_db_read_replica()
with db.scoped_session() as session:
def get_unpopulated_users():
def get_users_and_ids():

can_use_shared_cache = (
"id" in args and
"is_creator" not in args and
"wallet" not in args and
"min_block_number" not in args and
"handle" not in args
)

if can_use_shared_cache:
users = get_unpopulated_users(session, args.get("id"))
ids = list(map(lambda user: user["user_id"], users))
return (users, ids)

# Create initial query
base_query = session.query(User)
# Don't return the user if they have no wallet or handle (user creation did not finish properly on chain)
Expand Down Expand Up @@ -80,11 +71,7 @@ def get_unpopulated_users():

return (users, user_ids)

key = make_cache_key(args)
(users, user_ids) = use_redis_cache(
key,
UNPOPULATED_USER_CACHE_DURATION_SEC,
get_unpopulated_users)
(users, user_ids) = get_users_and_ids()

# bundle peripheral info into user results
users = populate_user_metadata(
Expand Down

0 comments on commit 86a67dc

Please sign in to comment.