Skip to content

cache

import "github.com/wnjoon/go-yfinance/pkg/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:

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

Default settings

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

func ClearGlobal()

ClearGlobal removes all entries from the global cache.

func DeleteGlobal

func DeleteGlobal(key string)

DeleteGlobal removes a key from the global cache.

func GetGlobal

func GetGlobal(key string) (interface{}, bool)

GetGlobal retrieves a value from the global cache.

func GetGlobalString

func GetGlobalString(key string) (string, bool)

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

func SetGlobalWithTTL(key string, value interface{}, ttl time.Duration)

SetGlobalWithTTL stores a value in the global cache with a custom TTL.

type Cache

Cache is a thread-safe, TTL-based in-memory cache.

type Cache struct {
    // contains filtered or unexported fields
}

func New

func New(opts ...Option) *Cache

New creates a new Cache with the given options.

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all entries from the cache.

func (*Cache) Close

func (c *Cache) Close()

Close stops the cleanup goroutine and releases resources.

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes a key from the cache.

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get retrieves a value from the cache. Returns the value and true if found and not expired, otherwise nil and false.

func (*Cache) GetString

func (c *Cache) GetString(key string) (string, bool)

GetString retrieves a string value from the cache. Returns the value and true if found, not expired, and is a string.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache (including expired ones).

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

Set stores a value in the cache with the default TTL.

func (*Cache) SetWithTTL

func (c *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration)

SetWithTTL stores a value in the cache with a custom TTL.

type Option

Option is a function that configures a Cache.

type Option func(*Cache)

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the default TTL for cache entries.