Documentation
¶
Overview ¶
Package cache provides middleware and utilities for HTTP response caching in Gin web applications. It supports pluggable cache stores, cache key generation, and decorators for page and site-level caching.
Index ¶
- Constants
- Variables
- func Cache(store *persistence.CacheStore) gin.HandlerFunc
- func CachePage(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
- func CachePageAtomic(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
- func CachePageWithoutHeader(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
- func CachePageWithoutQuery(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
- func CreateKey(u string) string
- func RegisterResponseCacheGob()
- func SiteCache(store persistence.CacheStore, expire time.Duration) gin.HandlerFunc
Constants ¶
const (
CACHE_MIDDLEWARE_KEY = "gincontrib.cache"
)
Variables ¶
var PageCachePrefix = "gincontrib.page.cache"
Functions ¶
func Cache ¶
func Cache(store *persistence.CacheStore) gin.HandlerFunc
Cache is a Gin middleware that injects the cache store into the request context.
func CachePage ¶
func CachePage(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
CachePage is a decorator that caches the response of the given handler based on the request URI. If a cached response exists, it is served directly. Otherwise, the handler is executed and its response is cached. If the context is aborted, the cache entry is deleted.
func CachePageAtomic ¶
func CachePageAtomic(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
CachePageAtomic is a decorator that wraps CachePage with a mutex to ensure atomic access. This prevents concurrent requests from generating duplicate cache entries for the same resource.
func CachePageWithoutHeader ¶
func CachePageWithoutHeader(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
CachePageWithoutHeader is a decorator that caches responses without restoring headers from the cache. Only the status and body are restored from the cache.
func CachePageWithoutQuery ¶
func CachePageWithoutQuery(store persistence.CacheStore, expire time.Duration, handle gin.HandlerFunc) gin.HandlerFunc
CachePageWithoutQuery is a decorator that caches responses ignoring GET query parameters. The cache key is based only on the request path, so all queries to the same path share the cache.
func CreateKey ¶
CreateKey generates a cache key for the given string using the package-specific prefix.
func RegisterResponseCacheGob ¶
func RegisterResponseCacheGob()
RegisterResponseCacheGob registers the responseCache type with the encoding/gob package. This is required for gob-based cache stores to serialize/deserialize cached responses.
func SiteCache ¶
func SiteCache(store persistence.CacheStore, expire time.Duration) gin.HandlerFunc
SiteCache is a Gin middleware that caches entire site responses based on the request URI. If a cached response exists, it is written directly; otherwise, the request proceeds as normal.
Types ¶
This section is empty.