cache http requests on file system in go

To cache HTTP requests on the file system, you can use the ioutil package in Go. Here's an example implementation:

main.go
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
)

func main() {
    url := "https://www.example.com"

    // Check if the cached response is available on the file system
    cachePath := "/tmp/cache/example.html"
    if _, err := os.Stat(cachePath); err == nil {
        // Cached response is available, return it
        data, err := ioutil.ReadFile(cachePath)
        if err != nil {
            fmt.Println("Error reading cached file: ", err)
        } else {
            fmt.Println("Returning cached response")
            fmt.Println(string(data))
            return
        }
    }

    // Cached response is not available, fetch the response from the web server
    fmt.Println("Fetching response from server")
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println("Error fetching response: ", err)
        return
    }
    defer resp.Body.Close()

    // Read the response body
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body: ", err)
        return
    }

    // Write the response body to the cache
    err = ioutil.WriteFile(cachePath, body, 0644)
    if err != nil {
        fmt.Println("Error writing to cache: ", err)
    }

    fmt.Println("Returning response from server")
    fmt.Println(string(body))
}
1329 chars
52 lines

In this example, we check if the cached response is available on the file system using os.Stat, if it is available we read the contents of the file using ioutil.ReadFile and return that as the response. If the cached response is not available, we fetch the response from the web server using http.Get, then read and return the response body. Finally, we write the response body to the cache using ioutil.WriteFile.

gistlibby LogSnag