[Database] Redis data types

table of Contents:

  1, automatic allocation (Redis)

    - Batch Import

  2, micro-channel automatic binding

  3, django's ORM can not do the operation, how to customize database operations

    extra

    ’

  4, report

    The company's monthly sales performance

  5, privileges

=================================

1, the automatic assignment, you use the automatic allocation at what time?

A: Marketing or Operations attracted new customers, a single (batch) data entry time, automatic assignment.

2, that is how you automatically assign it?

A: Based on the list redis implemented. It corresponds with the queue.

====================================

An automatic dispensing (Redis)

Put the data cache, in order to speed

redis support persistent, if shut down after the data is already in the file will be placed

To buy a computer: redis server software installed

  - The server has a working network IP: 47.93.4.198

  - Port: 6379

Our computer: install the module links redis

  - pip instaill redis

redis: that white is a software server, helps us save data in memory inside

 conn.lpush ( "names": "sss") # Wang Libian put value

 conn.lpush ( "names": * [local laws and regulations "," dfgdf "]) # put more value added from the left, equivalent to insert

 conn.rpush ( "names": * [local laws and regulations "," dfgdf "]) # put more value added from behind, equivalent to append

conn.lpop ( "names") # a value a from the inside, bytes type

conn.rpop ( "names") # values ​​from the inside, bytes type

 conn.llen ( "names") # View Length

Redis Import 
Conn = redis.Redis (Host = "192.168.20.150", Port = 6379) # =========== for string set, get set, a value obtained ======= ==== 
conn.set ( "K13", "v2") # written on a key to the remote redis in 
val = conn.get ( "k13") # get key-value pairs 
Print (Val) 
conn.set ( "names", "SS") 
val2 = conn.get ( "names") 
Print (val2) # =========== for the operation of the list: lpush operation and lpop operation, (from left to put value, the value from the left) ============= 
VAL4 = conn.lpush ( "names_s" * [ "swallow", "Liu"]) 
conn.lpush ( 'names_s', * [ 'the number', 'Luning']) # 'Lu Ning', 'the number', "Liu", "Swallow", 
conn.delete ( "names_s") 
V = conn.llen ( " names_s ") 
Print (conn.llen (" names_s ")). 4 # 
for I in Range (V): 
    . Print (conn.lpop ( "names_s") decode ( "UTF-. 8")) # ========== for operations list: rpush operation and rpop operations (from the right to put values, ranging from the right) ======






conn.rpush ( "ABCD", [l, 2,3]) 
conn.rpush ( "ABCD", * [ "A", "B", "C"]) # [l, 2,3], A, b, c # * representatives are unpacked, if not * go into is a list of 
# conn.delete ( "ABCD") 
v = conn.llen ( "ABCD") 
# Print (v) # 
for i in the Range (V): 
    Print (. conn.rpop ( "ABCD") decode ( "UTF-. 8")) # C, B, A, [l, 2,3]
 

 

Application in the project

Redis link, lists all the data on the redis where it rollback also placed inside redis

 

Turned out to be a iterator a value, we can now link redis, rpop a value of a

When the problem rollback (or not used), we can use it to add rpush it in, and then taken out

conn.lindex ( "said_id_list_origin", 0) # 0 corresponding to the value of the index View

 

The first run, only the database data

If the value is not accessible to the database, then the direct return None

Obtaining a next one, if taken to None, you have to take complete, then the standby list data back inside the discharge

Allocation table data inside if you need to update, then reset.

 

 
import redis
from crm import models

POOL = redis.ConnectionPool(host='47.93.4.198', port=6379, password='123123')
CONN = redis.Redis(connection_pool=POOL)

class AutoSale(object):

    @classmethod
    def fetch_users(cls):
        # [obj(销售顾问id,num),obj(销售顾问id,num),obj(销售顾问id,num),obj(销售顾问id,num),]
        sales = models.SaleRank.objects.all().order_by('-weight')

        sale_id_list = []
        count = 0
        while True:
            flag = False
            for row in sales:
                if count < row.num:
                    sale_id_list.append(row.user_id)
                    flag = True
            count += 1
            if not flag:
                break

        if sale_id_list:
            CONN.rpush('sale_id_list', *sale_id_list)  # 自动pop数据
            CONN.rpush('sale_id_list_origin', *sale_id_list)  # 原来的数据
            return True
        return False

    @classmethod
    def get_sale_id(cls):
        # 查看原来数据是否存在
        sale_id_origin_count = CONN.llen('sale_id_list_origin')
        if not sale_id_origin_count:
            # 去数据库中获取数据,并赋值给: 原数据,pop数据
            status = cls.fetch_users()
            if not status:
                return None

        user_id = CONN.lpop('sale_id_list')
        if user_id:
            return user_id

        reset = CONN.get('sale_id_reset')
        # 要重置
        if reset:
            CONN.delete('sale_id_list_origin')
            status = cls.fetch_users()
            if not status:
                return None
            CONN.delete('sale_id_reset')
            return CONN.lpop('sale_id_list')
        else:
            ct = CONN.llen('sale_id_list_origin')
            for i in range(ct):
                v = CONN.lindex('sale_id_list_origin', i)
                CONN.rpush('sale_id_list', v)
            return CONN.lpop('sale_id_list')


    @classmethod
    def reset(cls):
        CONN.set('sale_id_reset',1)

    @classmethod
    def rollback(cls,nid):
        CONN.lpush('sale_id_list',nid)
 

 

总结:

  1、什么是redis?

  2、set,get对字符串做操作的,

  3、lpush,rpush,lpop,rpop对于列表做操作的

     lindex:取索引

     llen() :长度

  4、 delete :删除 :公共的

 扩展:

redis和我们的数据库一样,不能每次都链接,redis支持连接池

 不推荐

 推荐:

二、批量导入

上传excel文件,页面上显示

上传文件

1、拿到文件名和文件大小

file_obj.field_name:  文件名,

file_obj.size :文件大小

2、打开文件读取,以写的方式存起来

3、安装xlrd-1.1.0的两种方式

python setup.py  build

pip instail xlrd

4、打开excle文件做操作。拿到每一个单元格的数据,写入数据库

吧第一行排除,可以吧列表转换成字典,录入到数据库

5、作业

  自动获取ID

  录入客户表

     录入分配表

  不在创建临时xlsx文件,写在内存里面,

  写上一个模板文件,让用户去下载

6、文件,用户下载文件的两种方式

  吧文件写在静态文件里面,用a标签去跳转。但是这种当是可能不行

  设置响应头(搜索django如何实现文件下载)

三、微信自动绑定

前提:公司里的每一个人都得关注公众号,才可以给推送消息

Guess you like

Origin www.cnblogs.com/xqjoker/p/11127649.html