给你的 django 网站加个速 gevent django缓存

利用 gevent 给网站加个速

进入 /usr/lib/systemd/system 文件夹

Python
(base) ➜ system cat xxx.service [Unit] After=syslog.target network.target remote-fs.target nss-lookup.target [Service] # 你的用户 User=root # 你的目录 WorkingDirectory=/temp/website/www.xxx.com/ # gunicorn启动命令 ExecStart=/root/.local/share/virtualenvs/www.xxx.com-7iK-eqgK/bin/gunicorn --worker-class=gevent --bind unix:/tmp/www.heigejia.com.socket cms_v1.wsgi:application Restart=on-failure [Install] WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
( base ) ➜    system cat xxx . service             
[ Unit ]
After = syslog . target network . target remote - fs . target nss - lookup . target
[ Service ]
# 你的用户
User = root
# 你的目录
WorkingDirectory = / temp / website / www . xxx . com /
# gunicorn启动命令
ExecStart = / root / . local / share / virtualenvs / www . xxx . com - 7iK - eqgK / bin / gunicorn    -- worker - class = gevent -- bind unix : / tmp / www . heigejia . com . socket cms_v1 . wsgi : application
Restart = on - failure
[ Install ]
WantedBy = multi - user . target
 

–worker-class
指定工作方式,这里我们用gevent

Python
gunicorn --worker-class=gevent isaced.wsgi:application gunicorn wsgi:application #8个worker gunicorn -w 8 wsgi:application #指定端口号 gunicorn -w 8 -b 0.0.0.0:8888 wsgi:application #unix socket gunicorn -w 8 --bind unix:/xx/mysock.sock wsgi:application #使用gevent做异步(默认worker是同步的) gunicorn -w 8 --bind 0.0.0.0:8000 -k 'gevent' wsgi:application #选项挺多,看文档或者使用 --help都可以查看 --log-level=DEBUG 日志级别 --timeout=100 超时时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
gunicorn -- worker - class = gevent isaced . wsgi : application
gunicorn wsgi : application
#8个worker
gunicorn - w 8 wsgi : application
#指定端口号
gunicorn - w 8 - b 0.0.0.0 : 8888 wsgi : application
#unix socket
gunicorn - w 8 -- bind unix : / xx / mysock . sock wsgi : application
#使用gevent做异步(默认worker是同步的)
gunicorn - w 8 -- bind 0.0.0.0 : 8000 - k 'gevent' wsgi : application
#选项挺多,看文档或者使用 --help都可以查看
-- log - level = DEBUG   日志级别
-- timeout = 100   超时时间
 
如何查看当前 linux 是几核

给你的 django 网站加入 缓存技术

修改 settings.py加入缓存配置
Python
CACHES = { 'default': { 'BACKEND': '<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/django" title="View all posts in django" target="_blank">django</a></span>.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/temp/website/www.xxx.com/tmp/<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/django" title="View all posts in django" target="_blank">django</a></span>_cache', 'TIMEOUT': 600, 'OPTIONS': { 'MAX_ENTRIES': 1000 } } }
1
2
3
4
5
6
7
8
9
10
11
CACHES = {
     'default' : {
         'BACKEND' : 'django.core.cache.backends.filebased.FileBasedCache' ,
         'LOCATION' : '/temp/website/www.xxx.com/tmp/django_cache' ,
         'TIMEOUT' : 600 ,
         'OPTIONS' : {
             'MAX_ENTRIES' : 1000
         }
     }
}
 
修改 views.py
Python
from django.views.decorators.cache import cache_page @cache_page(60 * 15) # 秒数,这里指缓存 15 分钟,不直接写900是为了提高可读性 def video_index(request): ... return xxx
1
2
3
4
5
6
7
8
from django . views . decorators . cache import cache _page
 
@ cache_page ( 60 * 15 ) # 秒数,这里指缓存 15 分钟,不直接写900是为了提高可读性
def video_index ( request ) :
   . . .
     return xxx
 
 
访问网站后产生缓存



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值