您好,登錄后才能下訂單哦!
django緩存進階
django六種緩存策略
開發(fā)調(diào)試
內(nèi)存
文件
數(shù)據(jù)庫
Memcache緩存(python-memcached模塊)
Memcache緩存(pylibmc模塊)
緩存頁面
1,@cache_page方法緩存
@cache_page(timeout=10,cache='html',key_prefix='page')
def list(request):
chrs = string.ascii_letters
char = random.choice(chrs)
return HttpResponse('用戶列表頁面: <br> %s' % char)
2,添加中間件進行緩存,創(chuàng)建緩存中間件,每次有請求前先判斷
class CachePageMiddleware(MiddlewareMixin):
cache_page_path = [
'/user/list/'
]
# 實現(xiàn)process_request和process_response來判斷緩存,request.path
def process_request(self, request):
# 判斷當前的請求是否支持緩存
if request.path in self.cache_page_path:
# 判斷頁面是否已緩存
if cache.has_key(request.path):
return HttpResponse(cache.get(request.path))
def process_response(self, request, response):
# 判斷當前請求路徑是否要被緩存
if request.path in self.cache_page_path:
# 設(shè)置響應(yīng)內(nèi)容為緩存的內(nèi)容,請求路徑,響應(yīng),超時
cache.set(request.path,
response.content, timeout=5)
return response
用redis進行的緩存
CACHES = {
# 設(shè)置默認緩存
'default': {
# 緩存的方法,django-radis.cache.RedisCache
'BACKEND': 'django_redis.cache.RedisCache',
# 設(shè)置緩存的路徑
'LOCATION': 'redis://127.0.0.1:6379/10',
# 設(shè)置緩存的路徑,用戶類型,連接超時,
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'SOCKET_CONNECT_TIMEOUT': 10,
'SOCKET_TIMEOUT': 10
}
}
}
用session設(shè)置緩存
SESSION_ENGINE='django.contrib.session.backends.cache'
SESSION_COOKIE_NAME='SESSION_ID'
SESSION_COOKIE_PATH='/'
SESSION_CACHE_ALIAS='default'
SESSION_COOKIE_AGE='1209600'
信號機制
監(jiān)控django的內(nèi)部事件
復(fù)雜業(yè)務(wù)解耦
接收內(nèi)置的信號
先設(shè)置普通的接收信號,使用信號的connect()函數(shù)
from django.db.models.signals impoer pre_delete
def model_delete_pre(sender,**kwargs):
from user.models import Order
if sender == Order:
print('')
pre_delete.connect(model_delete_pre)
from django.dispatch import receiver
@receiver(post_delete)
def delete_model_post(sender,**kwargs):
print(sender,kwargs)
pre_delete.connect(delete_model_post)
創(chuàng)建signals包,并在init中聲明
from django import dispatch
codeSignal = dispatch.Signal(providing_args=['path','phone','code'])
根據(jù)業(yè)務(wù)需求,在適當位置發(fā)送信息
def new_code(request):
# 生成手機驗證碼
# 隨機產(chǎn)生驗證碼,
code_text = code_new_code_str(4)
print(code_text)
phone = request.GET.get('phone')
print(phone)
# 將信號發(fā)送出去
signal.codeSignal.send('new_code',
path = 'request.path',
phone = phone,
code = code_text)
return HttpResponse('%s...' % phone)
接收信號
from signals import codeSignal
from django import dispatch
@dispatch.receiver(codeSignal)
def cache_code(sender,**kwargs):
print('...')
print(sender,kwargs)
HttpResponse的子類
JsonResponse
HttpResponseRedirect
HttpresopnseNotAllow
HttpResponseGone
django中間件相關(guān)函數(shù)
process_request()
process_response()
process_view()
process_exception()
process_template_response()
分頁器屬性
bumber
object_list
has_previous
has_next
previous_page_number
next_page_number
高并發(fā)解決方案
使用celery+redis隊列
celery解決c10k問題,通過中間件和后臺任務(wù)執(zhí)行單元解決高并發(fā)問題
celery組成部分
消息中間件broker
任務(wù)執(zhí)行單元worker
存儲任務(wù)執(zhí)行單元存儲result
配置
相關(guān)文檔: http://docs.celeryproject.org/en/latest/django/index.html
win在celery后不支持多進程方式執(zhí)行需要該換成協(xié)程方式
from future import absolute_import ,unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE','advicedjango.settings')
app = Celery('advicedjango',
broker='redis://10.36.174.1:6379/8')
app.config_from_object('django.conf.settings')
app.autodiscover_tasks()
from .celery import app as celer_app
all = ('celery',)
from celery import shared_task
@shared_task
def qbuy(id,name):
print(id,name)
time.sleep(1)
return '%s,%s % (id,name)'
CELERY_IMPORTS = ('stockapp.tasks',)
celery -A advicedjango worker -P gevent -I info
-A 指定項目,-P 指定協(xié)程 -I 指定信息
qbuy('1991',3)
Celery服務(wù)接收任務(wù),并執(zhí)行,可以看到執(zhí)行的結(jié)果,
但是結(jié)果發(fā)出的警告。因為沒有處理執(zhí)行單元完成任務(wù)的結(jié)果。
pip3 install django-celery-results
在settings中配置result方案
進行新館庫的遷移
python manage.py migrate django_celery_results
遷移成功后重新啟動Celery
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。