GOTTH+
GitHub

Building

Server-side cache

The optional Redis or Memcached cache, CheckCache, and how to key, invalidate and secure what you cache.

Choosing a cache

A GOTTH+ project can keep the results of slow or frequent database queries in a cache server, so most requests skip the database. Pick one when you create the project:

--cacheServerCacheURL
none (default)No cache. The project has no internal/cache package and no CacheURL.
redisRedis, or anything that speaks its protocol: Valkey, KeyDB, Dragonfly, a managed Redis. Uses go-redis.redis://localhost:6379/0, or rediss://user:password@host:6380/0 for TLS
memcachedMemcached, or anything that speaks its text protocol. Uses gomemcache.memcached://localhost:11211, or several servers: memcached://host1:11211,host2:11211
go run github.com/owenwexler/create-gotth-plus-app@latest my-app --cache=redis

The gotth-plus template itself ships the Redis version. A project whose pages read little from the database doesn't need a cache at all.

Turning it on

The cache is off until CacheURL is set in .env, so a new project runs, and its tests pass, without a cache server. CacheKeyPrefix is optional: every key becomes <prefix>:<key>, which keeps apps or environments apart on a shared server, and changing it drops everything cached at once.

CacheURL=redis://localhost:6379/0
CacheKeyPrefix=myapp

main.go connects at startup with cache.Connect and passes the cache to app.NewHandler(env, db, c). Hand it from newMux to the repositories or services that need it. A CacheURL that can't be parsed stops the app at startup; a cache server that doesn't answer is only a warning in the log. With no CacheURL, the cache is cache.Disabled(), which stores nothing.

For a local server, install Redis, Valkey or Memcached from your package manager, or run one in Docker: docker run --rm -p 6379:6379 valkey/valkey, or docker run --rm -p 127.0.0.1:11211:11211 memcached.

CheckCache

cache.CheckCache is a read-through helper. It returns the value cached under a key. On a miss it calls your function, usually a repository query, stores the result as JSON for the given time, and returns it.

cities, err := cache.CheckCache(ctx, c, "cities", 7*24*time.Hour,
	func(ctx context.Context) ([]model.City, error) { return repo.Cities(ctx) })
  • An error from your function is returned as it is, so errors.Is still works, and it is never cached.
  • The cache failing never fails the request. A read error counts as a miss, and a write error is only logged, so with the cache server down every call goes to the database. Short timeouts (500 ms) keep that fallback quick.
  • Values go through encoding/json, so cache types with exported fields. An entry that no longer decodes, because the type changed, is fetched again and overwritten.
  • The time to live must be positive. Nothing is cached forever.
  • Hits and misses are logged at debug level with the key.

Keys and invalidation

  • Put everything the value depends on in the key: the page, the filter and, above all, the user or owner for per-user data, such as tasks:owner:<id>:page:2. A key without the owner serves one user's data to another.
  • Check authorisation before reading the cache, exactly as you would before querying the database.
  • When the data changes, delete the keys it affects with c.Delete(ctx, keys...), or keep the time to live short enough that stale data doesn't matter.
  • Read through the cache in repositories or services, never in handlers.
  • Memcached keys must be at most 250 bytes, prefix included, with no spaces or control characters, and values at most 1 MB by default.

Security

  • Never expose the cache server to the internet. Anyone who reaches it can read every entry, plant poisoned ones, or flush it all. Keep it on localhost or a private network, behind a firewall.
  • Redis: set a password or an ACL user, and use rediss:// (TLS) for anything that isn't on the same host. CacheURL then carries a password, so it belongs in the secret store, and the app never repeats it in an error.
  • Memcached: the client has no passwords and no TLS, so the network is the only protection. Keep UDP off on the servers (-U 0).
  • Don't cache secrets such as password hashes, session data or tokens, and don't put secrets in keys, which are logged.
  • /healthz doesn't check the cache, because the app works without it. Watch the log for cache read failed warnings instead.

Testing

The cache tests need no server: the Redis client is tested against an in-memory Redis (miniredis), and the Memcached client against a small fake Memcached server in memcached_test.go. Both pass the same contract, testContract in internal/cache/cache_test.go, which covers misses, overwrites, expiry, deletes and CheckCache. Integration tests build the app with cache.Disabled().

Was this page helpful?

Be the first to answer.