On Sat, Jul 18, 2009 at 6:21 AM, Andrew Fong<fongand...@gmail.com> wrote:
>
> I'm playing around with some low-level caching of model instances.
> During testing, it's necessary for me to flush out the cache so cached
> whatnot from one test doesn't pollute another.
>
> To my knowledge, Django's currently doesn't offer support for
> resetting the cache after each testcase in the same way it does for
> the test database (via the custom testcase class).
>
> The options I'm looking at:
>> Manually (re)starting a separate cache process
>> Adding a prefix to my cache keys and changing them for each test case
>
> Ideally, I'd just like to flush the cache but after poking around in
> Django's caching code, I haven't seen any explicit support for
> flushing. Does anyone have any ideas?

You're correct that there isn't any explicit support for cache
flushing in the Django cache API. However, you can manually flush the
cache - the technique just depends on the cache backend you are using.
In most cases, you will be able to fall back to the capabilities of
the underlying cache implementation. For example, to flush memcache,
you can use the capabilities of the underlying cache object:

from django.core.cache import cache
cache._cache.flush_all()

If you're using locmem, you can just reset the dictionary in the cache object:

from django.core.cache import cache
self._cache = {}

Analogous solutions will exist for the other backends.

Another possibility is to use the dummy cache for testing - that is,
use the cache backend that doesn't actually cache. This will ensure
that you don't get any caching effects in your code. Of course, this
doesn't help if you're actually trying to test the caching behaviour.

However, your original point is valid. Caching is one of those things
that needs to be reset for testing purposes. It makes sense for
Django's cache backend to expose a flushing capability, and Django's
TestCase should be using these facilities. Please open a ticket to
make sure this idea isn't lost. If you take a swing at a patch (and
tests), I'll see about getting it into trunk in the v1.2 timeframe.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to