/
conf.py
70 lines (56 loc) · 1.89 KB
/
conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
from copy import deepcopy
import redis
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
profile_defaults = {
'ops': (),
'local_get': False
}
profiles = {
'just_enable': {},
'all': {'ops': ('get', 'fetch', 'count')},
'get': {'ops': ('get',)},
'count': {'ops': ('count',)},
}
for key in profiles:
profiles[key] = dict(profile_defaults, **profiles[key])
# Connecting to redis
try:
redis_conf = settings.CACHEOPS_REDIS
except AttributeError:
raise ImproperlyConfigured('You must specify non-empty CACHEOPS_REDIS setting to use cacheops')
redis_client = redis.Redis(**redis_conf)
model_profiles = {}
def prepare_profiles():
"""
Prepares a dict 'app.model' -> profile, for use in model_profile()
"""
if hasattr(settings, 'CACHEOPS_PROFILES'):
profiles.update(settings.CACHEOPS_PROFILES)
ops = getattr(settings, 'CACHEOPS', {})
for app_model, profile in ops.items():
profile_name, timeout = profile[:2]
try:
model_profiles[app_model] = mp = deepcopy(profiles[profile_name])
except KeyError:
raise ImproperlyConfigured('Unknown cacheops profile "%s"' % profile_name)
if len(profile) > 2:
mp.update(profile[2])
mp['timeout'] = timeout
mp['ops'] = set(mp['ops'])
if not model_profiles and not settings.DEBUG:
raise ImproperlyConfigured('You must specify non-empty CACHEOPS setting to use cacheops')
def model_profile(model):
"""
Returns cacheops profile for a model
"""
if not model_profiles:
prepare_profiles()
app, model_name = model._meta.app_label, model._meta.module_name
app_model = '%s.%s' % (app, model_name)
for guess in (app_model, '%s.*' % app, '*.*'):
if guess in model_profiles:
return model_profiles[guess]
else:
return None