journal/models.py: use stadard cache

This commit is contained in:
Christophe Siraut 2020-09-14 14:00:30 +02:00
parent bc5f0ca945
commit b84931938b
1 changed files with 13 additions and 5 deletions

View File

@ -1,7 +1,9 @@
import random
from django.db import models
from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.fields.jsonb import KeyTextTransform
from django.utils.functional import cached_property
from django.core.cache import cache
class EntryManager(models.Manager):
@ -22,13 +24,19 @@ class EntryManager(models.Manager):
options['data__' + k.upper() + '__%s' % mode] = v
return options
@cached_property
def hosts(self):
return self.model.objects.all().values_list('host', flat=True).distinct()
value = cache.get('hosts')
if value is None:
value = self.model.objects.all().values_list('host', flat=True).distinct()
cache.set('hosts', value, 6000 * random.random())
return value
@cached_property
def units(self):
return self.model.objects.all().annotate(unit=KeyTextTransform('_SYSTEMD_UNIT', 'data')).values_list('unit', flat=True).distinct()
value = cache.get('units')
if value is None:
value = self.model.objects.all().annotate(unit=KeyTextTransform('_SYSTEMD_UNIT', 'data')).values_list('unit', flat=True).distinct()
cache.set('units', value, 600 * random.random())
return value
class Entry(models.Model):