Elgg\Cache\EntityCache::set PHP Метод

set() публичный Метод

Cache an entity.
public set ( ElggEntity $entity ) : void
$entity ElggEntity Entity to cache
Результат void
    public function set(ElggEntity $entity)
    {
        $guid = $entity->guid;
        if (!$guid || isset($this->entities[$guid]) || isset($this->disabled_guids[$guid])) {
            // have it or not saved
            return;
        }
        // Don't cache non-plugin entities while access control is off, otherwise they could be
        // exposed to users who shouldn't see them when control is re-enabled.
        if (!$entity instanceof \ElggPlugin && $this->session->getIgnoreAccess()) {
            return;
        }
        // Don't store too many or we'll have memory problems
        if (count($this->entities) > self::MAX_SIZE) {
            $this->remove(array_rand($this->entities));
        }
        $this->entities[$guid] = $entity;
        if ($entity instanceof \ElggUser) {
            $this->username_cache[$entity->username] = $entity->guid;
        }
    }

Usage Example

Пример #1
0
 /**
  * Sets the last logon time of the given user to right now.
  *
  * @param ElggUser $user User entity
  * @return void
  */
 public function setLastLogin(ElggUser $user)
 {
     $time = $this->getCurrentTime()->getTimestamp();
     if ($user->last_login == $time) {
         // no change required
         return;
     }
     $query = "\n\t\t\tUPDATE {$this->table}\n\t\t\tSET\n\t\t\t\tprev_last_login = last_login,\n\t\t\t\tlast_login = :last_login\n\t\t\tWHERE guid = :guid\n\t\t";
     $params = [':last_login' => $time, ':guid' => (int) $user->guid];
     $user->prev_last_login = $user->last_login;
     $user->last_login = $time;
     execute_delayed_write_query($query, null, $params);
     $this->entity_cache->set($user);
     // If we save the user to memcache during this request, then we'll end up with the
     // old (incorrect) attributes cached. Hence we want to invalidate as late as possible.
     // the user object gets saved
     register_shutdown_function(function () use($user) {
         $user->storeInPersistedCache(_elgg_get_memcache('new_entity_cache'));
     });
 }
All Usage Examples Of Elgg\Cache\EntityCache::set