1. 程式人生 > 實用技巧 >redis之列表操作及在django中操作等相關內容-124

redis之列表操作及在django中操作等相關內容-124

1 redis之列表操作

import redis

#
# class MyRedis():
# def __enter__(self):
# self.conn = redis.Redis()
# return self.conn
#
# def __exit__(self, exc_type, exc_val, exc_tb):
# self.conn.close()
#
#
# with MyRedis() as conn:
# 列表操作
# 1 lpush(name,values)
# conn.lpush('l1',1,2,3,4,5,'lqz')

# 2 rpush
# conn.rpush('l1','egon')

# 3 lpushx(name,value)
# 在name對應的list中新增元素,只有name已經存在時,值新增到列表的最左邊
# conn.lpushx('l2',9)
# conn.lpushx('l1',9)

# llen(name)
# res=conn.llen('l1')
# print(res)

# linsert(name, where, refvalue, value)) 在某個位置插入值
# where :BEFORE或AFTER,大小寫都行
# refvalue: lqz 表示以lqz為標準(不是下標索引)
# conn.linsert('l1','after','lqz','劉亦菲')
# conn.linsert('l1','before','lqz','baby')

# lset
# conn.lset('l1',0,'黃曉明')
# conn.lset('l1',5,'老夥計')

# lrem
# 第二個引數count:
# count = 0,刪除列表中所有的指定值;
# count=2,從前到後,刪除2個;
# count=-2,從後向前,刪除2個
# conn.lrem('l1',0,'lqz') # 把所有lqz都刪除
# conn.lrem('l1',2,'lqz') # 從前往後,刪除2個lqz
# conn.lrem('l1',-1,'lqz') # 從前往後,刪除2個lqz

# lpop
# res=conn.lpop('l1')

# res=conn.rpop('l1')
# print(res)

# lindex(不刪除)
# res=conn.lindex('l1',0)
# print(res)

# lrange # 前閉後閉區間
# res = conn.lrange('l1', 0, 0)
# print(res)

# ltrim
# res=conn.ltrim('l1',3,5)
# print(res)


# rpoplpush 需要兩個列表
# conn.lpush('l2','lqz','egon')
# conn.rpoplpush('l1','l1')


# blpop # block:阻塞 左邊彈出

# res=conn.blpop('l1')
# print(res)


'''
lpush
llen
linsert
lset
lrem
lpop
lrange # 使用它,自定義增量迭代
blpop
'''

### redis類庫中沒有提供對列表元素的增量迭代,藉助lrange

import redis

conn = redis.Redis()
# res=conn.lrange('l2',0,9999) # 全部取出來
# res=conn.lrange('l2',0,conn.llen('l2')) # 從0取到列表長度


# 類似於字典的hgetall,一次性全取出來,存在的問題是,因為不知道列表有多大,很有可能撐爆記憶體
# 我們實現一個增量迭代
# print(res)

# for i in range(10000):
# conn.lpush('l_test','test_%s'%i)


# res=conn.lrange('l2',0,conn.llen('l2'))
# print(res)
# res = conn.lrange('l_test', 0, 9)
# print(res)


# def lscan_iter(name, conn, count=10):
# cursor = 0
# lenght = conn.llen(name) # 計算列表總長度
# while cursor < lenght:
# data = conn.lrange(name, cursor, (cursor+count) - 1)
# cursor += count
#
# for item in data:
# yield item

def lscan_iter(name, conn, count=10):
cursor = 0
while True:
data = conn.lrange(name, cursor, (cursor+count) - 1)
if data:
cursor += count
for item in data:
yield item
else:
break

for i in lscan_iter('l_test',conn,20):
print(i)

2 redis之其它操作

import redis


## 公共操作

# 搭建哨兵。用了叢集,這個模組就不夠用了
# conn=redis.Redis()

# delete
# res=conn.delete('l2','name')


#exists
# res=conn.exists('l1','l_test','l2')
# res=conn.exists('l_test')

# expire
# res=conn.expire('l_test',5)

# rename
# conn.lpush('l1',1,2,3)
# res=conn.rename('l1','l2')

#
# res=conn.move('l2',3)

## redis的庫是隔離的
conn = redis.Redis()
# res=conn.lpop('l2')


# 隨機出一個key值
# res=conn.randomkey()


# conn.sadd('choujiang',9)
# conn.sadd('choujiang',90)
# conn.sadd('choujiang',99)
# print(res)
# res=conn.spop('choujiang')
# print(res)




# type----檢視型別



# res=conn.type('choujiang')
# res=conn.type('ss')
res=conn.type('zzz')
print(res)
conn.close()

3 django中使用redis

3.1 通用方案

redis_pool.py

import redis

POOL=redis.ConnectionPool(host='127.0.0.1',port=6379,max_connections=100)

views.py

from utils.redis_pool import POOL
import redis
def test_redis(request):
conn=redis.Redis(connection_pool=POOL)
age=str(conn.get('age'),encoding='utf-8')

return HttpResponse('人的年齡,從redis中取出來了是:%s'%age)

3.2 django提供的方案

pip3 install django-redis

配置檔案

# redis的配置
#以後django的快取,用的就是redis,很方便使用redis的連線
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
# "PASSWORD": "123",
}
}
}

views.py

from django_redis import get_redis_connection
def test_django_redis(request):
# 從連線池中拿到連線
conn=get_redis_connection()

age = str(conn.get('age'), encoding='utf-8')

from django.core.cache import cache
cache.set('name','lqz',4) # 往快取中放key和value,其實放到了redis中了


cache.set('xxx',test_redis)
return HttpResponse('人的年齡是:%s' % age)

4 celery簡介,架構

1 celery:芹菜(跟芹菜沒有任何關係)
2 python中的一個分散式非同步任務框架
-執行非同步任務---(對立:同步任務):解決耗時任務,將耗時操作任務提交給Celery去非同步執行,比如傳送簡訊/郵件、訊息推送、音視訊處理等等
-執行延時任務(5分鐘後幹一件事):解決延遲任務
-執行定時任務:每天,隔幾分鐘,幹什麼事:解決週期(週期)任務,比如每天資料統計
3 解釋
Celery is a project with minimal funding, so we don’t support Microsoft Windows. Please don’t open any issues related to that platform.

4 celery特點(瞭解)
1)可以不依賴任何伺服器,通過自身命令,啟動服務(內部支援socket)
2)celery服務為為其他專案服務提供非同步解決任務需求的

5 Celery架構
Celery的架構由三部分組成,訊息中介軟體(message broker)、任務執行單元(worker)和 任務執行結果儲存(task result store)組成

#訊息中介軟體
Celery本身不提供訊息服務,但是可以方便的和第三方提供的訊息中介軟體整合。包括,RabbitMQ, Redis等等

# 任務執行單元
Worker是Celery提供的任務執行的單元,worker併發的執行在分散式的系統節點中。

# 任務結果儲存
Task result store用來儲存Worker執行的任務的結果,Celery支援以不同方式儲存任務的結果,包括AMQP, redis等

安裝

pip install celery