python web(bottle框架)知行合一之-簡單知識付費平臺-”全椞憧В“實踐(12)---緩存處理-分頁獲取課程列表信息接口
PS:筆記只是為了更好表達我怎么語言表述,有些時候可能難免廢話一推付鹿!
因知識有限, 如有錯誤, 歡迎指正比藻!
每日細語:傳說中,癡心的眼淚會傾城~
續(xù)言
這里為什么需要使用到緩存倘屹?關(guān)于緩存的重要性其實對應用層系統(tǒng)設(shè)計來說是必不可少的,因為頻繁數(shù)據(jù)庫鏈接和打開對數(shù)據(jù)庫系統(tǒng)來說壓力還是有的~所以我們在應用層中盡量還是需要引入緩存相關(guān)機制慢叨,避免頻繁進行數(shù)據(jù)庫操作處理纽匙。
那緩存其實又分內(nèi)存的緩存和其他redis或其他緩存,那我們這里就有必要封裝處理一下相關(guān)緩存操作拍谐,這里我自己使用的redis來進行處理緩存烛缔。
緩存常用的操作有:
- 緩存設(shè)置
- 緩存獲取
- 緩存清除
- 緩存按key清除
- 緩存有效期設(shè)置
- 緩存寫入磁盤
而對于緩存來說還是也會遇到一些問題如:
- 緩存穿透
- 緩存擊穿
- 緩存雪崩
對于上述幾個緩存可能遇到的問題點馏段,大家百度一下,其實都會有相關(guān)的答案践瓷,我這里且不會做太多的深入說明院喜,百度都知道的東西,好像再講也沒什么意思晕翠!
解決參考:
https://blog.csdn.net/fei33423/article/details/79027790
關(guān)于緩存庫
個人的推薦使用這個封裝好的庫喷舀,當然其實也可以自己封裝一個!
https://github.com/hustcc/wrapcache
相關(guān)的使用介紹其實github說的也很詳細了淋肾!我這里也就不細說硫麻!
我們直接安裝使用即可!
不過需要說明的一點就是一般它的裝飾器的方式的話樊卓,是對整個函數(shù)的結(jié)果進行緩存拿愧,而對于的KEY設(shè)置是什么,我暫時還不清楚碌尔!
所以我們使用的話一般直接的使用API的方式浇辜!
緩存redis_cache_helper.py工具類封裝:
1:安裝對應的wrapcache庫
2:編寫工具模塊
#!/usr/bin/evn python
# coding=utf-8
"""
Author = zyx
@Create_Time: 2018/4/25 22:39
@version: v1.0.0
@Contact: 308711822@qq.com
@File: redis_cache_helper.py
@文件功能描述:
"""
import wrapcache
import redis
from wrapcache.adapter.RedisAdapter import RedisAdapter
def init(_redis):
REDIS_POOL = redis.ConnectionPool(host=_redis.get('host', ''), port=_redis.get('post', ''),
db=_redis.get('db', ''),
password=_redis.get('password', ''),
socket_timeout=1, socket_connect_timeout=1)
REDIS_INST = redis.Redis(connection_pool=REDIS_POOL, charset='utf8')
RedisAdapter.db = REDIS_INST
def set(key='', value='', timeout=3000):
print('key', key)
print('v', value)
return wrapcache.set(key, value, timeout=timeout, adapter=RedisAdapter)
def get(key):
return wrapcache.get(key, adapter=RedisAdapter)
def remove(key):
return wrapcache.remove(key, adapter=RedisAdapter)
# clear all the cache.
def flush():
return wrapcache.flush(adapter=RedisAdapter)
from business_logic.configs import redis_config
if __name__ == '__main__':
init(_redis=redis_config.REDIS)
3:啟動的時候初始化好redis的配置
4:編寫對應的redis_config模塊
const = {
# 服務地址
'host': 'localhost',
# 服務端口
'post': 6379,
# 服務密碼
'password': '',
# 數(shù)據(jù)庫序號
'db': 2
}
5:修改獲取課程的course_logic.py邏輯處理
from business_logic.db_model.knowledgepay_model import session_scope, Course
from base_framework.cache import redis_cache_helper
# 分頁查詢
def get_course_paginate(page_num=1):
# 緩存的Key
cache_key_course_paginate = 'get_course_paginate' + str(page_num)
# 獲取緩存值
result_list = redis_cache_helper.get(cache_key_course_paginate)
# 判斷是否有值
if result_list:
is_ends = False
if not result_list:
is_ends = True
# 直接的從緩存中返回
print('直接的從緩存中返回')
return result_list, is_ends
# 否則從數(shù)據(jù)庫中進行讀取
print('否則從數(shù)據(jù)庫中進行讀取')
with session_scope():
result = Course.select().dicts().order_by(Course.id).paginate(int(page_num), 4)
result_list = []
is_ends = False
if result:
for row in result:
result_list.append(row)
if not result_list:
is_ends = True
# 把對應的結(jié)果保存到緩存中
redis_cache_helper.set(cache_key_course_paginate, result_list, timeout=10)
return result_list, is_ends
course_logic.py細節(jié)小優(yōu)化
#!/usr/bin/evn python
# coding=utf-8
"""
Author = zyx
@Create_Time: 2018/4/25 11:23
@version: v1.0.0
@Contact: 308711822@qq.com
@File: course_logic.py
@文件功能描述:
"""
from business_logic.db_model.knowledgepay_model import session_scope, Course
from base_framework.cache import redis_cache_helper
# 分頁查詢
def get_course_paginate(page_num=1):
# 緩存的Key
cache_key_course_paginate = 'get_course_paginate' + str(page_num)
# 獲取緩存值
result_list = redis_cache_helper.get(cache_key_course_paginate)
# 判斷是否有值
if result_list:
print('直接的從緩存中返回')
if result_list == 'null':
return [], True # 查詢沒有結(jié)果的時候
return result_list, False
# 否則從數(shù)據(jù)庫中進行讀取
print('否則從數(shù)據(jù)庫中進行讀取')
with session_scope():
result = Course.select().dicts().order_by(Course.id).paginate(int(page_num), 4)
if not result:
# 把對應的結(jié)果保存到緩存中---緩存穿透:處理
redis_cache_helper.set(cache_key_course_paginate, "null", timeout=20)
return [], True # 查詢沒有結(jié)果的時候
# for row in result: result_list.append(row)
result_list = [v for v in result] # 使用列表推導式
# 把對應的結(jié)果保存到緩存中
redis_cache_helper.set(cache_key_course_paginate, result_list, timeout=10)
# 返回最終查詢結(jié)果
return result_list, False
測試
結(jié)束
以上筆記純屬個人學習實踐總結(jié),有興趣的同學可以加群一起學習討論QQ:308711822