def get_cache_key(self): """Construct a cache key from genre + user + time""" request_items = to_dict(request.args) request_items.pop('limit', None) request_items.pop('offset', None) key = extract_key(request.path, request_items.items()) return key
def get_full_trending_playlists(request, args, strategy): offset, limit = format_offset(args), format_limit(args, TRENDING_LIMIT) current_user_id, time = args.get("user_id"), args.get("time", "week") time = "week" if time not in ["week", "month", "year"] else time # If we have a user_id, we call into `get_trending_playlist` # which fetches the cached unpopulated tracks and then # populates metadata. Otherwise, just # retrieve the last cached value. # # If current_user_id, # apply limit + offset inside the cached calculation. # Otherwise, apply it here. if current_user_id: args = { 'time': time, 'with_tracks': True, 'limit': limit, 'offset': offset } decoded = decode_string_id(current_user_id) args["current_user_id"] = decoded playlists = get_trending_playlists(args, strategy) else: args = { 'time': time, 'with_tracks': True, } key = get_trending_cache_key(to_dict(request.args), request.path) playlists = use_redis_cache( key, TRENDING_TTL_SEC, lambda: get_trending_playlists(args, strategy)) playlists = playlists[offset:limit + offset] return playlists
def get_full_recommended_tracks(request, args, strategy): # Attempt to use the cached tracks list if args["user_id"] is not None: full_recommended = get_recommended_tracks(args, strategy) else: key = get_trending_cache_key(to_dict(request.args), request.path) full_recommended = use_redis_cache( key, TRENDING_TTL_SEC, lambda: get_recommended_tracks(args, strategy)) return full_recommended
def get_full_trending(request, args, strategy): offset = format_offset(args) limit = format_limit(args, TRENDING_LIMIT) key = get_trending_cache_key(to_dict(request.args), request.path) # Attempt to use the cached tracks list if args['user_id'] is not None: full_trending = get_trending(args, strategy) else: full_trending = use_redis_cache(key, TRENDING_TTL_SEC, lambda: get_trending(args, strategy)) trending_tracks = full_trending[offset:limit + offset] return trending_tracks
def get_underground_trending(request, args, strategy): offset, limit = format_offset(args), format_limit(args, TRENDING_LIMIT) current_user_id = args.get("user_id") args = {'limit': limit, 'offset': offset} # If user ID, let _get_underground_trending # handle caching + limit + offset if current_user_id: decoded = decode_string_id(current_user_id) args["current_user_id"] = decoded trending = _get_underground_trending(args, strategy) else: # If no user ID, fetch all cached tracks # and perform pagination here, passing # no args so we get the full list of tracks. key = get_trending_cache_key(to_dict(request.args), request.path) trending = use_redis_cache( key, TRENDING_TTL_SEC, lambda: _get_underground_trending({}, strategy)) trending = trending[offset:limit + offset] return trending