[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace OAuth\Common\Storage; 4 5 use OAuth\Common\Token\TokenInterface; 6 use OAuth\Common\Storage\Exception\TokenNotFoundException; 7 use Predis\Client as Predis; 8 9 /* 10 * Stores a token in a Redis server. Requires the Predis library available at https://github.com/nrk/predis 11 */ 12 class Redis implements TokenStorageInterface 13 { 14 /** 15 * @var string 16 */ 17 protected $key; 18 19 /** 20 * @var object|\Redis 21 */ 22 protected $redis; 23 24 /** 25 * @var object|TokenInterface 26 */ 27 protected $cachedTokens; 28 29 /** 30 * @param Predis $redis An instantiated and connected redis client 31 * @param string $key The key to store the token under in redis. 32 */ 33 public function __construct(Predis $redis, $key) 34 { 35 $this->redis = $redis; 36 $this->key = $key; 37 $this->cachedTokens = array(); 38 } 39 40 /** 41 * {@inheritDoc} 42 */ 43 public function retrieveAccessToken($service) 44 { 45 if (!$this->hasAccessToken($service)) { 46 throw new TokenNotFoundException('Token not found in redis'); 47 } 48 49 if (isset($this->cachedTokens[$service])) { 50 return $this->cachedTokens[$service]; 51 } 52 53 $val = $this->redis->hget($this->key, $service); 54 55 return $this->cachedTokens[$service] = unserialize($val); 56 } 57 58 /** 59 * {@inheritDoc} 60 */ 61 public function storeAccessToken($service, TokenInterface $token) 62 { 63 // (over)write the token 64 $this->redis->hset($this->key, $service, serialize($token)); 65 $this->cachedTokens[$service] = $token; 66 67 // allow chaining 68 return $this; 69 } 70 71 /** 72 * {@inheritDoc} 73 */ 74 public function hasAccessToken($service) 75 { 76 if (isset($this->cachedTokens[$service]) 77 && $this->cachedTokens[$service] instanceof TokenInterface 78 ) { 79 return true; 80 } 81 82 return $this->redis->hexists($this->key, $service); 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 public function clearToken($service) 89 { 90 $this->redis->hdel($this->key, $service); 91 unset($this->cachedTokens[$service]); 92 93 // allow chaining 94 return $this; 95 } 96 97 /** 98 * {@inheritDoc} 99 */ 100 public function clearAllTokens() 101 { 102 // memory 103 $this->cachedTokens = array(); 104 105 // redis 106 $keys = $this->redis->hkeys($this->key); 107 $me = $this; // 5.3 compat 108 109 // pipeline for performance 110 $this->redis->pipeline( 111 function ($pipe) use ($keys, $me) { 112 foreach ($keys as $k) { 113 $pipe->hdel($me->getKey(), $k); 114 } 115 } 116 ); 117 118 // allow chaining 119 return $this; 120 } 121 122 /** 123 * @return Predis $redis 124 */ 125 public function getRedis() 126 { 127 return $this->redis; 128 } 129 130 /** 131 * @return string $key 132 */ 133 public function getKey() 134 { 135 return $this->key; 136 } 137 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |