Ctrl K
ComunidadGuías…

store/store.go

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
package store

import (
	"sync"
	"time"

	"github.com/usememos/memos/internal/profile"
	"github.com/usememos/memos/provider/storage"
	"github.com/usememos/memos/store/cache"
)

// Store provides database access to all raw objects.
type Store struct {
	profile *profile.Profile
	driver  Driver

	userCreateMu   sync.Mutex
	authConfigMu   sync.Mutex
	refreshTokenMu sync.Mutex
	patMu          sync.Mutex
	memoViewMu     sync.Mutex

	// Cache settings
	cacheConfig cache.Config

	// Caches
	instanceSettingCache *cache.Cache // cache for instance settings
	userCache            *cache.Cache // cache for users
	userSettingCache     *cache.Cache // cache for user settings

	// storageDriverCache reuses object-storage clients across requests, keyed
	// by the resolved configuration. Reset whenever the STORAGE setting changes.
	storageDriverMu         sync.Mutex
	storageDriverGeneration uint64
	storageDriverCache      map[storageDriverCacheKey]storage.Driver
}

// New creates a new instance of Store.
func New(driver Driver, profile *profile.Profile) *Store {
	// Default cache settings
	cacheConfig := cache.Config{
		DefaultTTL:      10 * time.Minute,
		CleanupInterval: 5 * time.Minute,
		MaxItems:        1000,
		OnEviction:      nil,
	}

	store := &Store{
		driver:               driver,
		profile:              profile,
		cacheConfig:          cacheConfig,
		instanceSettingCache: cache.New(cacheConfig),
		userCache:            cache.New(cacheConfig),
		userSettingCache:     cache.New(cacheConfig),
	}

	return store
}

func (s *Store) GetDriver() Driver {
	return s.driver
}

// GetDataDir returns the store data directory.
func (s *Store) GetDataDir() string {
	return s.profile.Data
}

func (s *Store) Close() error {
	// Stop all cache cleanup goroutines
	s.instanceSettingCache.Close()
	s.userCache.Close()
	s.userSettingCache.Close()

	return s.driver.Close()
}