Skip to content

Commit

Permalink
Redis Cache for Metrics (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
utxo-one authored Oct 19, 2022
1 parent ceca4b4 commit 8a493f2
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions app/Http/Controllers/Frontend/MetricController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,85 @@
use App\Enums\UserType;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class MetricController extends Controller
{
public function totalBitcoiners()
/**
* Get Total Bitcoiners
*
* @return JsonResponse
*/
public function totalBitcoiners(): JsonResponse
{
// Check if the total bitcoiners metric is cached in Redis
$totalBitcoiners = Redis::get('total_bitcoiners');

if ($totalBitcoiners) {
return response()->json([
'totalBitcoiners' => (int)$totalBitcoiners,
]);
}

$totalBitcoiners = User::where('type', UserType::BITCOINER)->count();

// Cache the total bitcoiners metric in Redis for 1 hour
Redis::setex('total_bitcoiners', 3600, $totalBitcoiners);

return response()->json([
'totalBitcoiners' => $totalBitcoiners,
]);
}

public function totalShitcoiners()
/**
* Get Total Shitcoiners
*
* @return JsonResponse
*/
public function totalShitcoiners(): JsonResponse
{
// Check if the total shitcoiners metric is cached in Redis
$totalShitcoiners = Redis::get('total_shitcoiners');

if ($totalShitcoiners) {
return response()->json([
'totalShitcoiners' => (int)$totalShitcoiners,
]);
}

$totalShitcoiners = User::where('type', UserType::SHITCOINER)->count();

// Cache the total shitcoiners metric in Redis for 1 hour
Redis::setex('total_shitcoiners', 3600, $totalShitcoiners);

return response()->json([
'totalShitcoiners' => $totalShitcoiners,
]);
}

public function totalNocoiners()
/**
* Get Total Nocoiners
*
* @return JsonResponse
*/
public function totalNocoiners(): JsonResponse
{
// Check if the total nocoiners metric is cached in Redis
$totalNocoiners = Redis::get('total_nocoiners');

if ($totalNocoiners) {
return response()->json([
'totalNocoiners' => (int)$totalNocoiners,
]);
}

$totalNocoiners = User::where('type', UserType::NOCOINER)->count();

// Cache the total nocoiners metric in Redis for 1 hour
Redis::setex('total_nocoiners', 3600, $totalNocoiners);

return response()->json([
'totalNocoiners' => $totalNocoiners,
]);
Expand Down

0 comments on commit 8a493f2

Please sign in to comment.