protected function incrementLoginAttempts(Request $request) { $key = $this->getLoginAttemptsKey($request); if (!Cache::has($key)) { Cache::add($key, 1, static::LOGIN_LOCKOUT_MINUTES); return 1; } return (int) Cache::increment($key); }
/** * * 生成流水号算法:第1位为支付渠道,2-7位为日期,8-17位是当日流水经过Skip32加密过的流水号,可解密出真实流水。最后3位为随机数。 * @param $prefix 1位数字,标记支付类型.1:微信支付,2:支付宝支付,3:银联支付 * @return string */ public static function getPaymentSerialNumber($prefix) { $timestamp = time(); $datePrefix = date('ymd', $timestamp); $key = self::CACHE_KEY_PAYMENT_COUNT . $datePrefix; if (!Cache::has($key)) { $counter = Payment::getTodayCount(); $expiresAt = Carbon::now()->addMinutes(1440); Cache::put($key, $counter, $expiresAt); } $value = Cache::increment($key); $value = str_pad(Skip32::encrypt(self::ENCRYPTED_KEY, $value), 10, '0', STR_PAD_LEFT); return $prefix . $datePrefix . $value . str_pad(rand(0, 999), 3, '0', STR_PAD_LEFT); }
/** * Increment the login attempts for the user. * * @param \Illuminate\Http\Request $request * @return int */ protected function incrementLoginAttempts(Request $request) { Cache::add($key = $this->getLoginAttemptsKey($request), 1, 1); return (int) Cache::increment($key); }
/** * increase view count */ private function incView() { if (!$this->cacheViewName) { $viewsCount = $this->views_count(); } $viewsCount = Cache::increment($this->cacheViewName, Config::get('counter.viewIncrementAmount', 1)); $this->setViewed(); $this->regularCheck($viewsCount, 'view'); return $viewsCount; }
/** * Execute the console command. * * @return mixed */ public function handle() { if (Cache::get('isWorkerStarted', false)) { return; } if (!Cache::get('isProcessing', false)) { return; } Cache::forever('isWorkerStarted', true); set_time_limit(0); $skipTables = explode(',', env('SKIP_TABLES', '')); if (Cache::has('tables')) { $tables = Cache::get('tables'); } else { $tables = ['pgsql' => [], 'mongodb' => []]; $pgsqlTables = DB::connection('pgsql')->getDoctrineSchemaManager()->listTableNames(); foreach ($pgsqlTables as $table) { $tables['pgsql'][] = str_replace('"', '', $table); } $tables['mongodb'] = DB::connection('mongodb')->getMongoDB()->getCollectionNames(); Cache::put('tables', $tables, 15); } if (Cache::has('rows')) { $rows = Cache::get('rows'); } else { $rows = ['pgsql' => [], 'mongodb' => []]; foreach ($tables['pgsql'] as $table) { if (in_array($table, $skipTables)) { $rows['pgsql'][$table] = 0; continue; } $rows['pgsql'][$table] = DB::connection('pgsql')->table($table)->count(); } foreach ($tables['mongodb'] as $table) { $rows['mongodb'][$table] = DB::connection('mongodb')->table($table)->raw(function ($collection) { return $collection->count(); }); } Cache::put('rows', $rows, 15); } $runStartedAt = time(); $wasWorking = false; while ($runStartedAt + 60 * 10 >= time()) { foreach ($tables['pgsql'] as $table) { if (!Schema::connection('mongodb')->hasCollection($table)) { Schema::connection('mongodb')->create($table); $tables['mongodb'][] = $table; $rows['mongodb'][$table] = 0; Cache::put('tables', $tables, 15); Cache::put('rows', $rows, 15); } if (in_array($table, $skipTables)) { continue; } if ($rows['pgsql'][$table] > $rows['mongodb'][$table]) { $batchSize = 10000; DB::connection('mongodb')->disableQueryLog(); DB::connection('pgsql')->disableQueryLog(); $data = DB::connection('pgsql')->table($table)->skip($rows['mongodb'][$table])->take($batchSize)->get(); $insert = json_decode(json_encode($data), true); // Cast date fields to MongoDate objects foreach ($insert as $key => $item) { foreach ($item as $rowName => $value) { if ($rowName == 'created_at' || $rowName == 'updated_at') { $insert[$key][$rowName] = new MongoDate(strtotime($value)); } } } DB::connection('mongodb')->table($table)->insert($insert); $rows['mongodb'][$table] += $batchSize; if ($rows['mongodb'][$table] > $rows['pgsql'][$table]) { $rows['mongodb'][$table] = $rows['pgsql'][$table]; } Cache::put('rows', $rows, 15); Cache::increment('rowsProcessedThisRun', $batchSize); $wasWorking = true; break; } } } if ($wasWorking == false) { Cache::forever('timeFinished', Carbon::now()); Cache::forever('isProcessing', false); } Cache::forever('isWorkerStarted', false); }