Documentation
¶
Index ¶
- Constants
- Variables
- type FileCache
- func (f *FileCache) Close() error
- func (f *FileCache) Delete(_ context.Context, key string) error
- func (f *FileCache) Flush(_ context.Context)
- func (f *FileCache) Get(_ context.Context, key string, v any) error
- func (f *FileCache) Invalidate(_ context.Context, tags []string)
- func (f *FileCache) Ping(_ context.Context) error
- func (f *FileCache) Set(_ context.Context, key string, value any, options Options)
- type MemCache
- func (c *MemCache) Close() error
- func (c *MemCache) Delete(_ context.Context, key string) error
- func (c *MemCache) Flush(_ context.Context)
- func (c *MemCache) Get(_ context.Context, key string, value interface{}) error
- func (c *MemCache) Invalidate(_ context.Context, tags []string)
- func (c *MemCache) Ping(_ context.Context) error
- func (c *MemCache) Set(_ context.Context, key string, value interface{}, opts Options)
- type OSCache
- func (o *OSCache) Close() error
- func (o *OSCache) Delete(_ context.Context, key string) error
- func (o *OSCache) Flush(_ context.Context)
- func (o *OSCache) Get(ctx context.Context, key string, v any) error
- func (o *OSCache) Invalidate(_ context.Context, tags []string)
- func (o *OSCache) Ping(_ context.Context) error
- func (o *OSCache) Set(_ context.Context, key string, value any, options Options)
- type Options
- type Redis
- func (r *Redis) Close() error
- func (r *Redis) Delete(ctx context.Context, key string) error
- func (r *Redis) Flush(ctx context.Context)
- func (r *Redis) Get(ctx context.Context, key string, v any) error
- func (r *Redis) Invalidate(ctx context.Context, tags []string)
- func (r *Redis) Ping(ctx context.Context) error
- func (r *Redis) Set(ctx context.Context, key string, value any, options Options)
- type Store
Examples ¶
Constants ¶
const Forever = -1
Forever defines an infinite expiration time.
Variables ¶
var ErrNotFound = errors.New("key not found")
ErrNotFound is the error that's returned by the Get() method when no cache key was found.
Functions ¶
This section is empty.
Types ¶
type FileCache ¶
type FileCache struct {
// contains filtered or unexported fields
}
FileCache represents a file-based cache store that persists data to a JSON file on disk.
func NewFileCache ¶
NewFileCache creates a new FileCache instance, initializing it with the given file path. If the file doesn't exist, it will be created.
TODO, add pretty print mechanism.
type MemCache ¶
type MemCache struct {
// contains filtered or unexported fields
}
MemCache represents an in-memory cache store.
func NewInMemory ¶
NewInMemory creates a new instance of MemCache.
type OSCache ¶
type OSCache struct {
// contains filtered or unexported fields
}
OSCache represents a file system-based cache where each key is stored as a separate file. It uses a separate index file to store expiration times and tags named index.json which is stored in the base directory.
func NewOSCache ¶
NewOSCache creates and initializes a new OSCache instance. It creates the base directory if it doesn't exist and loads the index.
type Options ¶
type Options struct { // Expiration allows to specify a global expiration // time when setting a value. Expiration time.Duration // Tags allows specifying associated tags to the // current value. Tags []string }
Options represents the cache store available options when using Set().
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis defines the methods for interacting with the cache layer.
type Store ¶
type Store interface { // Ping pings the Redis cache to ensure its alive. Ping(context.Context) error // Get retrieves a specific item from the cache by key. Values are // automatically marshalled for use with Redis. Get(context.Context, string, any) error // Set stores a singular item in memory by key, value // and options (tags and expiration time). Values are automatically // marshalled for use with Redis & Memcache. Set(context.Context, string, any, Options) // Delete removes a singular item from the cache by // a specific key. Delete(context.Context, string) error // Invalidate removes items from the cache via the tags passed. Invalidate(context.Context, []string) // Flush removes all items from the cache. Flush(context.Context) // Closer closes the client, releasing any open resources. io.Closer }
Store defines methods for interacting with the caching system.
Example ¶
var store cache.Store if env.IsDevelopment() { store = cache.NewInMemory(time.Hour) } else { store = cache.NewRedis(&redis.Options{ Addr: "localhost:6379", Password: "passsword", DB: 0, }) } // Set a value in the cache with a specific key and options store.Set(context.Background(), "key", "value", cache.Options{ Expiration: time.Minute * 30, Tags: []string{"tag1", "tag2"}, }) // Retrieve the value from the cache var value string err := store.Get(context.Background(), "key", &value) if err != nil { fmt.Println("Error getting value from MemCache:", err) return } fmt.Println(value)
Output: value
Directories
¶
Path | Synopsis |
---|---|
Code generated by MockGen.
|
Code generated by MockGen. |
Code generated by MockGen.
|
Code generated by MockGen. |