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

原文地址:https://www.cnblogs.com/anle123/p/13365405.html