Skip to content

Commit

Permalink
Bug 1851253 - adjust cache code to support two backends in the future…
Browse files Browse the repository at this point in the history
…. r=aryx (#7837)
  • Loading branch information
jmaher committed Oct 12, 2023
1 parent be68fca commit 2b28f7e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import responses
from celery import current_app
from django.core.cache import cache
from django.core.cache import caches
from django.core.management import call_command

from treeherder.utils.http import fetch_text
Expand All @@ -28,6 +28,8 @@ def test_no_missing_migrations():

def test_django_cache():
"""Test the Django cache backend & associated server are properly set up."""
cache = caches['default']

k, v = 'my_key', 'my_value'
cache.set(k, v, 10)
assert cache.get(k) == v
Expand Down
8 changes: 5 additions & 3 deletions treeherder/model/error_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import newrelic.agent

from django.core.cache import cache
from django.core.cache import caches

from treeherder.model.models import Bugscache, TextLogError

Expand Down Expand Up @@ -32,6 +32,8 @@ def get_error_summary(job, queryset=None):
Caches the results if there are any.
"""
db_cache = caches['default']
cache = caches['default']
cache_key = 'error-summary-{}'.format(job.id)
cached_error_summary = cache.get(cache_key)
if cached_error_summary is not None:
Expand All @@ -41,7 +43,7 @@ def get_error_summary(job, queryset=None):
line_cache_key = 'mc_error_lines'
if job.repository == "comm-central":
line_cache_key = 'cc_error_lines'
line_cache = cache.get(line_cache_key)
line_cache = db_cache.get(line_cache_key)
if line_cache is None:
line_cache = {str(job.submit_time.date()): {}}
else:
Expand Down Expand Up @@ -83,7 +85,7 @@ def get_error_summary(job, queryset=None):
logger.error('error caching error_summary for job %s: %s', job.id, e, exc_info=True)

try:
cache.set(line_cache_key, line_cache, LINE_CACHE_TIMEOUT)
db_cache.set(line_cache_key, line_cache, LINE_CACHE_TIMEOUT)
except Exception as e:
newrelic.agent.record_custom_event('error caching error_lines for job', job.id)
logger.error('error caching error_lines for job %s: %s', job.id, e, exc_info=True)
Expand Down
7 changes: 4 additions & 3 deletions treeherder/webapp/api/note.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.cache import cache
from django.core.cache import caches

from rest_framework import viewsets
from rest_framework.decorators import action
Expand Down Expand Up @@ -93,7 +93,8 @@ def create(self, request, project):
if fc_id == 2: # this is for fixed_by_commit (backout | follow_up_commit)
# remove cached failure line counts
line_cache_key = 'error_lines'
line_cache = cache.get(line_cache_key)
db_cache = caches['default']
line_cache = db_cache.get(line_cache_key)
date = current_job.submit_time.date().isoformat()
if line_cache and date in line_cache.keys():
for err in TextLogError.objects.filter(job=current_job):
Expand All @@ -107,7 +108,7 @@ def create(self, request, project):
):
del line_cache[date]["new_lines"][cache_clean_line]
try:
cache.set(line_cache_key, line_cache, LINE_CACHE_TIMEOUT)
db_cache.set(line_cache_key, line_cache, LINE_CACHE_TIMEOUT)
except Exception as e:
logger.error(
'error caching error_lines for job %s: %s',
Expand Down

0 comments on commit 2b28f7e

Please sign in to comment.