mirror of
https://github.com/kennethreitz/flask-common.git
synced 2026-06-05 14:50:17 +00:00
Merge pull request #5 from riquellopes/master
Support to define cache type
This commit is contained in:
@@ -61,3 +61,7 @@ Flask-Common automatically configures [WhiteNoise](http://whitenoise.evans.io) t
|
||||
app.config['COMMON_POWERED_BY_DISABLED'] = 1
|
||||
app.config['COMMON_PROCESSED_TIME_DISABLED'] = 1
|
||||
|
||||
## Cache configures
|
||||
By default *simple* is a default cache type. But can you change this options, following below steps. [Flask-Cache](https://pythonhosted.org/Flask-Cache/#configuring-flask-cache)
|
||||
|
||||
app.config['COMMON_CACHE_TYPE'] = "simple"
|
||||
|
||||
+12
-10
@@ -37,11 +37,12 @@ except ImportError:
|
||||
# --------------
|
||||
|
||||
def number_of_gunicorn_workers():
|
||||
if not 'WEB_CONCURRENCY' in os.environ:
|
||||
if 'WEB_CONCURRENCY' not in os.environ:
|
||||
return (multiprocessing.cpu_count() * 2) + 1
|
||||
else:
|
||||
return os.environ['WEB_CONCURRENCY']
|
||||
|
||||
|
||||
class WSGIApp(Application):
|
||||
|
||||
def __init__(self, application, options={}):
|
||||
@@ -66,11 +67,12 @@ class WSGIApp(Application):
|
||||
def load(self):
|
||||
""" Attempt an import of the specified application """
|
||||
|
||||
if isinstance(self.application,str):
|
||||
if isinstance(self.application, str):
|
||||
return util.import_app(self.application)
|
||||
else:
|
||||
return self.application
|
||||
|
||||
|
||||
class GunicornServer(object):
|
||||
|
||||
def __init__(self, app, **options):
|
||||
@@ -105,13 +107,13 @@ class Common(object):
|
||||
app.extensions['common'] = self
|
||||
self.app = app
|
||||
|
||||
if not 'COMMON_FILESERVER_DISABLED' in app.config:
|
||||
with app.test_request_context() as c:
|
||||
if 'COMMON_FILESERVER_DISABLED' not in app.config:
|
||||
with app.test_request_context():
|
||||
|
||||
# Configure WhiteNoise.
|
||||
app.wsgi_app = WhiteNoise(app.wsgi_app, root=url_for('static', filename='')[1:])
|
||||
|
||||
self.cache = Cache(app, config={'CACHE_TYPE': 'simple'})
|
||||
self.cache = Cache(app, config={'CACHE_TYPE': app.config.get("COMMON_CACHE_TYPE", 'simple')})
|
||||
|
||||
@app.before_request
|
||||
def before_request_callback():
|
||||
@@ -119,9 +121,9 @@ class Common(object):
|
||||
|
||||
@app.after_request
|
||||
def after_request_callback(response):
|
||||
if not 'COMMON_POWERED_BY_DISABLED' in current_app.config:
|
||||
if 'COMMON_POWERED_BY_DISABLED' not in current_app.config:
|
||||
response.headers['X-Powered-By'] = 'Flask'
|
||||
if not 'COMMON_PROCESSED_TIME_DISABLED' in current_app.config:
|
||||
if 'COMMON_PROCESSED_TIME_DISABLED' not in current_app.config:
|
||||
response.headers['X-Processed-Time'] = maya.now().epoch - request.start_time.epoch
|
||||
return response
|
||||
|
||||
@@ -129,7 +131,6 @@ class Common(object):
|
||||
def favicon():
|
||||
return redirect(url_for('static', filename='favicon.ico'), code=301)
|
||||
|
||||
|
||||
def serve(self, workers=None, **kwargs):
|
||||
"""Serves the Flask application."""
|
||||
if self.app.debug:
|
||||
@@ -140,6 +141,7 @@ class Common(object):
|
||||
print(crayons.yellow('Booting Gunicorn...'))
|
||||
|
||||
# Start the web server.
|
||||
server = GunicornServer(self.app, workers=workers or number_of_gunicorn_workers(), worker_class='egg:meinheld#gunicorn_worker', **kwargs)
|
||||
server = GunicornServer(
|
||||
self.app, workers=workers or number_of_gunicorn_workers(),
|
||||
worker_class='egg:meinheld#gunicorn_worker', **kwargs)
|
||||
server.run()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user