Example #1
0
 /**
  * @param $hostUid
  * @param $key
  * @return null|UserText
  */
 public function getTextsByUser($hostUid, $cached = true)
 {
     if ($cached) {
         $pattern = $this->buildKey($hostUid, '*', '*');
         $prefix = $this->redis->getOptions()->prefix;
         $redisKeys = array_map(function ($redisKey) use($prefix) {
             if (substr($redisKey, 0, strlen($prefix)) == $prefix) {
                 $redisKey = substr($redisKey, strlen($prefix));
             }
             return $redisKey;
         }, $this->redis->keys($pattern));
         if (count($redisKeys) > 0) {
             $result = array_combine($redisKeys, $this->redis->mget($redisKeys));
             foreach ($result as $redisKey => $text) {
                 list(, , $userId, , $key, $lang) = explode(':', $redisKey);
                 $this->texts[$userId][$key][$lang] = $text;
             }
         }
     }
     if (!$cached || !isset($this->texts[$hostUid]) || !$this->texts[$hostUid]) {
         $this->texts[$hostUid] = $this->getTextsByHostFromMysql($hostUid);
         $mset = [];
         foreach ($this->texts[$hostUid] as $key => $langs) {
             foreach ($langs as $lang => $text) {
                 $mset[$this->buildKey($hostUid, $key, $lang)] = $text;
             }
         }
         if ($mset) {
             $this->redis->mset($mset);
         }
     }
     return $this->texts[$hostUid];
 }