python之路_redis相關介紹

來源:互聯網
上載者:User

標籤:檔案   mem   views   總結   伺服器   資料格式   儲存   介紹   rom   

一、Redis介紹

  什麼是redis?一個專門做資料緩衝的軟體,將資料在伺服器的記憶體上進行儲存,提高資料的查詢的效率。與memcache不同的是,redis將資料儲存在記憶體中的同時,會定期將資料也會寫到檔案中,避免伺服器關機等時資料消失。電腦開機的會將資料重新新放在記憶體上。總結如下:

‘‘‘1、用途:     做緩衝和做訊息佇列2、本質:     將資料存放在記憶體中    3、特性:     可做持久化     支援存放5種資料格式,如下:      ‘‘‘ {"k1":"xxxx"                                    #字串"k2":[11,22,33]                                 #列表"k3":{11,22,33}                                 #集合"k4":{"k1":"v1","k2":"v2"}                      #雜湊散列表"k5":{("egon",1),("alex",2)}                    #有序集合}

 二、redis-py串連池

  redis-py使用connection pool來管理對一個redis server的所有串連,避免每次建立、釋放串連的開銷。預設,每個Redis執行個體都會維護一個自己的串連池。可以直接建立一個串連池,然後作為參數Redis,這樣就可以實現多個Redis執行個體共用一個串連池。

1、方式一

  利用redis中的ConnectionPool建立串連池,然後各視圖中使用此串連池,執行個體如下:

#pool.py檔案import redisPOOL = redis.ConnectionPool(host=‘47.93.4.198‘,port=6379)                    #views.py檔案from django.shortcuts import renderimport redisfrom pool import POOLdef index(request):conn = redis.Redis(connection_pool=POOL)....def home(request):conn = redis.Redis(connection_pool=POOL)....

2、方式二

  利用django-redis組件實現串連池,安裝django-redis組件方式為:pip3 install django-redis ,使用執行個體如下:

設定檔:

CACHES = {        "default": {                "BACKEND": "django_redis.cache.RedisCache",                "LOCATION": "redis://47.93.4.198:6379",                           #伺服器ip                "OPTIONS": {                        "CLIENT_CLASS": "django_redis.client.DefaultClient",                        "PASSWORD": "",                                            #有密碼就設定                            }                }        }

使用執行個體:

import django_redisconn = django_redis.get_redis_connection()

 

python之路_redis相關介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.