Skip to content

Instantly share code, notes, and snippets.

@ahmeti

ahmeti/01-redis.sh

Last active Apr 18, 2021
Embed
What would you like to do?
Redis İşlemleri
redis-cli> INFO keyspace
# Keyspace
db0:keys=7,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
db2:keys=1,expires=0,avg_ttl=0
db11:keys=1,expires=0,avg_ttl=0
redis-cli> SET ornek_anahtar "örnek_değer"
// Tüm anahtar listesini verir.
redis-cli> KEYS *
// ornek ile başlayan anahtar listesini verir.
redis-cli> KEYS ornek*
redis-cli> DEL ornek_anahtar
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Throwable;
class StoreRedis
{
public function handle($request, Closure $next)
{
try {
$data = [
'ipv4' => $request->getClientIp(),
'user_agent' => $request->userAgent(),
'method' => $request->getMethod(),
'path' => $request->path(),
'params' => $request->getQueryString(),
'viewed_at' => date('Y-m-d H:i:s')
];
$key = uniqid();
Redis::set('page_view:'.$key, serialize($data));
}catch (Throwable $e){
Log::error('Page Views hatası. '.$e->getMessage());
}
return $next($request);
}
}
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
class Kernel extends ConsoleKernel
{
protected $commands = [
//
];
protected function schedule(Schedule $schedule)
{
// Her 5 dakikada bir çalışacak
$schedule->call(function(){
try {
$keys = Redis::keys('page_view:*');
foreach ($keys as $key){
$str = Str::replaceFirst(config('database.redis.options.prefix'), '', $key);
$item = unserialize(Redis::get($str));
DB::table('page_views')
->insert([
'ipv4' => $item['ipv4'],
'user_agent' => $item['user_agent'],
'method' => $item['method'],
'path' => $item['path'],
'params' => $item['params'],
'viewed_at' => $item['viewed_at'],
]);
Redis::del($str);
}
}catch (\Throwable $e){
Log::error('Page Views hatası. '.$e->getMessage());
}
})->everyFiveMinutes();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment