/** * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { $cacheKey = "user:{$identifier}"; if ($this->cache->has($cacheKey)) { return $this->cache->get($cacheKey); } $result = $this->createModel()->newQuery()->find($identifier); if (is_null($result)) { return null; } $this->cache->add($cacheKey, $result, 120); return $result; }
/** * Source: http://fideloper.com/laravel-http-middleware. */ public function handle($request, Closure $next) { $limit = $this->config->get('api.ratelimit.requests'); $time = $this->config->get('api.ratelimit.time'); // Rate limit by IP address $key = sprintf('api:%s', $request->get('key')); // Add if doesn't exist $this->cache->add($key, 0, $time); // Add to count $count = $this->cache->increment($key); if ($count > $limit) { throw new TooManyRequestsHttpException($time * 60, 'Rate limit exceed.'); } return $next($request); }
/** * Increment the hit counter for the cache key. * * @param string $key * * @return bool */ public function addHit($key) { if (!$this->cache->has($this->getKey($key))) { return $this->cache->add($this->getKey($key), 1, 1); } return $this->cache->increment($this->getKey($key), 1); }
/** * Authコンポーネントのuser()メソッドなどを利用した場合に実行されるメソッドです * デフォルトの場合、user()メソッドコール時に都度SQLが発行されますので、cacheを利用します。 * ユーザー情報更新時などにcacheを再生成するように実装します。 * * @param mixed $identifier * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { /** * user:$identifier(user_id) としてキャッシュを検索し、 * 見つからない場合は作成してデータベースから取得したデータを保持します * 以降はデータベースへアクセスしません */ $cacheKey = "user:{$identifier}"; if ($this->cache->has($cacheKey)) { return $this->cache->get($cacheKey); } $result = $this->createModel()->newQuery()->find($identifier); if (is_null($result)) { return null; } $this->cache->add($cacheKey, $result, 120); return $result; }
/** * Increment the counter for a given key for a given decay time. * * @param string $key * @param int $decayMinutes * @return int */ public function hit($key, $decayMinutes = 1) { $this->cache->add($key, 1, $decayMinutes); return (int) $this->cache->increment($key); }
/** * Store an item in the cache if the key does not exist. * * @param string $key * @param mixed $value * @param \DateTime|int $minutes * @return bool */ public function add($key, $value, $minutes) { return $this->cache->add($key, $value, $minutes); }
/** * Lock a user out. * * @author Andrea Marco Sartori * @return void */ public function lockOut() { $this->resetAttempts(); $this->cache->add($this->lockOutKey, $this->getDelay() + time(), $this->getExpiry()); }
/** * @param $job * @param $data * @return mixed */ public function fire($job, $data) { $this->queueToEventDispatcher->fire($job, $data); $this->cache->add($data['command_id'], new Carbon(), 1); }