当前位置:网站首页>How to use memcached to implement Django project caching
How to use memcached to implement Django project caching
2022-06-21 20:31:00 【Minimalist lesson】
1. install memcached
Here we use Centos Take how to install as an example , Run the following command , install memcached
sudo yum install memcachedTest for successful installation
memcached -helpIf no error is reported , It means that the installation is successful
2. function memcached
memcached -d -m 64 -l 127.0.0.1 -p 11211 -u root-d: Indicates that the daemon is started in the background
-m: Indicates how much memory is used , Use here 64M
-l: It's a listening server IP Address , Here we fill in the local , If you want other machines to access , It's written in 0.0.0.0
-p: It's the port number , The default is 11211
-u: Specify which user to run , Designated here root
see memcached Whether it runs successfully or not , Use the following command
ps -ef|grep memcachedIf the display is as shown in the figure below , Indicates successful startup
You can use the following command to turn off memcached process
killall -9 memcached3. Connect Memcached
Use telnet De link , Install first telnet, Following commands
sudo yum install telnetAfter installation , Use the following command to connect memcached
telnet 127.0.0.1 11211The following interface appears , Indicates successful connection , function quit Exit connection
thus , We installed it successfully memcached, And can run successfully , Let's introduce Django How the project uses it
4. install python-mamcached
In their own projects python Installation in environment , In the case of virtualization , Run the virtual environment before installing , The order is as follows
pip install python-memcached5. To configure Django project
In our project setting.py In the document , Join in CACHE Options , as follows
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}Then according to the actual situation of your project , Set the required cache time , My personal Blog The cache time is set to one day , namely 60*60*24 second
CACHE_MIDDLEWARE_SECONDS=60*60*24Last , We are working on middleware MIDDLEWARE add to memcached what is needed Django middleware , Because I chose to cache the whole site , So the things we need are as follows : Pay attention to the position and order of middleware
'django.middleware.cache.UpdateCacheMiddleware',# Put it in the first place of the middleware
...... # Other middleware
'django.middleware.cache.FetchFromCacheMiddleware',# Put it in the last position of the middleware The above is all our configuration and installation , restart nginx and uwsgi Then you can open the website for speed measurement , It may be slow to open for the first time , Because you just put your request in the cache , When you open it again , Or open it within the cache time you just set , It's all on in seconds .
6. matters needing attention
Because we set up 24 Hour cache , So during the cache period , If you write a blog post , Your website won't show up immediately , Wait until the cache time expires , Will be updated , If you want to show it right away , You need to clean up the cache manually , Is the cache expired , The website will re cluster the database to get data , Manual cache expiration , Use the following command
# Connect first memcached
telnet 127.0.0.1 11211
# Clear cache
flush_all边栏推荐
猜你喜欢

mysql如何对列求和

Rough reading of targeted supervised contractual learning for long tailed recognition

RPA financial process automation | Shanghai Pudong Development Group and cloud expansion technology accelerate financial digital operation

Netcore3.1 Ping whether the network is unblocked and obtaining the CPU and memory utilization of the server

某大厂第二轮裁员来袭,套路满满

Custom code template

软件测试办公工具推荐-桌面日历

IAR重大升级,支持VS Code,ST发布第一个带有处理单元的传感器

mysql增加的语句是什么
![[wechat applet failed to change appid] wechat applet failed to modify appid all the time and reported an error. Tourist appid solution](/img/b7/6ce97e345a4f8fce7f3aeb2c472e13.png)
[wechat applet failed to change appid] wechat applet failed to modify appid all the time and reported an error. Tourist appid solution
随机推荐
Is there any difference between MySQL and Oracle
Inno setup window drag learning
汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(一)——整体系统设计
机器学习和模式识别怎么区分?
日常开发常用工具提升效率
2022-06-20
mysql如何對列求和
ENVI-Classic-Annotation-object添加的元素图例比例尺如何撤回修改删除
mysql增加的语句是什么
Introduction to machine learning
BTC投资者损失预计达73亿美元!“割肉式”抛售来袭?加密寒冬比预期更冷、更长!
How MySQL implements grouping sum
Jenkins regularly builds and passes build parameters
Can the financial product be redeemed on the due date?
Points cloud to Depth maps: conversion, Save, Visualization
The difference between break and continue
SQL语句知识点有哪些
《跟老卫学 HarmonyOS 开发》:以父之名·码力全开!写段HarmonyOS祝父亲节
技术实践 | 场景导向的音视频通话体验优化
mysql如何查询第几条数据