Пример #1
0
    def test_get_from_cache(self):
        if settings.ENABLE_CACHING:
            s = Student().blocking_save()

            cached_value = ""
            clean_value = s._data

            try:
                cached_value = cache.get(s.key)
            except:
                pass

            if six.PY3:
                cached_value = cached_value.decode()

            cached_value_d = json.loads(cached_value)

            for key in cached_value_d.keys():
                assert cached_value_d[key] == clean_value[key]


            # To make sure the data is read from cache while key is in it
            try:
                cache.set(s.key, "a")
                assert Student.objects.get(s.key) == "a"
            except:
                pass

            s.blocking_delete()

            try:
                assert not cache.get(s.key)
            except:
                pass
Пример #2
0
 def set_to_cache(key, value):
     try:
         if not value['deleted']:
             cache.set(key, value, settings.CACHE_EXPIRE_DURATION)
         else:
             cache.delete(key)
     except Exception as e:
         # todo should add log.error()
         pass
Пример #3
0
def test_clear_cache():
    cache.set("CSTMPRFX:someCustom_key",
              "SOME DUMMY VALUE WHICH HAS TEST PURPOSES")
    assert len(cache.keys("CSTMPRFX*")) == 1

    ManagementCommands(args=['clear_cache', '--prefix', 'CSTMPRFX'])
    assert len(cache.keys("CSTMPRFX*")) == 0

    ManagementCommands(args=['clear_cache', '--prefix', 'all'])
    assert len(cache.keys()) == 0
Пример #4
0
    def set_to_cache(vk):
        """
        Args:
            vk (tuple): obj data (dict), obj key(str)

        Return:
            tuple: value (dict), key (string)
        """
        v, k = vk

        try:
            cache.set(k, json.dumps(v), settings.CACHE_EXPIRE_DURATION)
        except Exception as e:
            pass
            # todo should add log.error()
        return v, k