zoukankan      html  css  js  c++  java
  • redis购物车示例

    ##第一种,一般不用
    import
    redis import json conn = redis.Redis(host='192.168.170.138',port=6379,password='chenchen') # conn.flushall() v = conn.keys() print(v) """ # 用户ID: 6 redis={ luffy_shopping_car:{ 6:{ 11:{ 'title':'21天入门到放弃', 'src':'xxx.png' } } } } """ # 购买的第一商品 data_dict = { 11:{ 'title':'21天入门到放弃', 'src':'xxx.png' } } print(json.dumps(data_dict)) #{"11": {"title": "21u5929u5165u95e8u5230u653eu5f03", "src": "xxx.png"}} conn.hset('luffy_shopping_car','6',json.dumps(data_dict)) #data_dict是字典,要用json.dumps转换成字符串 print(conn.hgetall('luffy_shopping_car')) #{b'6': b'{"11": {"title": "21\u5929\u5165\u95e8\u5230\u653e\u5f03", "src": "xxx.png"}}'} ##如果用下面这种添加,上面添加的会被覆盖,添加不成功 # data_dict = { # 13:{ # 'title':'23天入门到放弃', # 'src':'xxx.png' # } # } # # print(data_dict) # # conn.hset('luffy_shopping_car','6',json.dumps(data_dict)) # print(conn.hgetall('luffy_shopping_car')) #{b'6': b'{"13": {"title": "23\u5929\u5165\u95e8\u5230\u653e\u5f03", "src": "xxx.png"}}'} #******要想添加成功 # 购买的第二商品 # 需要先get回来 car = conn.hget('luffy_shopping_car','6') print(car) #这是一个字节类型b'{"11": {"title": "21\u5929\u5165\u95e8\u5230\u653e\u5f03", "src": "xxx.png"}}' # v = car.decode('utf-8') #把字节转换成字符串 # print(v) #{"11": {"title": "21u5929u5165u95e8u5230u653eu5f03", "src": "xxx.png"}} car_str = str(car,encoding='utf-8') ##把字节转换成字符串 print(car_str) #{"11": {"title": "21u5929u5165u95e8u5230u653eu5f03", "src": "xxx.png"}} # ret = bytes(car_str,encoding='utf-8') #把字符串转换成字节 # print(ret) #b'{"11": {"title": "21\u5929\u5165\u95e8\u5230\u653e\u5f03", "src": "xxx.png"}}' car_dict = json.loads(car_str) print(car_dict) #字节转换成字符串,字符串转换成字典,字典里面添加值,然后再添加到redis中 car_dict['12'] = { 'title':'22天入门到放弃', 'src':'xxx.png' } print(car_dict) conn.hset('luffy_shopping_car','6',json.dumps(car_dict)) print(conn.hgetall('luffy_shopping_car')) #{b'6': b'{"11": {"title": "21\u5929\u5165\u95e8\u5230\u653e\u5f03", "src": "xxx.png"}, "12": {"title": "22\u5929\u5165\u95e8\u5230\u653e\u5f03", "src": "xxx.png"}}'}

    第二种

    import redis
    import json
    conn = redis.Redis(host='192.168.170.138',port=6379,password='chenchen')
    # #
    conn.flushall()
    
    #添加课程
    redis_key = "chen_shopping_car_%s_%s" %(9,18,)
    conn.hmset(redis_key,{'title':'python基础','src':'111.png'})
    print(conn.hgetall(redis_key))  #{b'title': b'pythonxe5x9fxbaxe7xa1x80', b'src': b'111.png'}
    
    print(conn.keys())  #[b'chen_shopping_car_9_18']
    # #删除课程
    # conn.delete('chen_shopping_car_9_18')
    # print(conn.keys())  #[]
    
    #修改课程
    
    conn.hset('chen_shopping_car_9_18','src','666.png')
    print(conn.keys())
    print(conn.hget('chen_shopping_car_9_18','src'))    #b'666.png
    #
    conn.hset('chen_shopping_car_9_19','src','999.png')
    # #查看所有课程
    print(conn.keys('chen_shopping_car_9_*'))   #[b'chen_shopping_car_9_19', b'chen_shopping_car_9_18']
    
    for item in conn.scan_iter('chen_shopping_car_9_*',count=10):
        print(item) #b'chen_shopping_car_9_19'  'chen_shopping_car_9_18'
        course = conn.hgetall(item)
        print(course)   #{b'src': b'999.png'}   b'title': b'pythonxe5x9fxbaxe7xa1x80', b'src': b'666.png'}
    
    # conn.flushall()
    # print(conn.keys())
    # for key in conn.scan_iter('chen_shopping_cart_2_1'):
    #     title = conn.hget(key, 'title')
    #     img = conn.hget(key, 'img')
    #     price = conn.hget(key, 'price')
    #     conn.hget(key, 'default_price')
    #
    #     print(str(title, "utf-8"))
    #     print(str(img, "utf-8"))
    #     print(json.loads(str(price, "utf-8")))
    #     print(str(key, "utf-8"))
    #
    # print(conn.exists('chen_shopping_cart_2_1'))
    # conn.hgetall('chen_shopping_cart_2_3')
    # title = conn.hget('chen_shopping_cart_2_3','title')
    # print(str(title,"utf-8"))
    # img = conn.hget('chen_shopping_cart_2_3','img')
    # print(str(img,"utf-8"))
    # # print(conn.hget('chen_shopping_cart_2_3','price'))
    # price = conn.hget('chen_shopping_cart_2_3','price') #字典
    # print(json.loads(str(price,"utf-8")))
    # default_price = conn.hget('chen_shopping_cart_2_3','default_price')
    # print(str(default_price,"utf-8"))
  • 相关阅读:
    前后台分离--概念相关
    dubbo概述
    memcache
    分布式系统事务
    2018书单索引
    Lucene原理之概念
    java 通用对象排序
    LightOJ1214 Large Division —— 大数求模
    LightOJ1336 Sigma Function —— 质因子分解、约数和为偶数
    LightOJ1245 Harmonic Number (II) —— 规律
  • 原文地址:https://www.cnblogs.com/chvv/p/10188554.html
Copyright © 2011-2022 走看看