1. FastDFS
- 海量存儲(chǔ), 存儲(chǔ)容量擴(kuò)展方便
- 防止文件內(nèi)容重復(fù)
-
結(jié)合nginx提高網(wǎng)站訪問(wèn)圖片的效率
WX20171215-113919.png
2. Django對(duì)接fdfs
- 自定義文件存儲(chǔ)類
from fdfs_client.client import Fdfs_client
from django.core.files.storage import Storage
from django.conf import settings
class FDFSStorage(Storage):
# fast dfs 文件儲(chǔ)存類
def __init__(self, client_conf=None, base_url=None):
if client_conf is None:
client_conf = settings.FDFS_CLIENT_CONF
self.client_conf = client_conf
if base_url is None:
base_url = settings.FDFS_URL
self.base_url = base_url
def _open(self, name, mode='rb'):
pass
def _save(self, name, content):
# name:你選擇上傳文件的名字
# content:包含你上傳文件內(nèi)容的File對(duì)象
# 創(chuàng)建一個(gè)Fdfs_client對(duì)象
client = Fdfs_client(self.client_conf)
res = client.upload_by_buffer(content.read())
# dict
# {
# 'Group name': group_name,
# 'Remote file_id': remote_file_id,
# 'Status': 'Upload successed.',
# 'Local file name': '',
# 'Uploaded size': upload_size,
# 'Storage IP': storage_ip
# }
if res['Status'] != 'Upload successed.':
raise Exception('上傳文件到fast dfs失敗')
filename = res['Remote file_id']
return filename
def exists(self, name):
"""Django判斷文件名是否可用"""
return False
def url(self, name):
"""返回訪問(wèn)文件的url路徑"""
return self.base_url+name
- 配置文件fdfs
# 設(shè)置Django的文件存儲(chǔ)類
DEFAULT_FILE_STORAGE = 'utils.fdfs.storage.FDFSStorage'
# 設(shè)置fdfs使用的client.conf文件路徑
FDFS_CLIENT_CONF = './utils/fdfs/client.conf'
# 設(shè)置fdfs存儲(chǔ)服務(wù)器上nginx的IP和端口號(hào)
FDFS_URL = 'http://192.168.191.134:8888/'
WX20171215-114634.png
3. 商品首頁(yè)
- 動(dòng)態(tài)給對(duì)象增加屬性
4. 使用redis保存購(gòu)物車信息
- 用戶點(diǎn)擊加入購(gòu)物車時(shí)需要添加購(gòu)物車記錄
- 用戶訪問(wèn)購(gòu)物車頁(yè)面時(shí)獲取用戶的購(gòu)物車記錄
- 每個(gè)用戶的購(gòu)物車記錄用一條數(shù)據(jù)保存
hash:cart_用戶id: {'sku_id1': 數(shù)量, 'sku_id2': 數(shù)量} - 統(tǒng)計(jì)hash中元素的數(shù)量: hlen
5. 頁(yè)面靜態(tài)化
-
把原本動(dòng)態(tài)的頁(yè)面處理結(jié)果保存成html文件, 讓用戶直接訪問(wèn)生成出來(lái)的靜態(tài)html頁(yè)面
WX20171215-193136.png 什么時(shí)候需要重新生成靜態(tài)頁(yè)面摊唇?
管理員在后臺(tái)修改了首頁(yè)上數(shù)據(jù)表里的信息的時(shí)候比肄,需要重新生成靜態(tài)頁(yè)面逢倍。
celery
當(dāng)修改了數(shù)據(jù)庫(kù)數(shù)據(jù)時(shí)會(huì)調(diào)用管理類里的save_model或者delete_model
from django.contrib import admin
from goods.models import GoodsType, GoodsSKU, IndexGoodsBanner, IndexTypeGoodsBanner, IndexPromotionBanner
from django.core.cache import cache
# Register your models here.
class BaseModelAdmin(admin.ModelAdmin):
def delete_model(self, request, obj):
super().delete_model(request, obj)
# 發(fā)出任務(wù)
from celery_tasks.tasks import generate_static_index_html
generate_static_index_html.delay()
# 刪除緩存
cache.delete('index_page_data')
def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
# 發(fā)出任務(wù)
from celery_tasks.tasks import generate_static_index_html
generate_static_index_html.delay()
# 刪除緩存
cache.delete('index_page_data')
class IndexGoodsBannerAdmin(BaseModelAdmin):
pass
admin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)
6. 數(shù)據(jù)緩存
把頁(yè)面用到的數(shù)據(jù)緩存起來(lái)忍法,如果使用這些數(shù)據(jù)的時(shí)候逮矛,先從緩存中獲取蜗顽,
如果獲取不到坞嘀,再去查詢數(shù)據(jù)庫(kù)砚哆。什么時(shí)候緩存數(shù)據(jù)需要更新缩膝?
管理員在后臺(tái)修改了首頁(yè)上數(shù)據(jù)表里的信息的時(shí)候混狠,需要更新緩存數(shù)據(jù)。也是在管理器類
首頁(yè)數(shù)據(jù)緩存
class IndexView(View):
def get(self, request):
context = cache.get('index_page_data')
if context is None:
# 商品種類
types = GoodsType.objects.all()
# 獲取首頁(yè)輪播商品信息
goods_banners = IndexGoodsBanner.objects.all().order_by('index')
# 首頁(yè)促銷活動(dòng)信息
promotion_banners = IndexPromotionBanner.objects.all().order_by('index')
for type in types:
type.image_banner = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
type.title_banner = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')
context = {
'types': types,
'goods_banners': goods_banners,
'promotion_banners': promotion_banners
}
cache.set('index_page_data', context, 3600)
user = request.user
cart_count = 0
if user.is_authenticated():
conn = get_redis_connection('default')
cart_key = 'cart_%d' % user.id
cart_count = conn.hlen(cart_key)
context['cart_count'] = cart_count
return render(request, 'index.html', context)