Documentation
¶
Overview ¶
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:
cache.SetGlobal("key", "value")
value, ok := cache.GetGlobal("key")
Thread Safety ¶
All cache operations are thread-safe and can be used from multiple goroutines.
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:
cache.SetGlobal("key", "value")
value, ok := cache.GetGlobal("key")
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 (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
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 )
Default settings
Variables ¶
This section is empty.
Functions ¶
func GetGlobalString ¶
GetGlobalString retrieves a string value from the global cache.
func SetGlobal ¶
func SetGlobal(key string, value interface{})
SetGlobal stores a value in the global cache.
func SetGlobalWithTTL ¶
SetGlobalWithTTL stores a value in the global cache with a custom TTL.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe, TTL-based in-memory cache.
func (*Cache) Close ¶
func (c *Cache) Close()
Close stops the cleanup goroutine and releases resources.
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.