cache
Package cache provides an in-memory caching layer for go-yfinance.
Overview
The cache package provides a thread-safe, TTL-based in-memory cache for storing frequently accessed data like timezone mappings and ticker information.
Basic Usage
c := cache.New(cache.WithTTL(5 * time.Minute))
c.Set("AAPL:tz", "America/New_York")
if tz, ok := c.Get("AAPL:tz"); ok {
fmt.Println(tz)
}
Global Cache
A global cache instance is available for convenience:
Configuration Options
- WithTTL: Set custom TTL for cache entries (default: 5 minutes)
Automatic Cleanup
The cache automatically removes expired entries every 10 minutes to prevent memory leaks.
Thread Safety
All cache operations are thread-safe and can be used from multiple goroutines.
Index
- Constants
- func ClearGlobal()
- func DeleteGlobal(key string)
- func GetGlobal(key string) (interface{}, bool)
- func GetGlobalString(key string) (string, bool)
- func SetGlobal(key string, value interface{})
- func SetGlobalWithTTL(key string, value interface{}, ttl time.Duration)
- type Cache
- func New(opts ...Option) *Cache
- func (c *Cache) Clear()
- func (c *Cache) Close()
- func (c *Cache) Delete(key string)
- func (c *Cache) Get(key string) (interface{}, bool)
- func (c *Cache) GetString(key string) (string, bool)
- func (c *Cache) Len() int
- func (c *Cache) Set(key string, value interface{})
- func (c *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration)
- type Option
- func WithTTL(ttl time.Duration) Option
Constants
const (
// DefaultTTL is the default time-to-live for cache entries.
DefaultTTL = 5 * time.Minute
// DefaultCleanupInterval is the default interval for cleaning expired entries.
DefaultCleanupInterval = 10 * time.Minute
)
func ClearGlobal
ClearGlobal removes all entries from the global cache.
func DeleteGlobal
DeleteGlobal removes a key from the global cache.
func GetGlobal
GetGlobal retrieves a value from the global cache.
func GetGlobalString
GetGlobalString retrieves a string value from the global cache.
func SetGlobal
SetGlobal stores a value in the global cache.
func SetGlobalWithTTL
SetGlobalWithTTL stores a value in the global cache with a custom TTL.
type Cache
Cache is a thread-safe, TTL-based in-memory cache.
func New
New creates a new Cache with the given options.
func (*Cache) Clear
Clear removes all entries from the cache.
func (*Cache) Close
Close stops the cleanup goroutine and releases resources.
func (*Cache) Delete
Delete removes a key from the cache.
func (*Cache) Get
Get retrieves a value from the cache. Returns the value and true if found and not expired, otherwise nil and false.
func (*Cache) GetString
GetString retrieves a string value from the cache. Returns the value and true if found, not expired, and is a string.
func (*Cache) Len
Len returns the number of items in the cache (including expired ones).
func (*Cache) Set
Set stores a value in the cache with the default TTL.
func (*Cache) SetWithTTL
SetWithTTL stores a value in the cache with a custom TTL.
type Option
Option is a function that configures a Cache.
func WithTTL
WithTTL sets the default TTL for cache entries.