Exemplo n.º 1
0
def order_single_food(food):
	food_id = food['food_id']
	count = food['count']
	k = food_key(food_id)
	redis_store = get_redis_store()
	if redis_store.incrby(k, -count) < 0:
		redis_store.incrby(k, count)
		return False
	return True
Exemplo n.º 2
0
def cache_users_data():
    db = get_db()
    all_users = db.select("select * from user")
    db.close()
    for u in all_users:
        user_cache["dd.user%s.password%s" % (u[1], u[2])] = u[0]
    r = get_redis_store()
    user_cache["dd.user.min_id"] = int(r.hget("dd.user", "min_id"))
    user_cache["dd.user.max_id"] = int(r.hget("dd.user", "max_id"))
Exemplo n.º 3
0
def all_orders():
	orders = []
	redis_store = get_redis_store()
	min_user_id = int(redis_store.hget('dd.user', 'min_id'))
	max_user_id = int(redis_store.hget('dd.user', 'max_id'))
	for user_id in range(min_user_id, max_user_id+1):
		order = user_order(user_id)
		if order:
			orders.append(order)
	return my_response(orders)
Exemplo n.º 4
0
def cache_foods_data():
    r = get_redis_store()
    food_cache["dd.food.min_id"] = int(r.hget("dd.food", "min_id"))
    food_cache["dd.food.max_id"] = int(r.hget("dd.food", "max_id"))
    food_cache["dd.food.json"] = r.get("dd.food.json")
    db = get_db()
    all_foods = db.select("select * from food")
    db.close()
    for f in all_foods:
        food_cache["dd.food%d.price" % f[0]] = f[2]
Exemplo n.º 5
0
def order_muti_foods(cart):
	for i in range(0, len(cart)):
		food_id = cart[i]['food_id']
		count = cart[i]['count']
		k = food_key(food_id)
		redis_store = get_redis_store()
		if redis_store.incrby(k, -count) < 0:
			for j in range(0, i+1):				
				redis_store.incrby(food_key(cart[j]['food_id'], cart[j]['count']))
			return False
	return True
Exemplo n.º 6
0
def check_login(name, password):
	# p = redis_store.hget("dd.user%s" % name, "password")
	# if p != password:
	# 	return False
	# access_token = redis_store.hget("dd.user%s" % name, "id")
	redis_store = get_redis_store()
	access_token = redis_store.get("dd.user%s.password%s" % (name, password))
	if access_token == None:
		return False
	user_id = int(access_token)
	# redis_store.set("dd.access%s" % access_token, user_id)
	return (user_id, access_token)
Exemplo n.º 7
0
def cart_data(cart_id):
	data = []
	redis_store = get_redis_store()
	fid_set = redis_store.smembers("dd.cart%s" % cart_id)
	for food_id in fid_set:
		count = redis_store.get("dd.cart%s.count%s" % (cart_id, food_id))
		if count:
			count = int(count)
		else:
			count = 0
		data.append({'food_id': int(food_id), 'count': count})
	return data
Exemplo n.º 8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import json
import urlparse
from time import time, sleep
from wsgiref.simple_server import make_server
from db_manager import get_db, get_redis_store
from random import random

import cache
cache.cache_users_data()
cache.cache_foods_data()

redis_store = get_redis_store()

############### special responses ###############

def my_response(data, status_code = 200, status = "ok"):
	r = {'status' : status,
		'status_code' : status_code}
	r['data'] = data if isinstance(data, str) else json.dumps(data)
	return r

def bad_req_1():
	return my_response({"code": "EMPTY_REQUEST", "message": "请求体为空"}, 400, "Bad Request")

def bad_req_2():
	return my_response({"code": "MALFORMED_JSON", "message": "格式错误"}, 400, "Bad Request")
Exemplo n.º 9
0
def set_user_order_id(user_id, order_id):
	redis_store = get_redis_store()
	redis_store.set("dd.order%d" % user_id, order_id)
Exemplo n.º 10
0
def user_order_id(user_id):
	redis_store = get_redis_store()
	return redis_store.get("dd.order%d" % user_id)
Exemplo n.º 11
0
def cart_patch(cart_id, food_id, count):
	redis_store = get_redis_store()
	redis_store.sadd("dd.cart%s" % cart_id, food_id)
	k = "dd.cart%s.count%d" % (cart_id, food_id)
	if redis_store.incrby(k, count) < 0:
		redis_store.set(k, 0)
Exemplo n.º 12
0
def cart_len(cart_id):
	redis_store = get_redis_store()
	return redis_store.scard("dd.cart%s" % cart_id)
Exemplo n.º 13
0
def cart_belongs(cart_id, user_id):
	key = "dd.user%d.cart%s" %(user_id, cart_id)
	redis_store = get_redis_store()
	return  redis_store.get(key) == '1'
Exemplo n.º 14
0
def food_field(food_id, field = "stock"):
	redis_store = get_redis_store()
	return int(redis_store.get(food_key(food_id, field)))
Exemplo n.º 15
0
rows = db.select('select min(id) from food')
min_food_id = rows[0][0]
rows = db.select('select max(id) from food')
max_food_id = rows[0][0]
rows = db.select('select min(id) from user')
min_user_id = rows[0][0]
rows = db.select('select max(id) from user')
max_user_id = rows[0][0]

all_foods = db.select('select * from food', is_dict=True)
all_users = db.select('select * from user')

db.close()

myr = get_redis_store()
myr.flushdb()
myr.hset('dd.food', 'min_id', min_food_id)
myr.hset('dd.food', 'max_id', max_food_id)
myr.hset('dd.user', 'min_id', min_user_id)
myr.hset('dd.user', 'max_id', max_user_id)
myr.set('dd.food.json', json.dumps(all_foods))

for i in range(0, len(all_foods)):
	f = all_foods[i]
	myr.set("dd.food%d.stock" % f['id'], f['stock'])
	myr.set("dd.food%d.price" % f['id'], f['price'])

for i in range(0, len(all_users)):
	u = all_users[i]
	myr.set("dd.user%s.password%s" % (u[1], u[2]), u[0])