Categorygithub.com/diegodazpeitia/Go-Cache-API
modulepackage
0.0.0-20240906153632-85d079bb3d50
Repository: https://github.com/diegodazpeitia/go-cache-api.git
Documentation: pkg.go.dev

# README

1st, what you can check on the browser

Captura de pantalla 2024-09-06 a la(s) 12 16 11

Go-Cache-API

Overview

Go-Cache-API is a simple web application built with Go and the Fiber framework that demonstrates caching functionality using the go-cache library. This application includes middleware to cache GET request responses and serve them efficiently. The cache is configured to store responses for 10 minutes, reducing the need for repeated processing of the same requests.

Features

  • Cache Middleware: Implements caching for GET requests to improve response time and reduce load on the server.
  • Simple API: Includes a basic API endpoint to illustrate the caching mechanism.

Installation

  1. Clone the repository:

    git clone <repository-url>
    
  2. Navigate to the project directory:

    cd Go-Cache-API
    
  3. Install the dependencies:

    go mod tidy
    

Usage

  1. Run the application:

    go run main.go
    
  2. Access the application:

    Open your web browser or use a tool like curl to access the endpoints:

    • GET /: Returns a "Hello, World 👋!" message.
    • GET /posts/:id: Demonstrates caching for POST requests. Replace :id with an actual post ID.

Code Explanation

Middleware

The CacheMiddleware function is a Fiber middleware that:

  • Checks if the request method is GET (caching is only applied to GET requests).
  • Generates a cache key based on the request path and query parameters.
  • Checks if the response is already cached. If so, it returns the cached response.
  • If not cached, it proceeds to handle the request, caches the response, and then returns it.
func CacheMiddleware(cache *cache.Cache) fiber.Handler {
    return func(c *fiber.Ctx) error {
        if c.Method() != "GET" {
            // Only cache GET requests
            return c.Next()
        }

        cacheKey := c.Path() + "?" + c.Params("id") // Generate a cache key from the request path and query parameters

        // Check if the response is already in the cache
        if cached, found := cache.Get(cacheKey); found {
            c.Response().Header.Set("Cache-Status", "HIT")
            return c.JSON(cached)
        }

        c.Set("Cache-Status", "MISS")
        err := c.Next()
        if err != nil {
            return err
        }

        var data Post
        cacheKey = c.Path() + "?" + c.Params("id")

        body := c.Response().Body()
        err = json.Unmarshal(body, &data)
        if err != nil {
            return c.JSON(fiber.Map{"error": err.Error()})
        }

        // Cache the response for 10 minutes
        cache.Set(cacheKey, data, 10*time.Minute)

        return nil
    }
}

Main Application

The main application initializes a Fiber instance and sets up a basic route with caching middleware:

func main() {
    app := fiber.New() // Creating a new instance of Fiber.

    cache := cache.New(10*time.Minute, 20*time.Minute) // setting default expiration time and clearance time.

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, World 👋!")
    })
    app.Get("/posts/:id", middleware.CacheMiddleware(cache), routes.GetPosts) //commenting this route just to test the "/" endpoint.
    app.Listen(":8080")
}

Contributing

Feel free to fork the repository and submit pull requests. For significant changes or improvements, please open an issue to discuss your proposal.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any inquiries or further information, please reach out via the repository's issue tracker.

# Packages

No description provided by the author
No description provided by the author