Wednesday, June 27, 2018

Re: Django's cache framework problem

Django's cached framework and @cached_property are two different things, though both has "cached" it doesn't mean that they share memory....


Read the docs.

林挺滨 <lintingbin31@gmail.com>于2018年6月28日周四 上午10:31写道:
class Log(models.Model):
type = models.CharField(max_length=200, unique=True)
column = SortedManyToManyField(Column, blank=True)
min_column_cnt = models.IntegerField(blank=True, default=0, help_text='0 equal NULL')

@cached_property
def column_list_name(self):
return [col.name for col in self.column.all()]

@cached_property
def column_len(self):
return len(self.column.all())

@cached_property
def get_all_column_checker(self):
column_ids = [c.id for c in self.column.all()]
return ColumnChecker.objects.select_related('column').filter(column_id__in=column_ids)

def get_log_attr(log_type):
global log_attr_dict, log_attr_last_update_ts
if (datetime.now() - log_attr_last_update_ts).total_seconds() < update_log_attr_interval and \
log_type in log_attr_dict:
return log_attr_dict[log_type]
log_attr_dict[log_type] = Log.objects.prefetch_related('column').get(type=log_type)
log_attr_last_update_ts = datetime.now()
return log_attr_dict[log_type]

Now I use module global var "log_attr_dict" to cache the Log instance, @cached_property of the instance is work.

def get_log_attr(log_type):
log_attr = cache.get(log_type)
if not log_attr:
log_attr = Log.objects.prefetch_related('column').get(type=log_type)
cache.set(log_type, log_attr, update_log_attr_interval)
return log_attr

But if I use cache api to cache the Log instance, @cache_property of the instance isn't work. (I use the local memory cache.)

赖信桃 <laixintaoo@gmail.com> 于2018年6月27日周三 下午9:07写道:
Show me your code snippet.

林挺滨 <lintingbin31@gmail.com>于2018年6月27日周三 下午8:44写道:
When I use cache framework api to cache a model instance,  @cached_property of the instance doesn't work. Why?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5b27fa47-c712-460f-a421-1eb1d41c764e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.


--
祝您  身体健康
         工作愉快!
                     
林挺滨

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGdZMg8v2mp%3DOiO3coBFypGZ5O%2B_FLC_5cfnK94CRXax0OHRYw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMv51WTfACztXVaoCv6GgdJBEBh8KKhdfZXHQRYw4d%2BBC_2rOQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment