Skip to content

Instantly share code, notes, and snippets.

@theRealNG
Last active July 5, 2023 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theRealNG/61d6ea3189dd1c3247de097c272c58d6 to your computer and use it in GitHub Desktop.
Save theRealNG/61d6ea3189dd1c3247de097c272c58d6 to your computer and use it in GitHub Desktop.
Flask & Redis python
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
# Retrieve the value of hits from redis cache
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment