How does the Django project configure the Memcached and Redis caches? Which one is more advantageous?
- 2021-10-25 07:09:52
- OfStack
Memcached vs Redis
For medium and large websites, using cache to reduce the number of visits to the database is one of the key means to improve the performance of websites. The most commonly used cache backgrounds in the Django project production environment are Memcached and Redis. Today, this site will teach you how to configure Memcached and Redis as cache backgrounds in the Django project. So which of them is better? This article will compare these two storage systems and give the answer at the end of this article.
Memcache cache
Memcache is a high-performance distributed memory object caching system, which is the fastest and most effective caching system supported by Django natively. The advantage of Memcached is that it is fast, belongs to distributed cache, and supports running on multiple servers at the same time (Django will treat them as a large cache).
Step 1: Install Memcached
windows system: official website download, decompress and install.
Linux system: The Ubuntu system requires Memcached dependency environment to be installed using sudo apt-get install libevent ibevent-dev, and memcached to be installed using sudo apt-get install memcached.
Step 2: Start Memcached
# Linux System - Foreground startup
/usr/local/memcached/bin/memcached -p 11211 -m 64m -vv
# Linux System - Start as a background service
/usr/local/memcached/bin/memcached -p 11211 -m 64m -d
Step 3: pip Install python-memcached
Python Operation of the memcached database requires the installation of python-memcached or pylibmc, the former being recommended.
pip install pyhon-memcached
Step 4: Set memcached to Django cache background
# Local cache, using localhost
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
# Use unix soket Communication
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:/tmp/memcached.sock',
}
}
# Distributed caching, multiple servers, support for configuration weights.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': [
'172.19.26.240:11211',
'172.19.26.242:11211',
]
# We can also weigh the cache machines, and the ones with higher weights take on more requests, as follows:
'LOCATION': [
('172.19.26.240:11211',5),
('172.19.26.242:11211',1),
]
}
}
Redis cache
Redis is today's fastest in-memory non-relational (NoSQL) database. Redis not only supports simple key-value data, but also provides storage of list, set, zset, hash and other data structures.
Step 1: Install Redis
windows system: official website download, decompress and install, remember to check and add environment variables. Linux System: The Ubuntu system can be installed using sudo apt-get install redis-server.
Step 2: Start the Redis service
# Windows Systems: cmd Enter redis Installation directory, boot redis Services
redis-server.exe redis.windows.conf
# Linux System: Enter redis Installation directory startup redis Services
redis-server /etc/redis/redis.conf
# Open redis Interactive command line for testing ( Optional )
redis-cli.exe -h 127.0.0.1 -p 6379 # windows Open it separately under the system 1 Windows
redis-cli # linux System
Note: By default, no password is required to access the Redis server. In order to allow other servers to use the same security, we recommend setting the access password of the Redis server.
Because redis is bound native by default, Step 1 cancels this setting:
# Edit Configuration File
sudo vim /etc/redis/redis.conf
After opening the configuration file with vim, comment out the following line:
# bind 127.0.0.1
Then set the login password. Because the configuration file is long, enter in command mode
/requirepass foobared
Quickly search for this configuration item:
# Find this below 1 Row and remove comments, before modifying:
#requirepass foobared
# Amend to read:
requirepass your_pwd # Set a new password
Use after modification
redis-server restart
Restart the server for the configuration to take effect. When accessing redis from other servers in the future, you can bring the password you set:
redis-cli -a your_pwd -h hostip
Step 3: pip Install django-redis
After Redis is installed and started, you need to install django-redis through pip to operate the redis database in Django.
pip install django-redis
Step 4: Set Redis to Django cache background
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://your_host_ip:6379', # redis Server or container ip Address
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": "your_pwd", # The password you set
},
},
}
You can also find
settings.py
Set the default cache expiration time (not required).
pip install pyhon-memcached
0
Test whether the cache is set successfully
After you have modified the cache configuration in settings. py, you must want to know if the Django cache has been set up successfully. You can enter the following command to open the command interaction window of Python:
pip install pyhon-memcached
1
Then type the following commands one by one to test. If there are no errors reported, your cache setting is successful.
pip install pyhon-memcached
2
Memcached vs Redis
Memcached and Redis are both memory-based, key-value data storage systems that are fast and support distributed deployment, but they are quite different.
Data structure: Compared with memcached, Redis has more data structures (such as list, set, zset, hash) and supports rich data manipulation. Upper key size: The upper storage size of each key value (value) of Redis is up to 512MB, while the maximum size of each key value of Memcached is no more than 1MB. Data persistence: Memcached does not support data persistence, and the cached data will disappear after the server restarts, while Redis not only supports the persistence of cached data on the hard disk, but also supports data backup in master-slave mode.From all aspects, the functional characteristics of Redis are far superior to those of Memcached. If your enterprise wants to choose a caching system, this site suggests that it is enough to go directly to redis.
That's how the Django project configures the Memcached and Redis caches? Which one is more advantageous? For more information about Django project configuring Memcached and Redis cache, please pay attention to other related articles on this site!