Forráskód Böngészése

userstock分表优化

xjc 4 hónapja
szülő
commit
3350a4e240
3 módosított fájl, 90 hozzáadás és 31 törlés
  1. 22 0
      src/common/common_control.py
  2. 26 6
      src/common/models.py
  3. 42 25
      src/weixin/controls.py

+ 22 - 0
src/common/common_control.py

@@ -47,6 +47,28 @@ def cache_data(timeout=60*60):
         def __wrapper(*args,**kwargs):
             if args:
                 key = "cdata_{}_{}".format(func.__name__,str(args))
+            else:
+                key = "cdata_{}".format(func.__name__)
+            #if not kwargs.get("cache",True) or not cache.get(key):
+            if not cache.get(key):
+                res = func(*args,**kwargs)
+                if res:
+                    cache.set(key,json.dumps(res,cls=CusJSONEncoder),timeout)
+                #print u"不取缓存!!!!!!!!!"
+                return res
+            else:
+                #print u"取缓存"
+                res = cache.get(key)
+                return json.loads(res) if res else {}
+        return __wrapper
+    return _wrapper
+
+
+def cache_data_kw(timeout=60*60):
+    def _wrapper(func):
+        def __wrapper(*args,**kwargs):
+            if args:
+                key = "cdata_{}_{}".format(func.__name__,str(args))
             elif kwargs:
                 kwstr = ""
                 for k in kwargs:

+ 26 - 6
src/common/models.py

@@ -343,13 +343,33 @@ class UserStock(models.Model):
 
     ctime = models.DateTimeField(u"创建时间", auto_now_add=True)
 
-    class Meta:
-        db_table = "user_stock"
-        verbose_name = u"选手持股"
-        app_label = "common"
+    #class Meta:
+    #    db_table = "user_stock"
+    #    verbose_name = u"选手持股"
+    #    app_label = "common"
 
-    def __str__(self):
-        return u"{}){}".format(self.id, self.stock_name)
+    #def __str__(self):
+    #    return u"{}){}".format(self.id, self.stock_name)
+
+    @classmethod
+    def get_db_model(cls,month):
+        table_name = "user_stock_%s" % month
+        if table_name in cls._db_model_dict:
+            return cls._db_model_dict[table_name]
+        class Meta:
+            db_table = table_name
+
+        attrs = {
+            "__module__":cls.__module__,     
+            "Meta":Meta
+        }
+        db_model = type(str(table_name),(cls,),attrs)
+        cls._db_model_dict[table_name] = db_model
+        return db_model
+    _db_model_dict = {}
+    
+    class Meta:
+        abstract = True
 
 
 class WinDefendRank(models.Model):

+ 42 - 25
src/weixin/controls.py

@@ -575,6 +575,11 @@ def get_player_info(player_id):
     return player
 
 @ccc.cache_data()
+def get_player_info_cache(player_id):
+    player = cm.Player.objects.filter(id=player_id).values().first()
+    return player
+
+@ccc.cache_data()
 def get_match_info(match_id):
     match = cm.Match.objects.filter(id=match_id).values().first()
     return match
@@ -788,6 +793,7 @@ def add_model(cls,**kwargs):
             #记录持股情况
             new_stock_list = []
             today_stock_list = json.loads(today_stock)
+            month = "%s%s" % (stock_date.split("-")[0],stock_date.split("-")[1])
             for ts in today_stock_list:
                 if ts["name"] and ts["fund"]:
                     try:
@@ -795,11 +801,8 @@ def add_model(cls,**kwargs):
                             name = ts["name"] 
                         )
                         stock_id = stock.id
-                        try:
-                            cm.UserStock.objects.filter(stock_id=stock_id,player_id=player.id,stock_date=stock_date).delete()
-                        except Exception as e:
-                            print(e)
-                        usobj,flag = cm.UserStock.objects.get_or_create(
+
+                        usobj,flag = cm.UserStock.get_db_model(month).objects.get_or_create(
                             stock_id = stock_id,
                             player_id = player.id,
                             stock_date = stock_date
@@ -1414,16 +1417,22 @@ def get_stock_info(stock_id):
     return stock
     
 
+@ccc.cache_data()
+def get_stock_cache(stock_id):
+    stock = cm.Stock.objects.filter(id=stock_id).values().first()
+    return stock
+
 
 
 def get_hot_stock_buy(**kwargs):
     """
     """
     stock_date = kwargs.get("stock_date")
+    month = "%s%s" % (stock_date.split("-")[0],stock_date.split("-")[1])
 
-    qset = cm.UserStock.objects.filter(stock_date=stock_date,fund__gt=0)
+    qset = cm.UserStock.get_db_model(month).objects.filter(stock_date=stock_date,fund__gt=0)
     if kwargs.get("name"):
-        qset = cm.UserStock.objects.filter(stock_name__icontains=kwargs.get("name"),stock_date=stock_date)
+        qset = qset.filter(stock_name__icontains=kwargs.get("name"),stock_date=stock_date)
     
     if int(kwargs.get("fund_rank",0)) == 1:
         qset = qset.values("stock_id","stock_name").annotate(count=Count("stock_id"),total_fund=Sum("fund")).order_by("-total_fund")
@@ -1451,10 +1460,11 @@ def get_hot_stock_buy(**kwargs):
     if page and page_size:
         total,data = ccf.get_page_list(data,page,page_size)
         for item in data:
-            stock = cm.Stock.objects.filter(id=item["id"]).first()
+            #stock = cm.Stock.objects.filter(id=item["id"]).first()
+            stock = get_stock_cache(item["id"])
             if stock:
-                item["stock_code"] = stock.code
-                item["stock_desc"] = stock.desc
+                item["stock_code"] = stock.get("code","")
+                item["stock_desc"] = stock.get("desc","")
         return total,data
     else:
         return len(data),data
@@ -1578,20 +1588,22 @@ def get_hot_stock_sell_players(**kwargs):
     """
     _id = kwargs.get("id")
     stock_date = kwargs.get("stock_date")
+    month = "%s%s" % (stock_date.split("-")[0],stock_date.split("-")[1])
     stock_date_time = ccf.str_to_datetime(kwargs.get("stock_date"),"%Y-%m-%d")
     yesterday = (stock_date_time - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
 
     #昨天持股选手
-    yes_players = list(cm.UserStock.objects.filter(stock_date=yesterday,stock_id=_id).values_list("player_id",flat=True))
-    td_players = list(cm.UserStock.objects.filter(stock_date=stock_date,stock_id=_id).values_list("player_id",flat=True))
+    yes_players = list(cm.UserStock.get_db_model(month).objects.filter(stock_date=yesterday,stock_id=_id).values_list("player_id",flat=True))
+    td_players = list(cm.UserStock.get_db_model(month).objects.filter(stock_date=stock_date,stock_id=_id).values_list("player_id",flat=True))
     sell_players = list(set(yes_players)-set(td_players))
 
     data = []
     for player_id in sell_players:
-        player = cm.Player.objects.filter(id=player_id).first()
+        #player = cm.Player.objects.filter(id=player_id).first()
+        player = get_player_info_cache(player_id)
         if player:
-            match_id = player.match_id
-            match_group = player.match_group
+            match_id = player["match_id"]
+            match_group = player["match_group"]
             today_record = get_today_record(player_id,match_id,match_group,stock_date)
 
             if today_record:
@@ -1684,7 +1696,7 @@ def get_defend_rank(request):
     return total,data
 
 
-@ccc.cache_data(10*60)
+@ccc.cache_data_kw(10*60)
 def get_champion_articles_list(**kwargs):
     """
     """
@@ -1913,15 +1925,19 @@ def get_stock_players(**kwargs):
     """
     stock_id = kwargs.get("stock_id")
     stock_date = kwargs.get("stock_date",get_today_date())
+    month = "%s%s" % (stock_date.split("-")[0],stock_date.split("-")[1])
+    print(month)
+
     data = []
-    user_stocks = cm.UserStock.objects.filter(stock_id=stock_id,stock_date=stock_date,fund__gt=0.0)
+    user_stocks = cm.UserStock.get_db_model(month).objects.filter(stock_id=stock_id,stock_date=stock_date,fund__gt=0.0)
     for us in user_stocks:
         player_id = us.player_id
-        player = cm.Player.objects.filter(id=player_id).first()
+        #player = cm.Player.objects.filter(id=player_id).first()
+        player = get_player_info_cache(player_id)
         if player:
-            match_id = player.match_id
-            match_group = player.match_group
-            user_id = player.user_id
+            match_id = player["match_id"]
+            match_group = player["match_group"]
+            user_id = player["user_id"]
             if stock_date:
                 today_record = get_today_record(user_id,match_id,match_group,stock_date)
             else:
@@ -2585,7 +2601,7 @@ def add_zan(request):
     zans = cm.PlayerRecord.get_db_model(match_id).objects.filter(id=record_id).first().zans
     return zans
 
-@ccc.cache_data(10*60)
+@ccc.cache_data_kw(10*60)
 def get_experience_list(**kwargs):
     """
     """
@@ -2756,6 +2772,7 @@ def get_user_match_list(request):
     user_id = kwargs.get("user_id")
     if not user_id:
         user_id = request.user.get("id")
+
     qset = cm.Player.objects.filter(user_id=user_id).filter(match_group__isnull=False).order_by("-id")
     qset = qset.values("user_id","match_id","match_group").annotate(cnt=Count("match_id"))
 
@@ -3235,7 +3252,7 @@ def articles_top5_by_type(**kwargs):
         data.append({"category":cat,"articles":articles})
     return data
 
-@ccc.cache_data(5*60)
+@ccc.cache_data_kw(5*60)
 def get_match_winlost_top5(**kwargs):
     """
     """
@@ -3481,7 +3498,7 @@ def get_opmode_group_rank_info_list(**kwargs):
 
 
 
-@ccc.cache_data(5*60)
+@ccc.cache_data_kw(5*60)
 def get_match_group_winlose_statistic(**qdata):
     """
     """
@@ -3631,7 +3648,7 @@ def get_match_group_winlose_allday(**qdata):
     return rst
 
 
-@ccc.cache_data(5*60)
+@ccc.cache_data_kw(5*60)
 def get_match_group_winlose_avg_allday(**qdata):
     """
     """