django中簡單使用redis的兩種方式

2021-10-14 03:59:48 字數 2289 閱讀 8060

utils資料夾下,建立redis_pool.py

import redis

pool = redis.connectionpool(host=

'127.0.0.1'

, port=

6379

, max_connections=

1000

)

路由: user/urls.py

from user import views

urlpatterns =

[ path(

'index/'

,views.index)

, path(

'order/'

,views.order)

, path(

'test_redis/'

,views.test_redis)

,]

檢視函式中使用:

# 示例1

'設定成功'

'獲取成功'

)# 示例2

# 連線redis 方式一

'人的年齡,從redis中取出來了是: %s'

%age)安裝django-redis模組

pip3 install django-redis
setting裡配置:

# redis配置

caches =

# "password": "123",}}

}

路由: user/urls.py

from user import views

urlpatterns =

[ path(

'test_django_redis'

,views.test_django_redis)

]

檢視函式

from django_redis import get_redis_connection

deftest_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中

'人的年齡是: %s'

%age)

Django 中celery的簡單使用

celery的本質 通過提前建立的程序呼叫函式來實現非同步的任務。任務執行者 worker 提前建立的程序,呼叫對應的函式來實現非同步任務。任務發出者 傳送需要worker執行的任務函式的資訊。中間人 broker 任務佇列,儲存任務發出者發出的資訊。1 任務執行者 建立的程序 可以單獨在另一台電腦...

Django中celery的使用 非常簡單的用法

這裡主要展示乙個最簡單的django中的celery任務,為了讓大家都可以用上celery。話不多說,首先給大家看一下我的目錄 這個專案安裝的環境 感覺有幾個沒用到,反正先裝上 1 python 3.5.2 2 django 2.1.15 3 celery 3.1.26.post2 4 django...

django 簡單使用xadmin

設定xadmin 在應用的urls.py中匯入xadmin 新增url path xadmin xadmin.site.urls 資料庫遷移 生成資料庫執行檔案 python manage.py makemigrations 資料庫遷移 python manage.py migrate 設定超級管理...