[Django]-Django API caching, how to check it's setup correctly

4👍

Just write and run a simple test to check that your resources cache is working:

from django.test import TestCase
from tastypie.cache import SimpleCache


class CacheTestCase(TestCase):
    cache_name = 'resources'

    def test_cache(self):
        simple_cache = SimpleCache(cache_name=self.cache_name)
        simple_cache.set('foo', 'bar', 60)
        simple_cache.set('moof', 'baz', 1)

        self.assertEqual(simple_cache.get('foo'), 'bar')
        self.assertEqual(simple_cache.get('moof'), 'baz')
        self.assertEqual(simple_cache.get(''), None)

Partially taken from the tastypie tests with some modifications.

👤alecxe

0👍

Given you followed the cache docs to the letter And tastypie has an extensive testsuite When their CI builds succeed Then I would expect it to work.

To double check you could:

Pick a resource you think should be cached and pound its URL with a tool like ab. If your config is right, your database load shouldn’t go up. You could put this in a last step of your deployment scripts together with the other tests that test your whole stack.

Or put it in a tiny test in your project, like alecxe suggests, that just asserts that your configured cache is indeed used.

Leave a comment