Skip to content

[API] Implement ETag Support for Caching #74

@fentz26

Description

@fentz26

Problem

No HTTP caching mechanism:

  • Every request hits database
  • No conditional GET support
  • Wasted bandwidth on unchanged data

Proposed Solution

ETag Implementation

func handleGetTask(w http.ResponseWriter, r *http.Request) {
    task, _ := store.GetTask(ctx, id)
    
    // Generate ETag from content hash
    etag := generateETag(task)
    w.Header().Set("ETag", etag)
    w.Header().Set("Cache-Control", "private, max-age=60")
    
    // Check If-None-Match
    if r.Header.Get("If-None-Match") == etag {
        w.WriteHeader(http.StatusNotModified)
        return
    }
    
    json.NewEncoder(w).Encode(task)
}

func generateETag(v interface{}) string {
    data, _ := json.Marshal(v)
    hash := sha256.Sum256(data)
    return fmt.Sprintf("\"%x\"", hash[:8])
}

Acceptance Criteria

  • ETag header on all GET responses
  • If-None-Match conditional GET
  • 304 Not Modified response
  • Cache-Control headers
  • Last-Modified header

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions