I have a function like this using Laravel lumen framework:
// Define sample payment transactions
$transactions = [
['transaction_id' => 1, 'amount' => 100, 'status' => 'success', 'payment_method' => 'credit_card', 'timestamp' => time()],
['transaction_id' => 2, 'amount' => 150, 'status' => 'success', 'payment_method' => 'paypal', 'timestamp' => time()],
['transaction_id' => 3, 'amount' => 200, 'status' => 'failed', 'payment_method' => 'credit_card', 'timestamp' => time()],
['transaction_id' => 4, 'amount' => 200, 'status' => 'failed', 'payment_method' => 'credit_card', 'timestamp' => time()],
['transaction_id' => 5, 'amount' => 200, 'status' => 'failed', 'payment_method' => 'credit_card', 'timestamp' => time()],
];
// Store sample transactions in Redis
foreach ($transactions as $transaction) {
try {
Redis::hmset("transaction:{$transaction['transaction_id']}", $transaction);
} catch (Exception $e) {
// Log any errors or exceptions
IlluminateSupportFacadesLog::error("Error storing transaction in Redis: " . $e->getMessage());
}
}
// Generate report by payment method
// Define payment methods
$paymentMethods = ['credit_card', 'paypal', 'bank_transfer']; // Add more payment methods as needed
// Initialize report array
$report = [];
// Calculate total amount and count for each payment method
foreach ($paymentMethods as $method) {
$transactions = Redis::keys("transaction:*:payment_method:{$method}"); // on this keys why show an empty
// Check if $transactions is an array before counting
$totalAmount = 0;
$totalCount = count($transactions);
foreach ($transactions as $key) {
$data = Redis::hgetall($key);
$totalAmount += $data['amount'];
}
$report[] = [
'payment_method' => $method,
'total_amount' => $totalAmount,
'total_count' => $totalCount,
];
}
return response()->json($report);
I need to handle much of the data and store the data to Redis so I can consume the cache data.
My goal is I just want to calculate the report by payment method but when I used Redis::keys(“transaction:*:payment_method:{$method}”) the value show an empty.