Skip to content

Switch for turning caching on/off for /users/{userid} #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
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
36 changes: 22 additions & 14 deletions functions/HttpTriggerAPIUsersId/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import json
import redis

#Global value to be used to invalidate GET,PUT, and DELETE for Redis Cache
#Global value to be used to invalidate GET, PUT, and DELETE for Redis Cache
ALL_USERS_KEY = b'users:all'
CACHE_TOGGLE = os.environ["CACHE_TOGGLE"]

# This is the Http Trigger for Users/userId
# It connects to the db and retrives the users added to the db by userId
Expand Down Expand Up @@ -134,7 +135,7 @@ def get_user(conn, user_id, _redis):
'''
)

logging.info('Caching results...')
logging.info('Attempting to cache results...')

# Cache the results
cache_user(_redis, user)
Expand All @@ -160,25 +161,32 @@ def get_user(conn, user_id, _redis):


def get_user_cache(_redis):
try:
cache = _redis.get(ALL_USERS_KEY)
return cache
except TypeError as e:
logging.critical('Failed to fetch user from cache: ' + e.args[1])
return None
if CACHE_TOGGLE == "true":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you just remove the == "true" this will work better - sometimes it is True, sometimes true, in the Azure function app, but both of those will evaluate to True if you don't tell it to compare Strings. If that makes sense. So everywhere you have if CACHE_TOGGLE == "true": to just if CACHE_TOGGLE:

try:
cache = _redis.get(ALL_USERS_KEY)
return cache
except TypeError as e:
logging.critical('Failed to fetch user from cache: ' + e.args[1])
return None
else:
logging.info('Caching toggle is set to false. User data not from cache.')


def clear_cache(_redis):
_redis.delete(ALL_USERS_KEY)
logging.info("Cache cleared")


def cache_user(_redis, user):
try:
_redis.set(ALL_USERS_KEY, json.dumps(user), ex=1200)
logging.info('Caching complete')
except TypeError as e:
logging.info('Caching failed')
logging.info(e.args[0])
if CACHE_TOGGLE == "true":
try:
_redis.set(ALL_USERS_KEY, json.dumps(user), ex=1200)
logging.info('Caching complete')
except TypeError as e:
logging.info('Caching failed')
logging.info(e.args[0])
else:
logging.info('Caching toggle is set to false. User data not cached.')


def update_user(user_req_body, conn, user_id, _redis):
Expand Down