Created
June 24, 2016 06:53
-
-
Save allanlei/65d36b4ea065bf164a52cab2ff45d12e to your computer and use it in GitHub Desktop.
Configuration helpers for flask-cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from six.moves import urllib | |
def config(uri): | |
parsed = urllib.parse.urlparse(uri) | |
options = dict(urllib.parse.parse_qsl(parsed.query)) | |
cache_type = parsed.scheme.lower() | |
if cache_type not in ['null', 'simple', 'file', 'memcached', 'saslmemcached', 'redis']: | |
raise Exception('Misconfiguration') | |
def _config(): | |
yield 'CACHE_TYPE', cache_type | |
if 'timeout' in options: | |
yield 'CACHE_DEFAULT_TIMEOUT', int(options['timeout']) | |
if 'prefix' in options: | |
yield 'CACHE_KEY_PREFIX', options['prefix'] | |
if cache_type in ['simple', 'file']: | |
if 'threshold' in options: | |
yield 'CACHE_THRESHOLD', int(options['threshold']) | |
if cache_type in ['file']: | |
yield 'CACHE_DIR', parsed.path[1:] | |
if cache_type in ['memcached', 'saslmemcached']: | |
yield 'CACHE_MEMCACHED_SERVERS', parsed.host | |
if cache_type in ['saslmemcached']: | |
yield 'CACHE_MEMCACHED_USERNAME', parsed.username | |
yield 'CACHE_MEMCACHED_PASSWORD', parsed.password | |
if cache_type in ['redis']: | |
yield 'CACHE_REDIS_HOST', parsed.hostname | |
if parsed.path: | |
yield 'CACHE_REDIS_DB', int(parsed.path[1:]) | |
if parsed.port: | |
yield 'CACHE_REDIS_PORT', parsed.port | |
if parsed.password: | |
yield 'CACHE_REDIS_PASSWORD', parsed.password | |
return dict(_config()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment