Purges in use sessions. If a session was last active an hour ago, we
assume it is expired and remove it from the pool. If last active 20
minutes ago, we attempt to return the session back to the queue.

@param array $data
483490
private function purgeOrphanedInUseSessions(array &$data)
    {
        foreach ($data['inUse'] as $key => $session) {
            if ($session['lastActive'] + SessionPoolInterface::SESSION_EXPIRATION_SECONDS < $this->time()) {
                unset($data['inUse'][$key]);
            } elseif ($session['lastActive'] + self::DURATION_TWENTY_MINUTES < $this->time()) {
                unset($session['lastActive']);
                array_push($data['queue'], $session);
                unset($data['inUse'][$key]);
            }
        }
    }
507117
def reap_expired_sessions
      @timestamps.each do |session_id,stamp|
        delete_session(session_id) if (stamp + @session_ttl) < Time.now 
      end
      GC.start
    end
1228745
protected function processData($data)
  {
    // set the next_request_timestamp $data->next seconds in the future
    $this->next_request_timestamp=time()+$data->next;
    // check if we have to purge all data
    if($data->reset){
      // purge all data
      $this->debugLog('processData: purging all data');
      // delete all stored prefixes
      // all lists must have no state
      foreach($this->lists as $list => $state){
        $this->lists[$list]='';
        $this->storage->removeHashPrefixesFromList($list);
      }
      $this->storage->updateLists($this->lists);
      // todo: purge cached full hashes
      return;
    }
    foreach($data->listdata as $listdata){
      // for each list
      
      // check if we have to purge all list data
      if($listdata->reset){
        // purge all data
        $this->debugLog('processData: purging data of list '.$listdata->list_name);
        // delete all stored prefixes
        // all list must have no state
        $this->lists[$listdata->list_name]='';
        $this->storage->removeHashPrefixesFromList($listdata->list_name);
      }
      
      $this->lists[$listdata->list_name]=$listdata->newClientState;
      
      // update the lists in the local database (applying the removals before the additions) 
      // https://developers.google.com/safe-browsing/v4/local-databases#database-updates
      
      // fetch removal ThreatEntrySet's
      foreach($listdata->removals as $entry){
        if(isset($entry->rawHashes) && count($entry->rawHashes)>0){ 
          // remove by hash
          $this->storage->removeHashPrefixes($entry->rawHashes,$listdata->list_name);
        }elseif(isset($entry->rawIndices) && count($entry->rawIndices)>0){
          // remove by indice
          $hashes=$this->storage->getHashPrefixesByIndices($entry->rawIndices,$listdata->list_name);
          $this->storage->removeHashPrefixes($hashes,$listdata->list_name);
        }else{
          // nothing??
          $this->debugLog('processData: removal ThreatEntrySet without data!');
        }
      }
      
      // fetch addition ThreatEntrySet's
      foreach($listdata->additions as $entry){
        if(isset($entry->rawHashes) && count($entry->rawHashes)>0){ 
          // remove by hash
          $this->storage->addHashPrefixes($entry->rawHashes,$listdata->list_name);
        }else{
          // nothing??
          $this->debugLog('processData: removal ThreatEntrySet without data!');
        }
      }
      
      if(isset($listdata->checksum)){
        if(isset($listdata->checksum['sha256'])){
          $this->debugLog('processData: checksum for list '.$listdata->list_name.' should be '.$listdata->checksum['sha256']);
          $data_checksum=$this->storage->getListChecksum($listdata->list_name,'sha256');
          if($listdata->checksum['sha256'] != $data_checksum){
            $this->debugLog('processData: checksum for list '.$listdata->list_name.' does not match expected checksum '.$listdata->checksum['sha256'].' != '.$data_checksum);
            // checksum mismatch, so reset the list (next time get the full one)
            $this->lists[$listdata->list_name]='';
            $this->debugLog('processData: Reset list state for '.$listdata->list_name);
          }
        }
      }
      
      // check if we need to bail out
      if($this->pcntl_enabled){
        // process signals with handlers if we received any
        $this->pcntlSignalDispatch();
        if($this->exit_signal_received){
          // signal received, stop processing
          break;
        }
      }
    }// end list loop
    $this->storage->updateLists($this->lists);
  }
46260
public function purge($master)
    {
        $this->recordOrphans($master);

        $expired = $this->processes->orphanedFor(
            $master, $this->supervisors->longestActiveTimeout()
        );

        collect($expired)->each(function ($processId) use ($master) {
            $this->comment("Killing Process: {$processId}");

            exec("kill {$processId}");

            $this->processes->forgetOrphans($master, [$processId]);
        });
    }
436335
private function hydrate($data = array()) {
        foreach ($data as $key => $value) {
            $method = "set" . ucfirst($key);
            if (method_exists($this, $method)) {
                call_user_func(array($this, $method), $value);
            }
        }
    }
564065
public function gc($past)
    {
        // delete the session records that have expired
        $sql = "DELETE FROM {$this->options['table']} WHERE {$this->options['time_col']} < :time";

        try {
            $stmt = $this->db->prepare($sql);

            $stmt->bindValue(':time', $past, \PDO::PARAM_INT);

            $stmt->execute();
        } catch (\PDOException $e) {
            throw new \RuntimeException(
                sprintf(
                    'PDOException was thrown when trying to delete expired sessions: %s',
                    $e->getMessage()
                ),
                0,
                $e
            );
        }

        return true;
    }
1358067
private function removeExpired($key): bool
    {
        if (
            \array_key_exists($key, self::$expired) === false
            ||
            \array_key_exists($key, self::$values) === false
        ) {
            return false;
        }

        list($time, $ttl) = self::$expired[$key];

        if (\time() > ($time + $ttl)) {
            unset(self::$values[$key]);
        }

        if (!isset(self::$values[$key])) {
            unset(self::$expired[$key]);
        }

        return true;
    }
1322715
public function hydrate(array $data)
    {
        foreach ($data as $key => $value) {
            if (!isset($this->directives[$key])) {
                continue;
            }
            $this->set($key, $value);
        }
    }
401849
protected function checkExpire($cache_data)
    {
        $time = time();
        $is_expire = intval($cache_data['expire']) !== 0 && (intval($cache_data['time']) + intval($cache_data['expire']) < $time);
        if ($is_expire) {
            return false;
        }

        return true;
    }
423753
public function purge(array $keys=null)
    {

        if ($keys === null) {

            $someonePurged = false;

            $storages = $this->preloadAllStorages();

            foreach ($storages as $url=>$storage) {

                if ($result = $storage->purge()) {
                    $someonePurged = true;
                }

                $this->removeStorageIfLoaded($url);

            }

            return $someonePurged;
        }

        if (!$keys) {
            return false;
        }

        foreach ($keys as $key) {
            $this->offsetUnset($key);
        }

        return $this->persist();

    }
674521
private static function unserialize($data)
    {
        $hasBuffer = isset($_SESSION);
        $buffer = $_SESSION;
        session_decode($data);
        $session = $_SESSION;

        if ($hasBuffer) {
            $_SESSION = $buffer;
        } else {
            unset($_SESSION);
        }

        return $session;
    }
468741
private function checkIfUserChanged(array $data)
    {
        $model = $this->getModel()->newQuery()->find($this->getVisitorData('id'));

        if (
            ! is_null($model) &&
            ! is_null($model->user_id) &&
            ! is_null($data['user_id']) &&
            $data['user_id'] !== $model->user_id
        ) {
            $newVisitor = $this->regenerateSystemVisitor($data);
            $model      = $this->findByUuid($newVisitor['uuid']);
            $model->update(Arr::except($data, ['id', 'uuid']));
        }
    }
980713
public function deleteTagUsers(array $data)
    {
        $result = $this->httpRaw(self::WECHAT_TAG_USERS_DELETE_PREFIX, $data, [
            'access_token' => $this->getAccessToken(),
        ]);
        return isset($result['errmsg']) && $result['errmsg'] == 'ok';
    }
193589
public function setData(array &$data)
    {
        $this->data = &$data;

        if (!isset($this->data['current'])) {
            $data['current'] = [];
        }

        $this->data['last'] = isset($this->data['current']) ? $this->data['current'] : [];

        $this->data['current'] = [];
    }
239731
private void expireOrphanSessions(long timestamp) {
    // Iterate through registered sessions.
    for (RaftSession session : raft.getSessions().getSessions()) {
      if (session.getService().deleted() && session.isTimedOut(timestamp)) {
        logger.debug("Orphaned session expired in {} milliseconds: {}", timestamp - session.getLastUpdated(), session);
        session = raft.getSessions().removeSession(session.sessionId());
        if (session != null) {
          session.expire();
        }
      }
    }
  }
575274
public static function clean_old_session_data($output = false) {
        global $CFG;
        if ($output) {
            mtrace('Cleaning up stale session data from cache stores.');
        }
        $factory = cache_factory::instance();
        $config = $factory->create_config_instance();
        $definitions = $config->get_definitions();
        $purgetime = time() - $CFG->sessiontimeout;
        foreach ($definitions as $definitionarray) {
            // We are only interested in session caches.
            if (!($definitionarray['mode'] & cache_store::MODE_SESSION)) {
                continue;
            }
            $definition = $factory->create_definition($definitionarray['component'], $definitionarray['area']);
            $stores = $config->get_stores_for_definition($definition);
            // Turn them into store instances.
            $stores = self::initialise_cachestore_instances($stores, $definition);
            // Initialise all of the stores used for that definition.
            foreach ($stores as $store) {
                // If the store doesn't support searching we can skip it.
                if (!($store instanceof cache_is_searchable)) {
                    debugging('Cache stores used for session definitions should ideally be searchable.', DEBUG_DEVELOPER);
                    continue;
                }
                // Get all of the keys.
                $keys = $store->find_by_prefix(cache_session::KEY_PREFIX);
                $todelete = array();
                foreach ($store->get_many($keys) as $key => $value) {
                    if (strpos($key, cache_session::KEY_PREFIX) !== 0 || !is_array($value) || !isset($value['lastaccess'])) {
                        continue;
                    }
                    if ((int)$value['lastaccess'] < $purgetime || true) {
                        $todelete[] = $key;
                    }
                }
                if (count($todelete)) {
                    $outcome = (int)$store->delete_many($todelete);
                    if ($output) {
                        $strdef = s($definition->get_id());
                        $strstore = s($store->my_name());
                        mtrace("- Removed {$outcome} old {$strdef} sessions from the '{$strstore}' cache store.");
                    }
                }
            }
        }
    }
620944
public function resetPassword(array $data)
    {
        $result = array();
        $this->hook->attach('user.reset.password.before', $data, $result, $this);

        if (!empty($result)) {
            return (array) $result;
        }

        if (empty($data['user']['user_id'])) {
            return $this->getResultError();
        }

        if (isset($data['password'])) {
            $result = $this->resetPasswordFinish($data);
        } else {
            $result = $this->resetPasswordStart($data);
        }

        $this->session->regenerate(true);
        $this->hook->attach('user.reset.password.after', $data, $result, $this);
        return (array) $result;
    }