Implement Rate Limiting In ASP.NET Core Web API

Image
Introduction In this article, you'll learn about rate limiting and how to apply it to your ASP.NET Core Web API. What is Rate Limiting? If you're building an API, it might be accessed by multiple clients. Sometimes, certain clients use the API excessively without any restrictions. However, if you need to control how often a specific client can use the API within a set timeframe, you can achieve this through Rate Limiting. Rate Limiting is like setting a speed limit on how many times someone can ask for something from a website or app in a certain amount of time. It means each person or device using the service can only ask for a certain number of things within a particular timeframe. Why do we need Rate Limiting? Rate Limiting is a useful tool for safeguarding against harmful bot attacks. Imagine a hacker sending bots to repeatedly request access to an API endpoint. This flood of requests can ove...

How to Optimizing Query Performance in Laravel



In this article, we will explore the process of checking the execution time of queries in Laravel. By the end, you will clearly understand how to obtain the query execution time in Laravel. Additionally, we will provide practical examples, ensuring you grasp the concept effectively.


This tutorial is applicable to various versions of Laravel, including 6, 7, 8, 9, and 10.


If you're specifically interested in obtaining the SQL query execution time in Laravel, we will present two examples for your convenience. We will use the 'microtime()' function and the 'enableQueryLog()' feature to accurately calculate the query execution time in Laravel.


 so, let's see the simple example code:

Demonstration: 1


TestController.php


<?php

     

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class TestController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index(Request $request)

    {

        $startTime = microtime(true);

        $users = User::get();

        $endTime = microtime(true);   

        $executionTime = $endTime - $startTime;

        dd("Query excutionTime :-  " . $executionTime );

    }

}


Output:

Query ExcutionTime:-  0.0032229423522949


Demonstration: 2 


TestController.php


<?php

     

namespace App\Http\Controllers;

    

use Illuminate\Http\Request;

use App\Models\User;

use DB;

    

class TestController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index(Request $request)

    {

        DB::connection()->enableQueryLog();

        $users = User::get();

        $queries = DB::getQueryLog();

        dd($queries);

    }

}


Output:


array:1 [ // app/Http/Controllers/TestController.php

  0 => array:3 [

    "query" => "select * from `users`"

    "bindings" => []

    "time" => 1.79

  ]

]


If you like this blog please add a comment I will create more blogs regarding your requirements.

Thank you :)


Comments

Popular posts from this blog

What is Signal-R in c#

Implement Rate Limiting In ASP.NET Core Web API

.Net Core vs .Net framework