2018-11-14 10:07:39
购物车有思路,用django中redis来做!!!具体思路参考下面笔记
回学校后,把笔记里面面试题都认真整理一下!!!!!!
越努力,越幸运!永远不要高估自己!
redis用于操作内存!可以用django内置的redis做,详情参考
关于 redis具体使用可以参考: http://www.cnblogs.com/wupeiqi/articles/5132791.html
在Django中使用redis 可以参考 : http://www.cnblogs.com/wupeiqi/articles/5246483.html
redis列表操作
import redis """ redis = { k1:[1,2,3,43,45] } """ conn = redis.Redis(host='47.94.172.250',port=6379,password='Luffy!4321') # 左插入 # conn.lpush('k1',11) # 右插入 # conn.rpush('k1',33) # 左获取 # val = conn.lpop('k1') # val = conn.blpop('k1',timeout=10) # hang住 # 右获取 # val = conn.rpop('k1') # val = conn.brpop('k1',timeout=10) # hang住 # conn.lpush('k1',*[11,22,33,44,55,66,77,88,99,66,666,2234,345234,234]) def list_iter(key,count=100):
""""
生成器实现列表读取,
使用了yield 为数不多的自己使用了yield
"""" index = 0 while True: data_list = conn.lrange('k1', index, index+count-1) if not data_list: return index += count for item in data_list: yield item print(conn.lrange('k1',0,101)) for item in list_iter('k1',count=3): print(item)
redis中pipeline的使用
import redis """ redis = { k1:[1,2,3,43,45] } """ conn = redis.Redis(host='47.94.172.250',port=6379,password='Luffy!4321') pipe = conn.pipeline(transaction=True) pipe.multi() pipe.set('k2','123') pipe.hset('k3','n1',666) pipe.lpush('k4','laonanhai') pipe.execute()
贴上笔记!!!
s9day111 内容回归: 1. django rest framework 2. git协同开发 a. 怎么通过git做得协同开发? b. 是否做代码review? c. 开发过程中出现bug如何做? d. git rebase作用? e. 给别人开源代码贡献力量。 f. 使用的github、gitlab ? PS:隐藏明感信息 3. orm操作 - select_related,连表操作,相当于主动做join - prefeth_related,多次单表操作,先查询想要的数据,然后构造条件,如:id=[1,2,3],再次查询其他表根据id做条件。 - only - defer - F - Q - 通过ORM写偏原生SQL: - extra Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,)) Entry.objects.extra(where=['headline=%s'], params=['Lennon']) Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"]) Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid']) - raw # 执行原生SQL models.UserInfo.objects.raw('select * from userinfo') # 如果SQL是其他表时,必须将名字设置为当前UserInfo对象的主键列名 models.UserInfo.objects.raw('select id as nid from 其他表') # 为原生SQL设置参数 models.UserInfo.objects.raw('select id as nid from userinfo where nid>%s', params=[12,]) name_map = {'first': 'first_name', 'last': 'last_name', 'bd': 'birth_date', 'pk': 'id'} Person.objects.raw('SELECT * FROM some_other_table', translations=name_map) - 原生SQL from django.db import connection, connections cursor = connection.cursor() # cursor = connections['default'].cursor() cursor.execute("""SELECT * from auth_user where id = %s""", [1]) row = cursor.fetchone() # fetchall()/fetchmany(..) PS: 选择数据库 queryset = models.Course.objects.using('default').all() 4. redis - 是否可以持久化?AOF、RDB - 单进程、单线程 - 5大数据类型 - 字典操作: - scan_iter 5. 跨域 - JSONP - CORS 6. Http协议 & django 生命周期 & wsgi & FBV、CBV 7. 中间件 8. restful 规范 9. 支付 - rsa - 商户私钥+支付宝公钥 - 精度 -宕机 10. 原生Ajax - XMLHttpRequest 今日内容: - redis - 列表 - 微信消息推送 - 支付相关表结构 内容详细: - redis - 左右操作 - 阻塞 - 通过yield创建一个生成器完成一点一点获取(通过字典操作的源码来的灵感) def list_iter(key,count=100): index = 0 while True: data_list = conn.lrange('k1', index, index+count-1) if not data_list: return index += count for item in data_list: yield item PS: 队列:先进先出 栈:后进先出 - 事务+一次发送多个命令: conn = redis.Redis(host='47.94.172.250',port=6379,password='Luffy!4321') pipe = conn.pipeline(transaction=True) pipe.multi() pipe.set('k2','123') pipe.hset('k3','n1',666) pipe.lpush('k4','laonanhai') pipe.execute() - 微信消息推送 - 公众号 - 已认证公众号 - 服务号 - 已认证服务号 - 企业号 基于:微信认证服务号 主动推送微信消息。 前提:关注服务号 环境:沙箱环境 总结: 1. 注册账号 appID:wx89085e915d351cae appsecret:64f87abfc664f1d4f11d0ac98b24c42d 网页授权获取用户基本信息:47.98.134.86 或 域名 2. 关注公众号(已认证的服务号) 3. 生成二维码,用户扫描; 将用户信息发送给微信,微信再将数据发送给设置redirect_uri地址(md5值) 4. 回调地址:47.98.134.86/callback/ - 授权 - 用户md5 - 获取wx_id 在数据库中更新设置:wx_id 5. 发送消息(模板消息) - wx_id - access_token(2小时有效期) - 支付相关: 1. 加入购物车,保存到redis 理由: a. 临时状态 b. 修改购物信息 结构: redis->{ shopping_car:{ 用户ID:{ 课程1:{ title:'金融量化分析入门', img:'/xx/xx/xx.png', policy:{ 10: {'name':'有效期1个月','price':599}, 11: {'name':'有效期3个月','price':1599}, 13: {'name':'有效期6个月','price':2599}, }, default_policy:12 }, 课程2:{ title:'金融量化分析入门', img:'/xx/xx/xx.png', policy:{ 10: {'name':'有效期1个月','price':599}, 11: {'name':'有效期3个月','price':1599}, 13: {'name':'有效期6个月','price':2599}, }, default_policy:10 } }, 用户ID:{...}, } } POST请求:购物车中添加一条数据 请求体: { courseid:1, policy_id:10 } 后台: 检验当前课程是否有此价格策略,合法:将数据构造字典,再添加到redis GET请求:查看自己购物车中的所有数据 获取当前登录用户ID,根据用户ID去redis的购物车中获取数据。 DELETE请求:删除购物车中的数据 请求体: { course_ids:[1,2] } PUT/PATCH请求:更新价格策略 请求体: { courseid:1, policy_id:13 } 注意:不要写vue、不要写vue、不要写vue 2. 结算中心 3. 去支付 总结: 1. redis列表操作 - 前后都可以 - 等 hang住 - yield - pipeline 2. 微信消息推送 - 已认证服务号 - 模板消息 - 生成二维码 3. 路飞购物车 - 为什么使用redis? - 字典是如何构造? 作业: 1. django orm操作 2. 购物车 - django-redis组件