journal/models.py: use stadard cache
This commit is contained in:
parent
bc5f0ca945
commit
b84931938b
|
@ -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):
|
||||
|
|
Reference in New Issue