Skip to content

Commit d561ac9

Browse files
committed
fix: cache
use per-view cache and invalidate cache when model chnages using admin
1 parent d7bccd5 commit d561ac9

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
worker_class = "gevent"
55
workers = int(os.getenv("WORKERS", "4"))
66
bind = [
7-
f'{os.environ.get("HTTP_ADDR", "0.0.0.0")}:{os.environ.get("HTTP_PORT", "8080")}'
7+
f'{os.environ.get("HTTP_ADDR", "0.0.0.0")}:{os.environ.get("HTTP_PORT", "8000")}'
88
]
99
daemon = os.environ.get("DEAMON_RUNNING", False)
1010
timeout = os.environ.get("TIMEOUT", 60)

controller/sentry/admin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from controller.sentry.forms import BumpForm
1515
from controller.sentry.models import App
16+
from controller.sentry.utils import invalidate_cache
1617

1718

1819
@admin.register(App)
@@ -86,3 +87,7 @@ def bump(self, request, queryset, form: BumpForm = None):
8687
active_sample_rate=form.cleaned_data["new_sample_rate"],
8788
active_window_end=new_date,
8889
)
90+
91+
def save_model(self, request, obj, form, change) -> None:
92+
invalidate_cache(f"/sentry/apps/{obj.reference}/")
93+
return super().save_model(request, obj, form, change)

controller/sentry/utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from django.conf import settings
2+
from django.core.cache import cache
3+
from django.http import HttpRequest
4+
from django.utils.cache import get_cache_key
5+
6+
7+
def invalidate_cache(path=""):
8+
"""this function uses Django's caching function get_cache_key(). Since 1.7,
9+
Django has used more variables from the request object (scheme, host,
10+
path, and query string) in order to create the MD5 hashed part of the
11+
cache_key. Additionally, Django will use your server's timezone and
12+
language as properties as well. If internationalization is important to
13+
your application, you will most likely need to adapt this function to
14+
handle that appropriately.
15+
"""
16+
17+
# Bootstrap request:
18+
# request.path should point to the view endpoint you want to invalidate
19+
# request.META must include the correct SERVER_NAME and SERVER_PORT as django uses these in order
20+
# to build a MD5 hashed value for the cache_key. Similarly, we need to artificially set the
21+
# language code on the request to 'en-us' to match the initial creation of the cache_key.
22+
# YMMV regarding the language code.
23+
request = HttpRequest()
24+
request.META = settings.CACHE_META_INVALIDATION
25+
request.LANGUAGE_CODE = "en-us"
26+
request.path = path
27+
28+
try:
29+
cache_key = get_cache_key(request)
30+
if cache_key:
31+
if cache_key in cache:
32+
cache.delete(cache_key)
33+
return (True, "successfully invalidated")
34+
else:
35+
return (False, "cache_key does not exist in cache")
36+
else:
37+
raise ValueError("failed to create cache_key")
38+
except (ValueError, Exception) as e:
39+
return (False, e)

controller/settings.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@
5050
MIDDLEWARE = [
5151
"django.middleware.security.SecurityMiddleware",
5252
"django.contrib.sessions.middleware.SessionMiddleware",
53-
"django.middleware.cache.UpdateCacheMiddleware",
5453
"django.middleware.common.CommonMiddleware",
55-
"django.middleware.cache.FetchFromCacheMiddleware",
5654
"django.middleware.csrf.CsrfViewMiddleware",
5755
"django.contrib.auth.middleware.AuthenticationMiddleware",
5856
"django.contrib.messages.middleware.MessageMiddleware",
@@ -135,28 +133,35 @@
135133

136134
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
137135

136+
CONN_MAX_AGE = None
137+
APP_CACHE_TIMEOUT = 0
138138

139139
if not DEBUG:
140+
APP_CACHE_TIMEOUT = int(os.getenv("APP_CACHE_TIMEOUT", "600"))
140141
CACHES = {
141142
"default": {
142143
"BACKEND": "django.core.cache.backends.redis.RedisCache",
143144
"LOCATION": os.getenv("CACHE_REDIS_URL", "redis://127.0.0.1:6379"),
144145
"OPTIONS": {
145-
"db": "10",
146146
"parser_class": "redis.connection.PythonParser",
147147
"pool_class": "redis.BlockingConnectionPool",
148148
},
149149
"TIMEOUT": int(os.getenv("CACHE_TIMEOUT", "120")),
150150
}
151151
}
152152

153-
CONN_MAX_AGE = None
154153

155-
APP_CACHE_TIMEOUT = int(os.getenv("APP_CACHE_TIMEOUT", "600"))
156154
DEFAULT_SAMPLE_RATE = float(os.getenv("DEFAULT_SAMPLE_RATE", "0.1"))
157155
DEFAULT_WSGI_IGNORE_PATHS = os.getenv(
158156
"DEFAULT_WSGI_IGNORE_PATHS", "/health,/healthz,/health/,/healthz/"
159157
).split(",")
160158

161159

162160
DEFAULT_CELERY_IGNORE_TASKS = []
161+
162+
163+
CACHE_META_INVALIDATION = {
164+
"SERVER_NAME": os.getenv("CACHE_META_SERVER_NAME", "localhost"),
165+
"SERVER_PORT": int(os.getenv("CACHE_META_SERVER_PORT", "8000")),
166+
"HTTP_ACCEPT": os.getenv("CACHE_META_HTTP_ACCEPT", "*/*"),
167+
}

0 commit comments

Comments
 (0)