Skip to content

Instantly share code, notes, and snippets.

@ahmeti
Last active April 18, 2021 19:00
Show Gist options
  • Save ahmeti/767b3f1dfe2bc670f79bdf1a9d581388 to your computer and use it in GitHub Desktop.
Save ahmeti/767b3f1dfe2bc670f79bdf1a9d581388 to your computer and use it in GitHub Desktop.
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