Sobes.tech
Middle

Have you worked with caching on the frontend and backend?

sobes.tech AI

Answer from AI

Yes, I have worked with caching both on the frontend and backend. On the frontend, I usually used browser cache and service workers to cache static resources and API requests, which improved performance and reduced server load.

On the backend, I applied various approaches: in-memory caching using Redis or Memcached to speed up access to frequently requested data, as well as HTTP caching using Cache-Control and ETag headers. For example, I often cached the results of complex API queries to reduce response time and database load.

An example of caching in Go with Redis:

import (
    "github.com/go-redis/redis/v8"
    "context"
    "time"
)

ctx := context.Background()
client := redis.NewClient(&redis.Options{
    Addr: "localhost:6379",
})

// Save data to cache for 10 minutes
err := client.Set(ctx, "user:123", "John Doe", 10*time.Minute).Err()
if err != nil {
    panic(err)
}

// Retrieve data from cache
val, err := client.Get(ctx, "user:123").Result()
if err == redis.Nil {
    // Cache is empty, need to fetch from DB
} else if err != nil {
    panic(err)
} else {
    fmt.Println("Cached value:", val)
}