Last active
August 29, 2015 14:05
-
-
Save shuxiang/710f2b57ba6bc830f839 to your computer and use it in GitHub Desktop.
easy ini config 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
import os | |
import os.path | |
import time | |
from ConfigParser import SafeConfigParser | |
from django.conf import settings | |
__all__ = ['get_config'] | |
_CONFIG_MAP = {} | |
def get_config(path): | |
global _CONFIG_MAP | |
if not path in _CONFIG_MAP: | |
_CONFIG_MAP[path] = [None, time.time()] | |
lconfig = _CONFIG_MAP[path] | |
mtime = os.path.getmtime(path) | |
if mtime > lconfig[1] or not lconfig[0]: | |
lconfig[1] = mtime | |
with file(path, 'r') as f: | |
lconfig[0] = SafeConfigParser() | |
lconfig[0].readfp(f) | |
return lconfig[0] | |
# #django example | |
# some_config = lambda:get_config(settings.BASE_PATH + '/conf/%s' % settings.SOME_CONF) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment