function test_incr_decr() { $c = new Cache(); $this->assertTrue($c->set('foo', 0)); $this->assertEquals($c->increment('foo'), 1); $this->assertEquals($c->increment('foo'), 2); $this->assertEquals($c->increment('foo', 2), 4); $this->assertEquals($c->decrement('foo'), 3); $this->assertEquals($c->get('foo'), 3); }
public function increateCount($key, $inc = 1) { $result = $this->cache->increment($key, $inc); if ($result === false) { if ($this->cache->set($key, $inc, 0) === false) { return false; } else { return $inc; } } return $result; }
/** * pushes identify key to cache * * @param $ident * @param int $minutes */ public function put($ident, $minutes = 10) { if (!$ident || !is_numeric($minutes)) { return; } $key = 'af:' . $ident; if (\Cache::has($key) && is_numeric(\Cache::get($key))) { \Cache::increment($key); } else { \Cache::put($key, 1, $minutes); } }
private function updateCache($inc = 1) { $key = $this->getMemKey(); $key_next = $key . '_next_value'; $this->initSeq($this->id_field); $seq = $this->cache->increment($key, $inc); $next_value = $this->cache->get($key_next); if ($seq >= $next_value) { $this->commitData($seq); $this->cache->increment($key_next, $this->step); } return $seq; }
public function counterIncrement($id) { if (Configure::read('debug') === 0 && $_SERVER['HTTP_HOST'] !== 'localhost') { $cacheKey = "{$this->name}/Counter/{$id}"; $cachedCounter = Cache::read($cacheKey); if (false === Cache::increment($cacheKey) || false === $cachedCounter) { Cache::write($cacheKey, 1); } if ((int) $cachedCounter >= 10) { Cache::write($cacheKey, 1); $this->updateAll(array('count_daily' => "count_daily + {$cachedCounter}", 'count_all' => "count_all + {$cachedCounter}"), array("{$this->name}.id" => $id)); } } }
public function store(Request $request) { $title = $request->input('title'); $content = $request->input('content'); $post = ['title' => trim($title), 'content' => trim($content)]; $posts = Cache::get('posts', []); if (!Cache::get('post_id')) { Cache::add('post_id', 1, 60); } else { Cache::increment('post_id', 1); } $posts[Cache::increment('post_id', 1)]; Cache::put('posts', $posts, 60); return redirect()->route('post.show', ['post' => Cache::get('post_id')]); }
public static function start() { $key = static::getKey(); $start = microtime(true); Cache::add($key, 0, static::$intervalSeconds); register_shutdown_function('Firewall::end'); while (Cache::increment($key) > static::$concurrency) { Cache::decrement($key); if (!static::$spinLockSeconds || microtime(true) - $start > static::$intervalSeconds) { http_response_code(429); die('429: Too Many Requests'); } usleep(static::$spinLockSeconds * 1000000); } }
public function postLogin(Request $request) { $valid = Validator::make($request->all(), ['name' => 'required', 'password' => 'required']); if ($valid->fails()) { return redirect()->back()->withErrors($valid)->withInput(); } if (\Cache::has('loginAttempt')) { $attempt_count = \Cache::get('loginAttempt'); if ($attempt_count > 3) { return redirect()->back()->with('errorMess', 'Bạn đã đăng nhập sai quá nhiều lần, Vui lòng thử lại sau 1 giờ nữa'); } \Cache::increment('loginAttempt'); } else { \Cache::put('loginAttempt', 1, 60); } if (auth()->attempt(['name' => $request->input('name'), 'password' => $request->input('password'), 'active' => 1])) { \Cache::forget('loginAttempt'); return redirect()->route('admin.index'); } return redirect()->back()->withInput()->with('errorMess', trans('Tên đăng nhập hoặc mật khẩu không đúng')); }
/** * testIncrement method * * @return void */ public function testIncrement() { $result = Cache::write('test_increment', 5, 'memcache'); $this->assertTrue($result); $result = Cache::increment('test_increment', 1, 'memcache'); $this->assertEqual(6, $result); $result = Cache::read('test_increment', 'memcache'); $this->assertEqual(6, $result); $result = Cache::increment('test_increment', 2, 'memcache'); $this->assertEqual(8, $result); $result = Cache::read('test_increment', 'memcache'); $this->assertEqual(8, $result); }
/** * testIncrement method * * @access public * @return void */ function testIncrement() { if ($this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment() %s')) { return; } $result = Cache::write('test_increment', 5); $this->assertTrue($result); $result = Cache::increment('test_increment'); $this->assertEqual(6, $result); $result = Cache::read('test_increment'); $this->assertEqual(6, $result); $result = Cache::increment('test_increment', 2); $this->assertEqual(8, $result); $result = Cache::read('test_increment'); $this->assertEqual(8, $result); }
/** * Throttle on the basis of source type * * @param array $definition * * @return Response */ private function applyThrottle($definition) { if ($definition['source_type'] == 'ElasticsearchDefinition') { $requestsPerHour = 720; // Rate limit by IP address $key = sprintf('api:%s', \Request::getClientIp()); // Add if doesn't exist // Remember for 1 hour \Cache::add($key, 0, 60); // Add to count $count = \Cache::get($key); if ($count > $requestsPerHour) { $response = \Response::make('', 429); $response->setContent('Rate limit exceeded, maximum of ' . $requestsPerHour . ' requests per hour has been reached.'); return $response; } else { \Cache::increment($key); } } }
/** * testIncrement method * * @return void */ public function testIncrement() { $this->skipIf(!function_exists('wincache_ucache_inc'), 'No wincache_inc() function, cannot test increment().'); $result = Cache::write('test_increment', 5, 'wincache'); $this->assertTrue($result); $result = Cache::increment('test_increment', 1, 'wincache'); $this->assertEquals(6, $result); $result = Cache::read('test_increment', 'wincache'); $this->assertEquals(6, $result); $result = Cache::increment('test_increment', 2, 'wincache'); $this->assertEquals(8, $result); $result = Cache::read('test_increment', 'wincache'); $this->assertEquals(8, $result); }
public static function banCall($api, $scope, $owner = 0, $accessMask = 0, $reason = null) { \Log::warning('Processing a ban request for api: ' . $api . ' scope: ' . $scope . ' owner: ' . $owner, array('src' => __CLASS__)); // Check if we should retreive the current access mask if ($accessMask == 0) { $accessMask = \EveAccountAPIKeyInfo::where('keyID', '=', $owner)->pluck('accessMask'); } // Generate a hash with which to ID this call $hash = BaseApi::makeCallHash($api, $scope, $owner . $accessMask); // Check the cache if a ban has been recorded if (!\Cache::has('call_ban_grace_count_' . $hash)) { // Record the new ban, getting the grance period from the seat config and return \Cache::put('call_ban_grace_count_' . $hash, 0, \Config::get('seat.ban_grace')); return; } else { // Check if we have reached the limit for the allowed bad calls from the config if (\Cache::get('call_ban_grace_count_' . $hash) < \Config::get('seat.ban_limit') - 1) { // Add another one to the amount of failed calls and return \Cache::increment('call_ban_grace_count_' . $hash); return; } } \Log::warning('Ban limit reached. Actioning ban for api: ' . $api . ' scope: ' . $scope . ' owner: ' . $owner, array('src' => __CLASS__)); // We _should_ only get here once the ban limit has been reached $banned = \EveBannedCall::where('hash', '=', $hash)->first(); if (!$banned) { $banned = new \EveBannedCall(); } $banned->ownerID = $owner; $banned->api = $api; $banned->scope = $scope; $banned->accessMask = $accessMask; $banned->hash = $hash; $banned->reason = $reason; $banned->save(); // We also need to keep in mind how many errors have occured so far. // This is mainly for the checks that are done in bootstrap() // allowing us to pause and not cause a IP to be banned. if (\Cache::has('eve_api_error_count')) { \Cache::increment('eve_api_error_count'); } else { \Cache::put('eve_api_error_count', 1, 30); } }
/** * @return mixed */ public function readArticleIndex() { if (!\Cache::has($this->getCacheArticleBeanFeedKey())) { \Cache::put($this->getCacheArticleBeanFeedKey(), 1, 1440); } else { if (\Cache::get($this->getCacheArticleBeanFeedKey()) < 5) { \Cache::increment($this->getCacheArticleBeanFeedKey()); } } return \Cache::get($this->getCacheArticleBeanFeedKey()); }
/** * testIncrement method * * @return void */ public function testIncrement() { $result = Cache::write('test_increment', 5); $this->assertTrue($result); $result = Cache::increment('test_increment'); $this->assertEquals(6, $result); $result = Cache::read('test_increment'); $this->assertEquals(6, $result); $result = Cache::increment('test_increment', 2); $this->assertEquals(8, $result); $result = Cache::read('test_increment'); $this->assertEquals(8, $result); }
} }); /* |-------------------------------------------------------------------------- | API Filters |-------------------------------------------------------------------------- | | Authentication and rate limiting. | */ Route::filter('api.auth', function () { if (!Request::getUser()) { App::abort(401, 'A valid API key is required'); } $user = User::where('api_key', '=', Request::getUser())->first(); if (!$user) { App::abort(401); } Auth::login($user); }); Route::filter('api.limit', function () { $key = sprintf('api:%s', Auth::user()->api_key); // Create the key if it doesn't exist Cache::add($key, 0, 60); // Increment by 1 $count = Cache::increment($key); // Fail if hourly requests exceeded if ($count > Config::get('api.requests_per_hour')) { App::abort(403, 'Hourly request limit exceeded'); } });
/** * Test reading from a config that is undefined. * * @return void */ public function testReadNonExistingConfig() { $this->assertFalse(Cache::read('key', 'totally fake')); $this->assertFalse(Cache::write('key', 'value', 'totally fake')); $this->assertFalse(Cache::increment('key', 1, 'totally fake')); $this->assertFalse(Cache::decrement('key', 1, 'totally fake')); }
/** * Throttle on the basis of source type * * @param array $definition * * @return Response */ private function applyThrottle($definition) { if ($definition['source_type'] == 'ElasticsearchDefinition') { // Per 5 minutes $rate_limit = 60; // Rate limit by IP address $key = sprintf('api:%s', \Request::getClientIp()); $key = sha1($key); // Add the ip to the cache if it doesn't exist // and make it reset after an hour $response = \Cache::add($key, 0, 5); // Add to count $count = \Cache::get($key); if ($count > $rate_limit) { $response = \Response::make('', 429); $response->setContent('Rate limit exceeded, maximum of ' . $rate_limit . ' requests per hour has been reached.'); return $response; } else { $response = \Cache::increment($key); } } }
/** * Tests that configuring groups for stored keys return the correct values when read/written * Shows that altering the group value is equivalent to deleting all keys under the same * group * * @return void */ public function testGroupReadWrite() { Cache::config('redis_groups', array('engine' => 'Redis', 'duration' => 3600, 'groups' => array('group_a', 'group_b'), 'prefix' => 'test_')); Cache::config('redis_helper', array('engine' => 'Redis', 'duration' => 3600, 'prefix' => 'test_')); $this->assertTrue(Cache::write('test_groups', 'value', 'redis_groups')); $this->assertEquals('value', Cache::read('test_groups', 'redis_groups')); Cache::increment('group_a', 1, 'redis_helper'); $this->assertFalse(Cache::read('test_groups', 'redis_groups')); $this->assertTrue(Cache::write('test_groups', 'value2', 'redis_groups')); $this->assertEquals('value2', Cache::read('test_groups', 'redis_groups')); Cache::increment('group_b', 1, 'redis_helper'); $this->assertFalse(Cache::read('test_groups', 'redis_groups')); $this->assertTrue(Cache::write('test_groups', 'value3', 'redis_groups')); $this->assertEquals('value3', Cache::read('test_groups', 'redis_groups')); }
/** * testIncrement method * * @return void */ public function testIncrement() { $hasSupport = function_exists('apc_inc') || function_exists('apcu_inc'); $this->skipIf(!function_exists('apc_inc'), 'No apc_inc()/apcu_inc() function, cannot test increment().'); $result = Cache::write('test_increment', 5, 'apc'); $this->assertTrue($result); $result = Cache::increment('test_increment', 1, 'apc'); $this->assertEquals(6, $result); $result = Cache::read('test_increment', 'apc'); $this->assertEquals(6, $result); $result = Cache::increment('test_increment', 2, 'apc'); $this->assertEquals(8, $result); $result = Cache::read('test_increment', 'apc'); $this->assertEquals(8, $result); }