1

I have a very simple Django class:

from django.db import models

class MyClass(models.Model):
    a = models.IntegerField()
    b = models.IntegerField()

    def __str__(self):
        return "MyClass #%s: a: %s, b %s" % (self.pk, self.a, self.b)

This class works fine when I perform CRUD-type operations:

>>> from statistics.models import MyClass
>>> print MyClass.objects.all()
[]
>>> x = MyClass(a=6, b=10)
>>> print MyClass.objects.all()
[]
>>> print x
MyClass #None: a: 6, b 10
>>> x.save()
>>> print MyClass.objects.all()
[<MyClass: MyClass #2: a: 6, b 10>]
>>>

Now I add/modify 3 lines to the class definition to enable the CacheMachine:

from caching.base import CachingManager, CachingMixin  # This line added
from django.db import models

class MyClass(CachingMixin, models.Model):             # Added a Mix-in
    a = models.IntegerField()
    b = models.IntegerField()
    objects = CachingManager()                         # This line added

    def __str__(self):
        return "MyClass #%s: a: %s, b %s" % (self.pk, self.a, self.b)

After manage.py makemigrations and manage.py migrate, I truncate the database, flush memcached and run the same experiment I ran before.

However that experiment fails! After saving the newly created MyClass instance, querying the database shows it is not there. Why? How to workaround this issue? Has anyone else seen this?

>>> print MyClass.objects.all()
[]
>>> x = MyClass(a=6, b=10)
>>> print MyClass.objects.all()
[]
>>> print x
MyClass #None: a: 6, b 10
>>> x.save()
>>> print MyClass.objects.all()
[]

FYI, I ran the tests for Django Cache Machine shown here. They all passed.

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • 1
    Looks like it might be identical to this open bug in django-cache-machine : https://github.com/jbalogh/django-cache-machine/issues/62 – Håken Lid Feb 07 '15 at 09:25
  • Wow. That's a pretty huge bug that is not addressed. It makes the entire DjangoCacheMachine nearly useless. Do you know if there is a known workaround? – Saqib Ali Feb 07 '15 at 22:45
  • 1
    I don't know anything about cache-machine, I just found this after search of the github issue tracker. I would encourage you to post a bug report to the github repo with details to help the contributors reproduce the issue. It's an easy and meaningful way of giving something back, and might contribute to getting this fixed in a upcoming release. – Håken Lid Feb 08 '15 at 11:58

2 Answers2

1

Are you sure that you are running this test against my branch? This was the problem that I was having (similar) and was solved with it.

Each save should invalidate all querysets related with the model. So in this case, after x.save() the object MyClass shouldn't have any info in your cache.

I've tested and works fine in my case, using Redis as nocache db.

asketsus
  • 96
  • 2
1

I've just tested my fork on a windows machine which I was now an check yourself:

Image check: http://oi61.tinypic.com/2w5jf9d.jpg

Github URL: https://github.com/asketsus/django-cache-machine

asketsus
  • 96
  • 2
  • Yes. THANK YOU!! I ran `pip install -e git://github.com/asketsus/django-cache-machine.git#egg=django-cache-machine` and It all seems to work fine now *except* I get the following error when I try to run `pip -r requirements.txt`: https://gist.github.com/syedsaqibali/03bbccd39adad31ee222. Any ideas why? – Saqib Ali Feb 08 '15 at 06:23
  • 2
    @SaqibAli: I've run into this as well. I solved it by manually adding `#egg=some-repo-name` to the relevant lines in requirements.txt. `pip freeze` did not do that for me, but pip install would not accept the github address with no `#egg=` – Håken Lid Feb 08 '15 at 11:46
  • @SaqibAli: your requirement.txt link requires google docs permission to access. I recommend instead using https://gist.github.com/ for this purpose. – Håken Lid Feb 08 '15 at 12:06
  • Thank you Haken, I will try this and report back to you shortly. Are u unable to access the Google doc link? It is an image (That's why I didn't use gist). The permissions on it are public. So everyone should be able to see it. I just tested it again browsing via Incognito mode in Chrome. It worked for me. BTW can `some-repo-name` be any string? If not, please let me know the exact value you used. – Saqib Ali Feb 08 '15 at 13:11
  • The solution was to remove the `/HEAD` at the end of the following line in requirements.txt: `-e git://github.com/asketsus/django-cache-machine.git@a26b302e119b469b94b0683b067755beaf678675#egg=django_cache_machine-origin/HEAD` – Saqib Ali Feb 08 '15 at 17:42