Python django.conf.settings.CACHES Examples

The following are 30 code examples for showing how to use django.conf.settings.CACHES(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.conf.settings , or try the search function .

Example 1
Project: coursys   Author: sfu-fas   File: panel.py    License: GNU General Public License v3.0 6 votes vote down vote up
def settings_info():
    info = []
    info.append(('Deploy mode', settings.DEPLOY_MODE))
    info.append(('Database engine', settings.DATABASES['default']['ENGINE']))
    info.append(('Authentication Backends', settings.AUTHENTICATION_BACKENDS))
    info.append(('Cache backend', settings.CACHES['default']['BACKEND']))
    info.append(('Haystack engine', settings.HAYSTACK_CONNECTIONS['default']['ENGINE']))
    info.append(('Email backend', settings.EMAIL_BACKEND))
    if hasattr(settings, 'CELERY_EMAIL') and settings.CELERY_EMAIL:
        info.append(('Celery email backend', settings.CELERY_EMAIL_BACKEND))
    if hasattr(settings, 'CELERY_BROKER_URL'):
        info.append(('Celery broker', settings.CELERY_BROKER_URL.split(':')[0]))

    DATABASES = copy.deepcopy(settings.DATABASES)
    for d in DATABASES:
        if 'PASSWORD' in DATABASES[d]:
            DATABASES[d]['PASSWORD'] = '*****'
    info.append(('DATABASES',  mark_safe('<pre>'+escape(pprint.pformat(DATABASES))+'</pre>')))

    return info 
Example 2
Project: GTDWeb   Author: lanbing510   File: __init__.py    License: GNU General Public License v2.0 6 votes vote down vote up
def _create_cache(backend, **kwargs):
    try:
        # Try to get the CACHES entry for the given backend name first
        try:
            conf = settings.CACHES[backend]
        except KeyError:
            try:
                # Trying to import the given backend, in case it's a dotted path
                import_string(backend)
            except ImportError as e:
                raise InvalidCacheBackendError("Could not find backend '%s': %s" % (
                    backend, e))
            location = kwargs.pop('LOCATION', '')
            params = kwargs
        else:
            params = conf.copy()
            params.update(kwargs)
            backend = params.pop('BACKEND')
            location = params.pop('LOCATION', '')
        backend_cls = import_string(backend)
    except ImportError as e:
        raise InvalidCacheBackendError(
            "Could not find backend '%s': %s" % (backend, e))
    return backend_cls(location, params) 
Example 3
Project: GTDWeb   Author: lanbing510   File: __init__.py    License: GNU General Public License v2.0 6 votes vote down vote up
def __getitem__(self, alias):
        try:
            return self._caches.caches[alias]
        except AttributeError:
            self._caches.caches = {}
        except KeyError:
            pass

        if alias not in settings.CACHES:
            raise InvalidCacheBackendError(
                "Could not find config for '%s' in settings.CACHES" % alias
            )

        cache = _create_cache(alias)
        self._caches.caches[alias] = cache
        return cache 
Example 4
Project: django-cachalot   Author: noripyt   File: benchmark.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(self):
        for db_alias in settings.DATABASES:
            self.db_alias = db_alias
            self.db_vendor = connections[self.db_alias].vendor
            print('Benchmarking %s…' % self.db_vendor)
            for cache_alias in settings.CACHES:
                cache = caches[cache_alias]
                self.cache_name = cache.__class__.__name__[:-5].lower()
                with override_settings(CACHALOT_CACHE=cache_alias):
                    self.execute_benchmark()

        self.df = pd.DataFrame.from_records(self.data)
        if not os.path.exists(RESULTS_PATH):
            os.mkdir(RESULTS_PATH)
        self.df.to_csv(os.path.join(RESULTS_PATH, 'data.csv'))

        self.xlim = (0, self.df['time'].max() * 1.01)
        self.output('db')
        self.output('cache') 
Example 5
Project: django-userlog   Author: aaugustin   File: util.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_redis_client():
    global _client

    if _client is not None:
        return _client

    try:
        cache = caches['userlog']
    except KeyError:
        raise ImproperlyConfigured("No 'userlog' cache found in CACHES.")

    try:
        try:
            _client = cache.client                  # django-redis
        except AttributeError:
            _client = cache.get_master_client()     # django-redis-cache
        assert isinstance(_client, redis.StrictRedis)
    except (AssertionError, AttributeError):
        raise ImproperlyConfigured("'userlog' cache doesn't use Redis.")

    return _client 
Example 6
Project: django-userlog   Author: aaugustin   File: util.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_userlog_settings():
    global _settings

    if _settings is not None:
        return _settings

    def get_setting(name, default):
        return getattr(settings, 'USERLOG_' + name, default)

    # Coerce values into expected types in order to detect invalid settings.
    _settings = UserLogSettings(
        # Hardcode the default timeout because it isn't exposed by Django.
        timeout=int(settings.CACHES['userlog'].get('TIMEOUT', 300)),
        max_size=int(get_setting('MAX_SIZE', 25)),
        publish=bool(get_setting('PUBLISH', True)),
        ignore_urls=[re.compile(pattern)
                     for pattern in get_setting('IGNORE_URLS', [])],
        websocket_address=str(get_setting('WEBSOCKET_ADDRESS',
                                          'ws://localhost:8080/')),
    )

    return _settings 
Example 7
Project: luscan-devel   Author: blackye   File: __init__.py    License: GNU General Public License v2.0 6 votes vote down vote up
def parse_backend_conf(backend, **kwargs):
    """
    Helper function to parse the backend configuration
    that doesn't use the URI notation.
    """
    # Try to get the CACHES entry for the given backend name first
    conf = settings.CACHES.get(backend, None)
    if conf is not None:
        args = conf.copy()
        args.update(kwargs)
        backend = args.pop('BACKEND')
        location = args.pop('LOCATION', '')
        return backend, location, args
    else:
        try:
            # Trying to import the given backend, in case it's a dotted path
            mod_path, cls_name = backend.rsplit('.', 1)
            mod = importlib.import_module(mod_path)
            backend_cls = getattr(mod, cls_name)
        except (AttributeError, ImportError, ValueError):
            raise InvalidCacheBackendError("Could not find backend '%s'" % backend)
        location = kwargs.pop('LOCATION', '')
        return backend, location, kwargs 
Example 8
Project: django-mysql   Author: adamchainz   File: cull_mysql_caches.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle(self, *args, **options):
        verbosity = options.get("verbosity")

        aliases = set(options["aliases"])

        if not aliases:
            aliases = settings.CACHES

        for alias in aliases:
            try:
                cache = caches[alias]
            except InvalidCacheBackendError:
                raise CommandError("Cache '{}' does not exist".format(alias))

            if not isinstance(cache, MySQLCache):  # pragma: no cover
                continue

            if verbosity >= 1:
                self.stdout.write(
                    "Deleting from cache '{}'... ".format(alias), ending=""
                )
            num_deleted = cache.cull()
            if verbosity >= 1:
                self.stdout.write("{} entries deleted.".format(num_deleted)) 
Example 9
Project: django-mysql   Author: adamchainz   File: mysql_cache_migration.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def handle(self, *args, **options):
        aliases = set(options["aliases"])

        if not aliases:
            aliases = settings.CACHES

        tables = set()
        for alias in aliases:
            try:
                cache = caches[alias]
            except InvalidCacheBackendError:
                raise CommandError("Cache '{}' does not exist".format(alias))

            if not isinstance(cache, MySQLCache):  # pragma: no cover
                continue

            tables.add(cache._table)

        if not tables:
            self.stderr.write("No MySQLCache instances in CACHES")
            return

        migration = self.render_migration(tables)
        self.stdout.write(migration) 
Example 10
Project: django-qr-code   Author: dprog-philippe-docourt   File: views.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def cache_qr_code():
    """
    Decorator that caches the requested page if a settings named 'QR_CODE_CACHE_ALIAS' exists and is not empty or None.
    """
    def decorator(view_func):
        @functools.wraps(view_func)
        def _wrapped_view(request, *view_args, **view_kwargs):
            cache_enabled = request.GET.get('cache_enabled', True)
            if cache_enabled and hasattr(settings, 'QR_CODE_CACHE_ALIAS') and settings.QR_CODE_CACHE_ALIAS:
                # We found a cache alias for storing the generate qr code and cache is enabled, use it to cache the
                # page.
                timeout = settings.CACHES[settings.QR_CODE_CACHE_ALIAS]['TIMEOUT']
                key_prefix = 'token=%s.user_pk=%s' % (request.GET.get('url_signature_enabled') or constants.DEFAULT_URL_SIGNATURE_ENABLED, request.user.pk)
                response = cache_page(timeout, cache=settings.QR_CODE_CACHE_ALIAS, key_prefix=key_prefix)(view_func)(request, *view_args, **view_kwargs)
            else:
                # No cache alias for storing the generated qr code, call the view as is.
                response = (view_func)(request, *view_args, **view_kwargs)
            return response
        return _wrapped_view
    return decorator 
Example 11
Project: GTDWeb   Author: lanbing510   File: createcachetable.py    License: GNU General Public License v2.0 5 votes vote down vote up
def add_arguments(self, parser):
        parser.add_argument('args', metavar='table_name', nargs='*',
            help='Optional table names. Otherwise, settings.CACHES is used to '
            'find cache tables.')
        parser.add_argument('--database', action='store', dest='database',
            default=DEFAULT_DB_ALIAS,
            help='Nominates a database onto which the cache tables will be '
            'installed. Defaults to the "default" database.') 
Example 12
Project: GTDWeb   Author: lanbing510   File: createcachetable.py    License: GNU General Public License v2.0 5 votes vote down vote up
def handle(self, *tablenames, **options):
        db = options.get('database')
        self.verbosity = int(options.get('verbosity'))
        if len(tablenames):
            # Legacy behavior, tablename specified as argument
            for tablename in tablenames:
                self.create_table(db, tablename)
        else:
            for cache_alias in settings.CACHES:
                cache = caches[cache_alias]
                if isinstance(cache, BaseDatabaseCache):
                    self.create_table(db, cache._table) 
Example 13
Project: django-cachalot   Author: noripyt   File: api.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _cache_db_tables_iterator(tables, cache_alias, db_alias):
    no_tables = not tables
    cache_aliases = settings.CACHES if cache_alias is None else (cache_alias,)
    db_aliases = settings.DATABASES if db_alias is None else (db_alias,)
    for db_alias in db_aliases:
        if no_tables:
            tables = connections[db_alias].introspection.table_names()
        if tables:
            for cache_alias in cache_aliases:
                yield cache_alias, db_alias, tables 
Example 14
Project: django-cachalot   Author: noripyt   File: api.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_last_invalidation(*tables_or_models, **kwargs):
    """
    Returns the timestamp of the most recent invalidation of the given
    ``tables_or_models``.  If ``tables_or_models`` is not specified,
    all tables found in the database (including those outside Django) are used.

    If ``cache_alias`` is specified, it only fetches invalidations
    in this cache, otherwise invalidations in all caches are fetched.

    If ``db_alias`` is specified, it only fetches invalidations
    for this database, otherwise invalidations for all databases are fetched.

    :arg tables_or_models: SQL tables names, models or models lookups
                           (or a combination)
    :type tables_or_models: tuple of strings or models
    :arg cache_alias: Alias from the Django ``CACHES`` setting
    :type cache_alias: string or NoneType
    :arg db_alias: Alias from the Django ``DATABASES`` setting
    :type db_alias: string or NoneType
    :returns: The timestamp of the most recent invalidation
    :rtype: float
    """
    # TODO: Replace with positional arguments when we drop Python 2 support.
    cache_alias = kwargs.pop('cache_alias', None)
    db_alias = kwargs.pop('db_alias', None)
    for k in kwargs:
        raise TypeError("get_last_invalidation() got an unexpected "
                        "keyword argument '%s'" % k)

    last_invalidation = 0.0
    for cache_alias, db_alias, tables in _cache_db_tables_iterator(
            list(_get_tables(tables_or_models)), cache_alias, db_alias):
        get_table_cache_key = cachalot_settings.CACHALOT_TABLE_KEYGEN
        table_cache_keys = [get_table_cache_key(db_alias, t) for t in tables]
        invalidations = cachalot_caches.get_cache(
            cache_alias, db_alias).get_many(table_cache_keys).values()
        if invalidations:
            current_last_invalidation = max(invalidations)
            if current_last_invalidation > last_invalidation:
                last_invalidation = current_last_invalidation
    return last_invalidation 
Example 15
Project: django-cachalot   Author: noripyt   File: apps.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_cache_compatibility(app_configs, **kwargs):
    cache = settings.CACHES[cachalot_settings.CACHALOT_CACHE]
    cache_backend = cache['BACKEND']
    if cache_backend not in SUPPORTED_CACHE_BACKENDS:
        return [Warning(
            'Cache backend %r is not supported by django-cachalot.'
            % cache_backend,
            hint='Switch to a supported cache backend '
                 'like Redis or Memcached.',
            id='cachalot.W001')]
    return [] 
Example 16
Project: django-cachalot   Author: noripyt   File: settings.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cache_compatibility(self):
        compatible_cache = {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        }
        incompatible_cache = {
            'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
            'LOCATION': 'cache_table'
        }

        with self.settings(CACHES={'default': compatible_cache,
                                   'secondary': incompatible_cache}):
            errors = run_checks(tags=[Tags.compatibility])
            self.assertListEqual(errors, [])

        warning001 = Warning(
            'Cache backend %r is not supported by django-cachalot.'
            % 'django.core.cache.backends.db.DatabaseCache',
            hint='Switch to a supported cache backend '
                 'like Redis or Memcached.',
            id='cachalot.W001')
        with self.settings(CACHES={'default': incompatible_cache}):
            errors = run_checks(tags=[Tags.compatibility])
            self.assertListEqual(errors, [warning001])
        with self.settings(CACHES={'default': compatible_cache,
                                   'secondary': incompatible_cache},
                           CACHALOT_CACHE='secondary'):
            errors = run_checks(tags=[Tags.compatibility])
            self.assertListEqual(errors, [warning001]) 
Example 17
Project: django-cachalot   Author: noripyt   File: api.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        super(APITestCase, self).setUp()
        self.t1 = Test.objects.create(name='test1')
        self.cache_alias2 = next(alias for alias in settings.CACHES
                                 if alias != DEFAULT_CACHE_ALIAS) 
Example 18
Project: django-cachalot   Author: noripyt   File: api.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        self.db_alias2 = next(alias for alias in settings.DATABASES
                              if alias != DEFAULT_DB_ALIAS)

        self.cache_alias2 = next(alias for alias in settings.CACHES
                                 if alias != DEFAULT_CACHE_ALIAS)

        self.t1 = Test.objects.create(name='test1')
        self.t2 = Test.objects.using(self.db_alias2).create(name='test2')
        self.u = User.objects.create_user('test') 
Example 19
def test_save(self):
        if (
            hasattr(self.session, '_cache')
            and 'DummyCache'
            in settings.CACHES[settings.SESSION_CACHE_ALIAS]['BACKEND']
        ):
            raise unittest.SkipTest(
                "Session saving tests require a real cache backend"
            )
        self.session.save()
        self.assertTrue(self.session.exists(self.session.session_key)) 
Example 20
Project: django-userlog   Author: aaugustin   File: util.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def reset_caches(**kwargs):
    global _client, _settings
    if kwargs['setting'] == 'CACHES':
        _client = None
        _settings = None 
Example 21
Project: django-userlog   Author: aaugustin   File: realtime.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def redis_connection():
    userlog = settings.CACHES['userlog']
    options = userlog.get('OPTIONS', {})
    if ':' in userlog['LOCATION']:
        host, port = userlog['LOCATION'].rsplit(':', 1)
        port = int(port)
    else:
        host = userlog['LOCATION']
        port = 0
    db = options.get('DB', 1)
    password = options.get('PASSWORD', None)
    redis = yield from asyncio_redis.Connection.create(
        host=host, port=port, password=password, db=db)
    return redis 
Example 22
Project: zing   Author: evernote   File: cache.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_cache(cache=None):
    """Return ``cache`` or the 'default' cache if ``cache`` is not specified or
    ``cache`` is not configured.

    :param cache: The name of the requested cache.
    """
    try:
        # Check for proper Redis persistent backends
        # FIXME: this logic needs to be a system sanity check
        if (
            not settings.DEBUG
            and cache in PERSISTENT_STORES
            and (
                cache not in settings.CACHES
                or "RedisCache" not in settings.CACHES[cache]["BACKEND"]
                or settings.CACHES[cache].get("TIMEOUT", "") is not None
            )
        ):
            raise ImproperlyConfigured(
                "Pootle requires a Redis-backed caching backend for %r "
                "with `TIMEOUT: None`. Please review your settings." % cache
            )

        return caches[cache]
    except InvalidCacheBackendError:
        return default_cache 
Example 23
Project: django-admin-interface   Author: fabiocaccamo   File: cache.py    License: MIT License 5 votes vote down vote up
def app_cache():
    return caches['admin_interface'] if 'admin_interface' in settings.CACHES else cache 
Example 24
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_or_set_racing(self):
        with mock.patch('%s.%s' % (settings.CACHES['default']['BACKEND'], 'add')) as cache_add:
            # Simulate cache.add() failing to add a value. In that case, the
            # default value should be returned.
            cache_add.return_value = False
            self.assertEqual(cache.get_or_set('key', 'default'), 'default') 
Example 25
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_second_call_doesnt_crash(self):
        out = io.StringIO()
        management.call_command('createcachetable', stdout=out)
        self.assertEqual(out.getvalue(), "Cache table 'test cache table' already exists.\n" * len(settings.CACHES)) 
Example 26
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_location_multiple_servers(self):
        locations = [
            ['server1.tld', 'server2:11211'],
            'server1.tld;server2:11211',
            'server1.tld,server2:11211',
        ]
        for location in locations:
            with self.subTest(location=location):
                params = {'BACKEND': self.base_params['BACKEND'], 'LOCATION': location}
                with self.settings(CACHES={'default': params}):
                    self.assertEqual(cache._servers, ['server1.tld', 'server2:11211']) 
Example 27
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_default_never_expiring_timeout(self):
        # Regression test for #22845
        with self.settings(CACHES=caches_setting_for_tests(
                base=self.base_params,
                exclude=memcached_excluded_caches,
                TIMEOUT=None)):
            cache.set('infinite_foo', 'bar')
            self.assertEqual(cache.get('infinite_foo'), 'bar') 
Example 28
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_default_far_future_timeout(self):
        # Regression test for #22845
        with self.settings(CACHES=caches_setting_for_tests(
                base=self.base_params,
                exclude=memcached_excluded_caches,
                # 60*60*24*365, 1 year
                TIMEOUT=31536000)):
            cache.set('future_foo', 'bar')
            self.assertEqual(cache.get('future_foo'), 'bar') 
Example 29
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        self.dirname = tempfile.mkdtemp()
        # Caches location cannot be modified through override_settings / modify_settings,
        # hence settings are manipulated directly here and the setting_changed signal
        # is triggered manually.
        for cache_params in settings.CACHES.values():
            cache_params.update({'LOCATION': self.dirname})
        setting_changed.send(self.__class__, setting='CACHES', enter=False) 
Example 30
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_or_set_racing(self):
        with mock.patch('%s.%s' % (settings.CACHES['default']['BACKEND'], 'add')) as cache_add:
            # Simulate cache.add() failing to add a value. In that case, the
            # default value should be returned.
            cache_add.return_value = False
            self.assertEqual(cache.get_or_set('key', 'default'), 'default')