django+redis使用方法


  -非關系型內存數據(nosql:mongodb,redis),key-value的存儲
  -單線程單進程,qps:10w
  -Memcached:多線程,支持的數據類型少:只支持字符串類型,不支持持久化
  -redis:5大數據類型
    k1:‘123‘, 字符串
    k2:[1,2,3,4], 列表/數組
    k3:{1,2,3,4} 集合:去重,爬蟲去重
    k4:{name:lqz,age:12} 字典/哈希表
    k5:{(‘lqz‘,18),(‘egon‘,33)} 有序集合:游戲排行榜
  -redis支持持久化:兩種持久化的方案

-python操作redis
  from redis import Redis
  conn=Redis()

-redis連接池
  #pool需要做成單例
  pool=ConnectionPool(host=‘127.0.0.1‘,port=6379,max_connections=100)
  conn=Redis(connection_pool=pool)

-redis之字符串操作
  -set
  -get
  -mset
  -mget
  -incr
  -decr
  -append

-redis之hash操作
  -hset
  -hmset
  -hget
  -hmget
  -hgetall
  -hlen
  -hdel
  -hincrby
  -hscan
  -hscan_iter

-redis之列表操作
  -lpush
  -llen
  -linsert
  -lpop
  -blpop

  -自定義增量迭代

只支持一層的5大數據類型:也就是說字典的value值只能是字符串,列表的value值只能是字符串

-redis的其他操作
  -delete
  -exisit
  -rename
  -keys 模糊匹配key值
  -expire
  -type

-事務(重點)
  conn=Redis()
  pipe = conn.pipeline(transaction=True)
  pipe.multi()
  pipe.set(‘name‘, ‘alex‘)
  pipe.set(‘role‘, ‘sb‘)
  pipe.execute()

-在django中使用redis
  -所有框架都能用的方式:
    -先新建一個py文件,生成一個redis連接池
    -在哪用,導過來,
      conn=Redis(connection_pool=POOL)
      conn.set(‘xxx‘,‘yyyy‘)
  -django中使用:django-redis模塊
    -在setting中配置:
      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}}}}
    -使用
      在使用的位置:
      from django_redis import get_redis_connection
      conn=get_redis_connection()
      conn.set


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM