Cart storage solutions

A complete shopping cart record includes: the user, product, quantity, check status

Login user storage Type Description

  • User, commodity, quantity: hash
carts_user_id: {sku_id1: count, sku_id3: count, sku_id5: count, ...}
  • Check status: set
selected_user_id: [sku_id1, sku_id3, ...]

No login

  • Since the user is not logged in, the server can not get the user's ID, so the server when generating cart recording is difficult to uniquely identify the record,
    we can not logged in user's shopping cart data in the cookie to the user's browser cache
  • JSON string data string can describe complex structures, to ensure that a cart can not be stored separately from record
{
    "sku_id1":{
        "count":"1",
        "selected":"True"
    },
    "sku_id3":{
        "count":"3",
        "selected":"True"
    },
    "sku_id5":{
        "count":"3",
        "selected":"False"
    }
}

Tip:
browser cookie is a string stored in plaintext data.
We need this kind of cart data encrypted data privacy.
Solution: pickle module and base64 module

Use the pickle module

pickle modules are standard modules python
provides the python for serialization data bytes can be converted to data type

pickle module provides two methods:

pickle.dumps( )

  • The serialized data bytes type python

pickle.loads( )

  • The type of data bytes deserialized data type python (dictionary, objects, etc.)
>>> import pickle

>>> d = {'1': {'count': 10, 'selected': True}, '2': {'count': 20, 'selected': False}}
>>> s = pickle.dumps(d)
>>> s
b'\x80\x03}q\x00(X\x01\x00\x00\x001q\x01}q\x02(X\x05\x00\x00\x00countq\x03K\nX\x08\x00\x00\x00selectedq\x04\x88uX\x01\x00\x00\x002q\x05}q\x06(h\x03K\x14h\x04\x89uu.'
>>> pickle.loads(s)
{'1': {'count': 10, 'selected': True}, '2': {'count': 20, 'selected': False}}

Use base64 module

Base64 is based on 64 printable characters to represent binary data representation.
Since 6 = 2 ^ 64, so every six bits as a unit, corresponding to a printable character. 3 bytes of 24 bits, corresponding to four Base64 units, i.e. 3 bytes by four printable characters to represent.
Base64 printable characters includes the letters AZ, az, 0-9, so that a total of 62 characters, in addition to two symbols can be printed in various different systems.
Base64 commonly used in the normal process in the case of text data, said transmission, storing some binary data, including some complex data MIME email and XML.

python standard library provided base64 module for converting

base64.b64encode( )

  • The bytes base64 encoding data type, returns the encoded bytes type

base64.b64deocde( )

  • The type base64 encoded bytes to decode, the type of the return decoded bytes
>>> import base64
>>> s
b'\x80\x03}q\x00(X\x01\x00\x00\x001q\x01}q\x02(X\x05\x00\x00\x00countq\x03K\nX\x08\x00\x00\x00selectedq\x04\x88uX\x01\x00\x00\x002q\x05}q\x06(h\x03K\x14h\x04\x89uu.'
>>> b = base64.b64encode(s)
>>> b
b'gAN9cQAoWAEAAAAxcQF9cQIoWAUAAABjb3VudHEDSwpYCAAAAHNlbGVjdGVkcQSIdVgBAAAAMnEFfXEGKGgDSxRoBIl1dS4='
>>> base64.b64decode(b)
b'\x80\x03}q\x00(X\x01\x00\x00\x001q\x01}q\x02(X\x05\x00\x00\x00countq\x03K\nX\x08\x00\x00\x00selectedq\x04\x88uX\x01\x00\x00\x002q\x05}q\x06(h\x03K\x14h\x04\x89uu.'

E.g:

# 导入
import pickle
import base64

if __name__ == '__main__':
    # 定义一个字典:
    cart_dict = {
        1: {
            'count':2,
            'selected':True
        },
        3: {
           'count':3,
            'selected':False
        }
    }

    # 将字典转为 二进制类型
    result_bytes =  pickle.dumps(cart_dict)
    print(result_bytes)
    print(type(result_bytes))
    print('---'*16)

    # 将 16进制 表示的二进制类型 编码为 base64格式
    result_base64 = base64.b64encode(result_bytes)
    print(result_base64)
    print(type(result_base64))
    print('---'*16)

    # 将 base64格式 解码得到 字符串( str )
    result = result_base64.decode()
    print(result)
    print(type(result))
    print('---'*16)

    # 我们一般使用的时候, 会连着使用:
    result2 = base64.b64encode(pickle.dumps(cart_dict)).decode()
    print(result2)
    print('---'*16)

    # 如果想要返回去, 得到最初的 字典类型数据, 又该怎么做呢?
    result3 = pickle.loads(base64.b64decode(result2))
    print(result3)

result:

b'\x80\x03}q\x00(K\x01}q\x01(X\x05\x00\x00\x00countq\x02K\x02X\x08\x00\x00\x00selectedq\x03\x88uK\x03}q\x04(h\x02K\x03h\x03\x89uu.'
<class 'bytes'>
------------------------------------------------
b'gAN9cQAoSwF9cQEoWAUAAABjb3VudHECSwJYCAAAAHNlbGVjdGVkcQOIdUsDfXEEKGgCSwNoA4l1dS4='
<class 'bytes'>
------------------------------------------------
gAN9cQAoSwF9cQEoWAUAAABjb3VudHECSwJYCAAAAHNlbGVjdGVkcQOIdUsDfXEEKGgCSwNoA4l1dS4=
<class 'str'>
------------------------------------------------
gAN9cQAoSwF9cQEoWAUAAABjb3VudHECSwJYCAAAAHNlbGVjdGVkcQOIdUsDfXEEKGgCSwNoA4l1dS4=
------------------------------------------------
{1: {'count': 2, 'selected': True}, 3: {'count': 3, 'selected': False}}

Supplementary:
base64.b64encode (byte stream byte):
the byte stream of bytes base64 encoding, content (bytes byte stream) before returning coding
base64.b64decode (bytes byte stream & str):
The parameter base64 decoding, decoding returns after the contents (bytes byte stream)

Guess you like

Origin www.cnblogs.com/oklizz/p/11234158.html