Prechádzať zdrojové kódy

用户推荐和安置网络都有自己的缓存key

root 3 rokov pred
rodič
commit
ada2fc7994

+ 8 - 5
common/helpers/Cache.php

@@ -48,6 +48,8 @@ class Cache
     const USER_INFO_KEY = 'user:baseInfo';
     const USER_NETWORK_PARENTS = 'user:networkParents';
     const USER_RELATION_PARENTS = 'user:relationParents';
+    const PREPARE_USER_NETWORK_PARENTS = 'prepare:user:networkParents';
+    const PREPARE_USER_RELATION_PARENTS = 'prepare:user:relationParents';
     const PRE_USER_CREATED_AT_LIST = 'prepare:user:createdAtList_';
     const USER_CREATED_AT_LIST = 'user:createdAtList_';
 
@@ -317,19 +319,20 @@ class Cache
     /**
      * 获取全部安置网络上级
      * @param $userId
+     * @param $isPrepare 是否是预计计算 true为是 则使用预计计算的缓存key
      * @return array|mixed
      */
-    public static function getAllNetworkParents($userId){
-        return UserNetwork::getAllParentsFromRedis($userId);
+    public static function getAllNetworkParents($userId, $isPrepare=false){
+        return UserNetwork::getAllParentsFromRedis($userId, $isPrepare);
     }
 
     /**
      * 获取全部推荐网络的上级
      * @param $userId
+     * @param $isPrepare 是否是预计计算 true为是 则使用预计计算的缓存key
      * @return array|mixed
      */
-    public static function getAllRelationParents($userId){
-        return UserRelation::getAllParentsFromRedis($userId);
+    public static function getAllRelationParents($userId, $isPrepare = false){
+        return UserRelation::getAllParentsFromRedis($userId, $isPrepare);
     }
-
 }

+ 2 - 2
common/helpers/bonus/PrepareCalcCache.php

@@ -136,8 +136,8 @@ class PrepareCalcCache {
      * 用户的网络是一套,应该用一个缓存,获取最新推荐关系
      */
     public static function clearNetCache(){
-        \Yii::$app->redis->del(Cache::USER_NETWORK_PARENTS);
-        \Yii::$app->redis->del(Cache::USER_RELATION_PARENTS);
+        \Yii::$app->redis->del(Cache::PREPARE_USER_NETWORK_PARENTS);
+        \Yii::$app->redis->del(Cache::PREPARE_USER_RELATION_PARENTS);
     }
 
     /**

+ 2 - 2
common/helpers/bonus/PreparePerfCalc.php

@@ -1221,7 +1221,7 @@ class PreparePerfCalc {
      * @return bool
      */
     public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
-        $allParents = Cache::getAllNetworkParents($userId);
+        $allParents = Cache::getAllNetworkParents($userId, true);
         $allData = array_slice($allParents, $offset, $this->_limit);
         unset($allParents);
         if ($allData) {
@@ -1248,7 +1248,7 @@ class PreparePerfCalc {
      * @return bool
      */
     public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
-        $allParents = Cache::getAllRelationParents($userId);
+        $allParents = Cache::getAllRelationParents($userId,true);
         $allData = array_slice($allParents, $offset, $this->_limit);
         unset($allParents);
         if ($allData) {

+ 3 - 2
common/models/UserNetwork.php

@@ -454,10 +454,11 @@ class UserNetwork extends \common\components\ActiveRecord
     /**
      * 从缓存中获取会员的全部父级(主要用于结算时的处理,能够提高效率不去查库)
      * @param $userId
+     * @param $isPrepare 是否是预计计算 true为是 则使用预计计算的缓存key
      * @return array|mixed
      */
-    public static function getAllParentsFromRedis($userId){
-        $key = Cache::USER_NETWORK_PARENTS;
+    public static function getAllParentsFromRedis($userId,$isPrepare=false){
+        $key = $isPrepare === true ? Cache::PREPARE_USER_NETWORK_PARENTS : Cache::USER_NETWORK_PARENTS;
         $data = Yii::$app->redis->hget($key, $userId);
         if(!$data){
             $data = [];

+ 3 - 2
common/models/UserRelation.php

@@ -346,10 +346,11 @@ class UserRelation extends \common\components\ActiveRecord
     /**
      * 从缓存中获取会员的全部父级(主要用于结算时的处理,能够提高效率不去查库)
      * @param $userId
+     * @param $isPrepare 是否是预计计算 true为是 则使用预计计算的缓存key
      * @return array|mixed
      */
-    public static function getAllParentsFromRedis($userId){
-        $key = Cache::USER_RELATION_PARENTS;
+    public static function getAllParentsFromRedis($userId, $isPrepare = false){
+        $key = $isPrepare === true ? Cache::PREPARE_USER_RELATION_PARENTS : Cache::USER_RELATION_PARENTS;
         $data = Yii::$app->redis->hget($key, $userId);
         if(!$data){
             $data = [];