Esempio n. 1
0
    def key_values(cls, *key_fields, **kwargs):
        """
        Get the set of unique values in the configuration table for the given
        key[s]. Calling cls.current(*value) for each value in the resulting
        list should always produce an entry, though any such entry may have
        enabled=False.

        Arguments:
            key_fields: The positional arguments are the KEY_FIELDS to return. For example if
                you had a course embargo configuration where each entry was keyed on (country,
                course), then you might want to know "What countries have embargoes configured?"
                with cls.key_values('country'), or "Which courses have country restrictions?"
                with cls.key_values('course'). You can also leave this unspecified for the
                default, which returns the distinct combinations of all keys.
            flat: If you pass flat=True as a kwarg, it has the same effect as in Django's
                'values_list' method: Instead of returning a list of lists, you'll get one list
                of values. This makes sense to use whenever there is only one key being queried.

        Return value:
            List of lists of each combination of keys found in the database.
            e.g. [("Italy", "course-v1:SomeX+some+2015"), ...] for the course embargo example
        """
        flat = kwargs.pop('flat', False)
        assert not kwargs, "'flat' is the only kwarg accepted"
        key_fields = key_fields or cls.KEY_FIELDS
        cache_key = cls.key_values_cache_key_name(*key_fields)
        cached_response = TieredCache.get_cached_response(cache_key)
        if cached_response.is_found:
            return cached_response.value

        values = list(
            cls.objects.values_list(*key_fields,
                                    flat=flat).order_by().distinct())
        TieredCache.set_all_tiers(cache_key, values, cls.cache_timeout)
        return values
Esempio n. 2
0
    def test_get_cached_response_django_cache_hit(self, mock_cache_get):
        mock_cache_get.return_value = EXPECTED_VALUE
        cached_response = TieredCache.get_cached_response(TEST_KEY)
        self.assertTrue(cached_response.is_found)
        self.assertEqual(cached_response.value, EXPECTED_VALUE)

        cached_response = self.request_cache.get_cached_response(TEST_KEY)
        self.assertTrue(
            cached_response.is_found,
            'Django cache hit should cache value in request cache.')
Esempio n. 3
0
    def test_get_cached_response_force_cache_miss(self, mock_cache_get):
        self.request_cache.set(SHOULD_FORCE_CACHE_MISS_KEY, True)
        mock_cache_get.return_value = EXPECTED_VALUE
        cached_response = TieredCache.get_cached_response(TEST_KEY)
        self.assertFalse(cached_response.is_found)

        cached_response = self.request_cache.get_cached_response(TEST_KEY)
        self.assertFalse(
            cached_response.is_found,
            'Forced Django cache miss should not cache value in request cache.'
        )
Esempio n. 4
0
    def current(cls, *args):
        """
        Return the active configuration entry, either from cache,
        from the database, or by creating a new empty entry (which is not
        persisted).
        """
        cache_key = cls.cache_key_name(*args)
        cached_response = TieredCache.get_cached_response(cache_key)
        if cached_response.is_found:
            return cached_response.value

        key_dict = dict(zip(cls.KEY_FIELDS, args))
        try:
            current = cls.objects.filter(
                **key_dict).order_by('-change_date')[0]
        except IndexError:
            current = cls(**key_dict)

        TieredCache.set_all_tiers(cache_key, current, cls.cache_timeout)
        return current
Esempio n. 5
0
    def get_routers(self, backend_name):
        """
        Bring active routers of a backend.

        A queryset for the active configuration entries only. Only useful if backend_name is passed.
        This function will return all active routers of a backend.
        """
        if not backend_name:
            return []

        cache_key = get_cache_key(namespace="event_routing_backends",
                                  resource=backend_name)
        cached_response = TieredCache.get_cached_response(cache_key)
        if cached_response.is_found:
            return cached_response.value

        current = self.current_set().filter(
            backend_name=backend_name, enabled=True).order_by('-change_date')
        TieredCache.set_all_tiers(cache_key, current, backend_cache_ttl())
        return current
Esempio n. 6
0
 def test_get_cached_response_request_cache_hit(self):
     self.request_cache.set(TEST_KEY, EXPECTED_VALUE)
     cached_response = TieredCache.get_cached_response(TEST_KEY)
     self.assertTrue(cached_response.is_found)
     self.assertEqual(cached_response.value, EXPECTED_VALUE)
Esempio n. 7
0
 def test_get_cached_response_all_tier_miss(self):
     cached_response = TieredCache.get_cached_response(TEST_KEY)
     self.assertFalse(cached_response.is_found)