Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Redis.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 3.19 KiB


001  <?php
002  namespace OAuth\Common\Storage;
003   
004  use OAuth\Common\Token\TokenInterface;
005  use OAuth\Common\Storage\Exception\StorageException;
006  use OAuth\Common\Storage\Exception\TokenNotFoundException;
007  use Predis\Client as Predis;
008   
009  /*
010   * Stores a token in a Redis server. Requires the Predis library available at https://github.com/nrk/predis
011   */
012  class Redis implements TokenStorageInterface
013  {
014      /**
015       * @var string
016       */
017      protected $key;
018   
019      /**
020       * @var object|\Redis
021       */
022      protected $redis;
023   
024      /**
025       * @var object|TokenInterface
026       */
027      protected $cachedTokens;
028   
029      /**
030       * @param Predis $redis An instantiated and connected redis client
031       * @param string $key The key to store the token under in redis.
032       */
033      public function __construct(Predis $redis, $key)
034      {
035          $this->redis = $redis;
036          $this->key = $key;
037          $this->cachedTokens = array();
038      }
039   
040      /**
041       * @return \OAuth\Common\Token\TokenInterface
042       * @throws TokenNotFoundException
043       */
044      public function retrieveAccessToken($service)
045      {
046          if (!$this->hasAccessToken($service))
047          {
048              throw new TokenNotFoundException('Token not found in redis');
049          }
050   
051          if (isset($this->cachedTokens[$service]))
052          {
053              return $this->cachedTokens[$service];
054          }
055   
056          $val = $this->redis->hget($this->key, $service);
057   
058          return $this->cachedTokens[$service] = unserialize($val);
059      }
060   
061      /**
062       * @param \OAuth\Common\Token\TokenInterface $token
063       * @throws StorageException
064       */
065      public function storeAccessToken($service, TokenInterface $token)
066      {
067          // (over)write the token
068          $this->redis->hset($this->key, $service, serialize($token));
069          $this->cachedTokens[$service] = $token;
070   
071          // allow chaining
072          return $this;
073      }
074   
075      /**
076      * @return bool
077      */
078      public function hasAccessToken($service)
079      {
080          if (isset($this->cachedTokens[$service]) &&
081              $this->cachedTokens[$service] instanceof TokenInterface)
082          {
083              return true;
084          }
085   
086          return $this->redis->hexists($this->key, $service);
087      }
088   
089      /**
090       * Delete the user's token. Aka, log out.
091       */
092      public function clearToken($service)
093      {
094          $this->redis->hdel($this->key, $service);
095          unset($this->cachedTokens[$service]);
096   
097          // allow chaining
098          return $this;
099      }
100   
101      /**
102       * Delete *ALL* user tokens. Use with care. Most of the time you will likely
103       * want to use clearToken() instead.
104       */
105      public function clearAllTokens()
106      {
107          // memory
108          $this->cachedTokens = array();
109   
110          // redis
111          $keys = $this->redis->hkeys($this->key);
112          $me = $this; // 5.3 compat
113   
114          // pipeline for performance
115          $this->redis->pipeline(function($pipe) use($keys, $me) {
116   
117              foreach ($keys as $k) {
118   
119                  $pipe->hdel($me->getKey(), $k);
120   
121              }
122   
123          });
124   
125          // allow chaining
126          return $this;
127      }
128   
129      /**
130       * @return Predis $redis
131       */
132      public function getRedis()
133      {
134          return $this->redis;
135      }
136   
137      /**
138       * @return string $key
139       */
140      public function getKey()
141      {
142          return $this->key;
143      }
144  }
145