I have configured cache in settings.py in my Django project as follows:
CACHE_MIDDLEWARE_ALIAS = 'Cache'
CACHE_MIDDLEWARE_SECONDS = 60
CACHE_MIDDLEWARE_KEY_PREFIX = ''
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
Cache service is running after I typed: $ memcached -p 11211 &
But when I try to run server, the following error shows up:
django.core.cache.backends.base.InvalidCacheBackendError: Could not find config for 'Cache' in settings.CACHES
What I am doing wrong?
The CACHE_MIDDLEWARE_ALIAS setting tells the django middleware which cache to use: https://docs.djangoproject.com/en/3.1/ref/settings/#cache-middleware-alias
Here it is set to 'Cache' but there is no cache of that name in your CACHES setting. You probably don't want a different alias and should just use the default of 'default' so just remove that setting.
Related
I configured ElastiCache redis with cluster mode enabled.
I want to connect ElastiCache with local Django.
So I configured bastion host.
I connected ElastiCache(non-cluster mode) with local Django. I tried cache.set(), cache.get(). It's OK.
I installed 'Django-redis-cache' and 'settings.py' is like that.
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'localhost:6379',
}
}
But I have problem when I connect ElastiCache(cluster mode) with django.
I tried tunneling with ElastiCache configuration endpoint.
When I use the same 'settings.py', error message is like that.
'SELECT is not allowed in cluster mode'
So, I changed 'settings.py'.
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'localhost:6379',
'OPTIONS': {
'DB': 0
},
}
}
And then, error message is like that.
'MOVED 4205 xx.xx.xx.xxx:6379'
What I have to do?
There are no example which connect ElastiCache(cluster mode) with Django.
I've built a simple SPA CRUD web app with Django, Vue and Docker(-compose).
Since I've finished developing the app, I'm now preparing for the production environment, that is, using bundle.js and bundle.css files.
When I try to load the main page, http://localhost:8000, no CSS or JS
are being loaded because I'm getting this error in the browser's console:
GET http://0.0.0.0:8080/bundle.css net::ERR_CONNECTION_REFUSED
GET http://0.0.0.0:8080/bundle.js net::ERR_CONNECTION_REFUSED
I really don't know why it is giving that error or how to fix it.
This is my vue.config.js file:
const webpack = require("webpack");
const BundleTracker = require("webpack-bundle-tracker");
module.exports = {
publicPath: "http://0.0.0.0:8080/",
outputDir: "./dist/",
filenameHashing: false,
configureWebpack: {
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
]
},
chainWebpack: config => {
config
.plugin("BundleTracker")
.use(BundleTracker, [{ filename: "./webpack-stats.json" }]);
config.output.filename("bundle.js");
config.optimization.splitChunks(false);
config.optimization.delete("splitChunks");
config.resolve.alias.set("__STATIC__", "static");
config.devServer
.hotOnly(true)
.watchOptions({ poll: 1000 })
.https(false)
.disableHostCheck(true)
.headers({ "Access-Control-Allow-Origin": ["*"] });
},
// uncomment before executing 'npm run build'
css: {
extract: {
filename: "bundle.css",
chunkFilename: "bundle.css"
}
}
};
This is part of my settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "assets"),
os.path.join(BASE_DIR, "frontend/dist"),
]
# STATIC_ROOT = "" # The absolute path to the dir for collectstatic
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'dist/',
'STATS_FILE': os.path.join(BASE_DIR, 'frontend', 'webpack-stats.json'),
}
}
When I run the npm run build command I get notified that both the bundle.css and the bundle.js files have been generated:
File Size Gzipped
dist/bundle.js 150.73 KiB 51.05 KiB
dist/bundle.css 192.06 KiB 26.89 KiB
Images and other types of assets omitted.
DONE Build complete. The dist directory is ready to be deployed.
INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
I really don't know why it is giving that ERR_CONNECTION_REFUSEDerror or how to fix it.
For production remove publicPath from your vue config file, I suppose you used that for development purposes, but in production you should only create the bundle and serve it to the user.
What's probably happening is that in your Docker setup you don't have the webpack dev server running (and you don't need it), thus you hit a connection refused error.
What LOCATION should I point Memcached to after deployment on pythonanywhere server? For local I am using this setting and things are working fine.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
I need to change 'LOCATION' to replace localhost. Any guidance?
You can location set to a path.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:~/memcached.sock', }}
However, I don't think pythonanywhere lets you use memcache, since you can't use 'sudo apt-get' on a pythonanywhere console, and using memcache requires you to install it. (sudo apt-get install memcached)
How can I set up collectfast for django on heroku?
This is assuming I've already successfully set up static files hosting and serving from Amazon S3.
1) To disable heroku's automatic collectstatic, run:
heroku config:set DISABLE_COLLECTSTATIC=1
2) Add the following to settings.py to use a table in your database for the collectfast's caching. Commit and push the change to heroku.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
},
'collectfast': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'collectfast_cache',
'TIMEOUT': 60,
'OPTIONS': {
'MAX_ENTRIES': 10000
},
},
}
COLLECTFAST_CACHE = 'collectfast'
4) To create the required cache table in the database, run:
heroku run createcachetable
5) To restore heroku's automatic collectstatic, run:
heroku config:unset DISABLE_COLLECTSTATIC
Each deploy will now correctly use collectfast to collect modified static files to s3.
I'm attempting to use django-redis using Unix sockets rather than a TCP connection:
This is the settings.py configuration:
CACHES = {
'default': {
'BACKEND': 'redis_cache.cache.RedisCache',
'LOCATION': 'unix:/tmp/redis.sock:1',
'OPTIONS': {
'PASSWORD': '',
'PICKLE_VERSION': -1, # default
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CLIENT_CLASS': 'redis_cache.client.DefaultClient',
},
},
}
and this is an extract of the redis config file at /etc/redis/6379.conf:
# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /tmp/redis.sock
unixsocketperm 755
Still I receive a ConnectionInterrumped exception, which stands for an error during connection. Any ideas about what this configuration's issue is?
P.S. My Django version is 1.5.1, django-redis is 3.3 and hiredis is 0.0.1.
EDIT: Apparently I read the cache provider wrong, the below answer is the solution for django-redis-cache, not django-redis. I'll let the answer remain though, since changing cache provider and using this config seems to have solved the problem.
You should not need the unix: prefix, and the backend setting looks strange;
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '/tmp/redis.sock',
'OPTIONS': { ...