Documentation ¶
Overview ¶
Package cache offers a library for managing caches in a unified manner in Go.
This package enables developers to transition between various cache implementations (such as Redis, Redis Cluster, Memcache, etc.) by altering the URL scheme. This is achieved by offering consistent, idiomatic interfaces for common operations.
Central to this package are "portable types", built atop service-specific drivers for supported cache services. For instance, cache.Cache portable type instances can be created using redis.OpenCache, memcache.OpenCache, or any other supported driver. This allows the cache.Cache to be used across your application without concern for the underlying implementation.
URL Format ¶
The cache package uses URLs to specify cache implementations. The URL scheme is used to determine the cache implementation to use. The URL format is:
scheme://<host>:<port>[?query]
The scheme is used to determine the cache implementation. The host and port are used to connect to the cache service. The optional query parameters can be used to configure the cache implementation. Each cache implementation supports different query parameters.
Usage ¶
To use a cache implementation, import the relevant driver package and use the OpenCache function to create a new cache. The cache package will automatically use the correct cache implementation based on the URL scheme.
import ( "context" "log" "github.com/bartventer/gocache" // Enable the Redis cache implementation _ "github.com/bartventer/gocache/redis" ) func main() { ctx := context.Background() urlStr := "redis://localhost:7000?maxretries=5&minretrybackoff=1000" c, err := cache.OpenCache(ctx, urlStr) if err != nil { log.Fatalf("Failed to initialize cache: %v", err) } // Now you can use c with the cache.Cache interface err = c.Set(ctx, "key", "value") if err != nil { log.Fatalf("Failed to set key: %v", err) } value, err := c.Get(ctx, "key") if err != nil { log.Fatalf("Failed to get key: %v", err) } log.Printf("Value: %s", value) }
Drivers ¶
For specific URL formats, query parameters, and examples, refer to the documentation of each cache implementation.
Index ¶
- Variables
- func RegisterCache(scheme string, opener URLOpener)
- func ValidateTTL(ttl time.Duration) error
- type Cache
- func (c *Cache) Clear(ctx context.Context) error
- func (c *Cache) Close() error
- func (c *Cache) Count(ctx context.Context, pattern string, modifiers ...keymod.Mod) (int64, error)
- func (c *Cache) Del(ctx context.Context, key string, modifiers ...keymod.Mod) error
- func (c *Cache) DelKeys(ctx context.Context, pattern string, modifiers ...keymod.Mod) error
- func (c *Cache) Exists(ctx context.Context, key string, modifiers ...keymod.Mod) (bool, error)
- func (c *Cache) Get(ctx context.Context, key string, modifiers ...keymod.Mod) ([]byte, error)
- func (c *Cache) Ping(ctx context.Context) error
- func (c *Cache) Set(ctx context.Context, key string, value interface{}, modifiers ...keymod.Mod) error
- func (c *Cache) SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, ...) error
- type URLOpener
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoCache is returned when no cache implementation is available. ErrNoCache = errors.New("gocache: no cache implementation available") // ErrKeyNotFound is returned when a key is not found in the cache. ErrKeyNotFound = errors.New("gocache: key not found") // ErrPatternMatchingNotSupported is returned when a pattern matching operation is not supported // by the cache implementation. ErrPatternMatchingNotSupported = errors.New("gocache: pattern matching not supported") // ErrInvalidTTL is returned when an invalid TTL is provided. ErrInvalidTTL = errors.New("gocache: invalid TTL") )
Functions ¶
func RegisterCache ¶
RegisterCache registers a URLOpener for a given scheme. If a URLOpener is already registered for the scheme, it panics.
func ValidateTTL ¶ added in v0.10.0
ValidateTTL validates the TTL and returns an error if it's invalid. A TTL is invalid if it's negative.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a portable type that implements driver.Cache.
func NewCache ¶ added in v0.14.0
NewCache creates a new Cache using the provided driver. Not intended for direct application use.
func OpenCache ¶
OpenCache opens a Cache for the provided URL string. It returns an error if the URL cannot be parsed, or if no URLOpener is registered for the URL's scheme.
func (*Cache) Clear ¶
Clear implements driver.Cache.
func (*Cache) Count ¶
Count implements driver.Cache.
func (*Cache) Del ¶
Del implements driver.Cache.
func (*Cache) DelKeys ¶
DelKeys implements driver.Cache.
func (*Cache) Exists ¶
Exists implements driver.Cache.
func (*Cache) Get ¶
Get implements driver.Cache.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
gcerrors
Package gcerrors provides error handling for the gocache package.
|
Package gcerrors provides error handling for the gocache package. |
logext
Package logext provides a custom log.Logger interface for debug logging.
|
Package logext provides a custom log.Logger interface for debug logging. |
urlparser
Package urlparser provides utilities for parsing URL query parameters into a given struct.
|
Package urlparser provides utilities for parsing URL query parameters into a given struct. |
memcache
module
|
|
pkg
|
|
driver
Package driver defines an interface for cache implementations, providing a standardized way to interact with different caching mechanisms.
|
Package driver defines an interface for cache implementations, providing a standardized way to interact with different caching mechanisms. |
drivertest
Package drivertest provides conformance tests for cache implementations.
|
Package drivertest provides conformance tests for cache implementations. |
keymod
Package keymod provides functions for modifying keys.
|
Package keymod provides functions for modifying keys. |
ramcache
module
|
|
redis
module
|
|
rediscluster
module
|