{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cache\n\n## Variables\n\n```go\nvar ErrClosed = errors.New(\"cache: provider closed\")\n```\nErrClosed is returned by a provider that has been closed.\n\n```go\nvar ErrFlushNotAllowed = errors.New(\"cache: Clear flushes the entire server; set AllowFlush in the provider config to permit it\")\n```\nErrFlushNotAllowed is returned by Clear on shared-server providers (Redis, Memcache) unless AllowFlush is set in their config, because Clear flushes the whole server/DB.\n\n```go\nvar ErrNotFound = errors.New(\"cache: key not found\")\n```\nErrNotFound is returned when a key is not in the cache. The key is deliberately not included in the error: keys may embed credentials (e.g. session tokens).\n\n## Functions\n\n```go\nfunc Close() error\n```\nClose closes the cache and releases resources.\n\n```go\nfunc Initialize(provider Provider)\n```\nInitialize initializes the cache with a provider. If not called, the package will use an in-memory provider by default.\n\n```go\nfunc SetDefaultCache(cache *Cache)\n```\nSetDefaultCache sets a custom cache instance as the default cache. This is useful for testing or when you want to use a pre-configured cache instance. The caller keeps ownership of both the new and the displaced cache; neither is closed.\n\n```go\nfunc UseMemcache(config *MemcacheConfig) error\n```\nUseMemcache configures the cache to use Memcache storage.\n\n```go\nfunc UseMemory(opts *Options) error\n```\nUseMemory configures the cache to use in-memory storage.\n\n```go\nfunc UseRedis(config *RedisConfig) error\n```\nUseRedis configures the cache to use Redis storage.\n\n## Types\n\n```go\ntype Cache struct {\n\t// contains filtered or unexported fields\n}\n```\nCache is the main cache manager that wraps a Provider.\n\n```go\nfunc GetDefaultCache() *Cache\n```\nGetDefaultCache returns the default cache instance. Initializes with in-memory provider if not already initialized. Safe for concurrent use.\n\n```go\nfunc NewCache(provider Provider) *Cache\n```\nNewCache creates a new cache manager with the specified provider.\n\n```go\nfunc (c *Cache) Clear(ctx context.Context) error\n```\nClear removes all items from the cache.\n\n```go\nfunc (c *Cache) Close() error\n```\nClose closes the cache and releases any resources.\n\n```go\nfunc (c *Cache) Delete(ctx context.Context, key string) error\n```\nDelete removes a key from the cache.\n\n```go\nfunc (c *Cache) DeleteByPattern(ctx context.Context, pattern string) error\n```\nDeleteByPattern removes all keys matching the pattern.\n\n```go\nfunc (c *Cache) DeleteByTag(ctx context.Context, tag string) error\n```\nDeleteByTag removes all keys associated with the given tag.\n\n```go\nfunc (c *Cache) Exists(ctx context.Context, key string) bool\n```\nExists checks if a key exists in the cache.\n\n```go\nfunc (c *Cache) Get(ctx context.Context, key string, dest interface{}) error\n```\nGet retrieves and deserializes a value from the cache.\n\n```go\nfunc (c *Cache) GetBytes(ctx context.Context, key string) ([]byte, error)\n```\nGetBytes retrieves raw bytes from the cache.\n\n```go\nfunc (c *Cache) GetOrSet(ctx context.Context, key string, dest interface{}, ttl time.Duration, loader func() (interface{}, error)) error\n```\nGetOrSet retrieves a value from cache, or sets it if it doesn't exist. The loader function is called only if the key is not found in cache.\n\n```go\nfunc (c *Cache) Remember(ctx context.Context, key string, ttl time.Duration, loader func() (interface{}, error)) (interface{}, error)\n```\nRemember is a convenience function that caches the result of a function call. It's similar to GetOrSet but returns the value directly.\n\nWARNING: the returned type differs between a hit and a miss. On a hit the value is generic decoded JSON (map\\[string]interface{}, \\[]interface{}, float64, string, ...); on a miss it is exactly what loader returned. Do not type-assert the result to a concrete type; prefer GetOrSet, which decodes into a typed destination.\n\n```go\nfunc (c *Cache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error\n```\nSet serializes and stores a value in the cache with the specified TTL.\n\n```go\nfunc (c *Cache) SetBytes(ctx context.Context, key string, value []byte, ttl time.Duration) error\n```\nSetBytes stores raw bytes in the cache with the specified TTL.\n\n```go\nfunc (c *Cache) SetBytesWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error\n```\nSetBytesWithTags stores raw bytes in the cache with the specified TTL and tags.\n\n```go\nfunc (c *Cache) SetWithTags(ctx context.Context, key string, value interface{}, ttl time.Duration, tags []string) error\n```\nSetWithTags serializes and stores a value in the cache with the specified TTL and tags.\n\n```go\nfunc (c *Cache) Stats(ctx context.Context) (*CacheStats, error)\n```\nStats returns statistics about the cache.\n\n```go\ntype CacheStats struct {\n\tHits          int64          `json:\"hits\"`\n\tMisses        int64          `json:\"misses\"`\n\tKeys          int64          `json:\"keys\"`\n\tProviderType  string         `json:\"provider_type\"`\n\tProviderStats map[string]any `json:\"provider_stats,omitempty\"`\n}\n```\nCacheStats contains cache statistics.\n\n```go\nfunc GetStats(ctx context.Context) (*CacheStats, error)\n```\nGetStats returns cache statistics.\n\n```go\ntype MemcacheConfig struct {\n\t// Servers is a list of memcache server addresses (e.g., \"localhost:11211\")\n\tServers []string\n\n\t// MaxIdleConns is the maximum number of idle connections (default: 2)\n\tMaxIdleConns int\n\n\t// Timeout for connection operations (default: 1 second)\n\tTimeout time.Duration\n\n\t// Options contains general cache options\n\tOptions *Options\n\n\t// AllowFlush permits Clear() to run flush_all, which wipes every key on every\n\t// configured server (including data not owned by this cache). Off by default.\n\tAllowFlush bool\n}\n```\nMemcacheConfig contains Memcache-specific configuration.\n\n```go\ntype MemcacheProvider struct {\n\t// contains filtered or unexported fields\n}\n```\nMemcacheProvider is a Memcache implementation of the Provider interface.\n\n```go\nfunc NewMemcacheProvider(config *MemcacheConfig) (*MemcacheProvider, error)\n```\nNewMemcacheProvider creates a new Memcache cache provider.\n\n```go\nfunc (m *MemcacheProvider) Clear(ctx context.Context) error\n```\nClear removes all items from the cache. It runs flush\\_all on every configured server and therefore requires MemcacheConfig.AllowFlush.\n\n```go\nfunc (m *MemcacheProvider) Close() error\n```\nClose closes the provider and releases idle connections.\n\n```go\nfunc (m *MemcacheProvider) Delete(ctx context.Context, key string) error\n```\nDelete removes a key from the cache.\n\n```go\nfunc (m *MemcacheProvider) DeleteByPattern(ctx context.Context, pattern string) error\n```\nDeleteByPattern is not supported by Memcache; it always returns an error. Use tags (SetWithTags / DeleteByTag) for group invalidation instead.\n\n```go\nfunc (m *MemcacheProvider) DeleteByTag(ctx context.Context, tag string) error\n```\nDeleteByTag removes all keys associated with the given tag.\n\n```go\nfunc (m *MemcacheProvider) Exists(ctx context.Context, key string) bool\n```\nExists checks if a key exists in the cache.\n\n```go\nfunc (m *MemcacheProvider) Get(ctx context.Context, key string) ([]byte, bool)\n```\nGet retrieves a value from the cache by key.\n\n```go\nfunc (m *MemcacheProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error\n```\nSet stores a value in the cache with the specified TTL.\n\n```go\nfunc (m *MemcacheProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error\n```\nSetWithTags stores a value in the cache with the specified TTL and tags. Note: Tag support in Memcache is limited and less efficient than Redis. The tag index is updated with compare-and-swap; if it cannot be updated the value is removed again and an error is returned, so an untracked entry is never left behind.\n\n```go\nfunc (m *MemcacheProvider) Stats(ctx context.Context) (*CacheStats, error)\n```\nStats returns statistics about the cache provider. Note: Memcache provider returns limited statistics.\n\n```go\ntype MemoryProvider struct {\n\t// contains filtered or unexported fields\n}\n```\nMemoryProvider is an in-memory implementation of the Provider interface.\n\n```go\nfunc NewMemoryProvider(opts *Options) *MemoryProvider\n```\nNewMemoryProvider creates a new in-memory cache provider. A MaxSize \\\u003c= 0 selects the default (10000); use MaxSize -1 for an unbounded cache. A background goroutine removes expired items until Close is called.\n\n```go\nfunc (m *MemoryProvider) CleanExpired(ctx context.Context) int\n```\nCleanExpired removes all expired items from the cache.\n\n```go\nfunc (m *MemoryProvider) Clear(ctx context.Context) error\n```\nClear removes all items from the cache.\n\n```go\nfunc (m *MemoryProvider) Close() error\n```\nClose closes the provider, stops the janitor and releases stored items. Later writes return ErrClosed and reads report a miss.\n\n```go\nfunc (m *MemoryProvider) Delete(ctx context.Context, key string) error\n```\nDelete removes a key from the cache.\n\n```go\nfunc (m *MemoryProvider) DeleteByPattern(ctx context.Context, pattern string) error\n```\nDeleteByPattern removes all keys matching the pattern. The pattern is a Go regular expression (unanchored); it is compiled before the lock is taken.\n\n```go\nfunc (m *MemoryProvider) DeleteByTag(ctx context.Context, tag string) error\n```\nDeleteByTag removes all keys associated with the given tag.\n\n```go\nfunc (m *MemoryProvider) Exists(ctx context.Context, key string) bool\n```\nExists checks if a key exists in the cache.\n\n```go\nfunc (m *MemoryProvider) Get(ctx context.Context, key string) ([]byte, bool)\n```\nGet retrieves a value from the cache by key.\n\n```go\nfunc (m *MemoryProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error\n```\nSet stores a value in the cache with the specified TTL.\n\n```go\nfunc (m *MemoryProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error\n```\nSetWithTags stores a value in the cache with the specified TTL and tags.\n\n```go\nfunc (m *MemoryProvider) Stats(ctx context.Context) (*CacheStats, error)\n```\nStats returns statistics about the cache provider.\n\n```go\ntype Options struct {\n\t// DefaultTTL is the default time-to-live for cache items.\n\tDefaultTTL time.Duration\n\n\t// MaxSize is the maximum number of items (for in-memory provider).\n\t// 0 selects the default (10000); a negative value means unbounded.\n\tMaxSize int\n\n\t// CleanupInterval is how often the in-memory provider removes expired items\n\t// (default: 1 minute).\n\tCleanupInterval time.Duration\n\n\t// EvictionPolicy determines how items are evicted (LRU, LFU, etc).\n\tEvictionPolicy string\n}\n```\nOptions contains configuration options for cache providers.\n\n```go\ntype Provider interface {\n\t// Get retrieves a value from the cache by key.\n\t// Returns nil, false if key doesn't exist or is expired.\n\tGet(ctx context.Context, key string) ([]byte, bool)\n\n\t// Set stores a value in the cache with the specified TTL.\n\t// If ttl is 0, the item never expires.\n\tSet(ctx context.Context, key string, value []byte, ttl time.Duration) error\n\n\t// SetWithTags stores a value in the cache with the specified TTL and tags.\n\t// Tags can be used to invalidate groups of related keys.\n\t// If ttl is 0, the item never expires.\n\tSetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error\n\n\t// Delete removes a key from the cache.\n\tDelete(ctx context.Context, key string) error\n\n\t// DeleteByTag removes all keys associated with the given tag.\n\tDeleteByTag(ctx context.Context, tag string) error\n\n\t// DeleteByPattern removes all keys matching the pattern.\n\t// Pattern syntax depends on the provider implementation.\n\tDeleteByPattern(ctx context.Context, pattern string) error\n\n\t// Clear removes all items from the cache.\n\tClear(ctx context.Context) error\n\n\t// Exists checks if a key exists in the cache.\n\tExists(ctx context.Context, key string) bool\n\n\t// Close closes the provider and releases any resources.\n\tClose() error\n\n\t// Stats returns statistics about the cache provider.\n\tStats(ctx context.Context) (*CacheStats, error)\n}\n```\nProvider defines the interface that all cache providers must implement.\n\n```go\ntype RedisConfig struct {\n\t// Host is the Redis server host (default: localhost)\n\tHost string\n\n\t// Port is the Redis server port (default: 6379)\n\tPort int\n\n\t// Password for Redis authentication (optional)\n\tPassword string\n\n\t// DB is the Redis database number (default: 0)\n\tDB int\n\n\t// PoolSize is the maximum number of connections (default: 10)\n\tPoolSize int\n\n\t// Options contains general cache options\n\tOptions *Options\n\n\t// AllowFlush permits Clear() to run FLUSHDB, which wipes the entire logical Redis DB\n\t// (including data that is not owned by this cache). Off by default.\n\tAllowFlush bool\n}\n```\nRedisConfig contains Redis-specific configuration.\n\n```go\ntype RedisProvider struct {\n\t// contains filtered or unexported fields\n}\n```\nRedisProvider is a Redis implementation of the Provider interface.\n\n```go\nfunc NewRedisProvider(config *RedisConfig) (*RedisProvider, error)\n```\nNewRedisProvider creates a new Redis cache provider.\n\n```go\nfunc (r *RedisProvider) Clear(ctx context.Context) error\n```\nClear removes all items from the cache. It runs FLUSHDB and therefore requires RedisConfig.AllowFlush.\n\n```go\nfunc (r *RedisProvider) Close() error\n```\nClose closes the provider and releases any resources.\n\n```go\nfunc (r *RedisProvider) Delete(ctx context.Context, key string) error\n```\nDelete removes a key from the cache.\n\n```go\nfunc (r *RedisProvider) DeleteByPattern(ctx context.Context, pattern string) error\n```\nDeleteByPattern removes all keys matching the pattern.\n\n```go\nfunc (r *RedisProvider) DeleteByTag(ctx context.Context, tag string) error\n```\nDeleteByTag removes all keys associated with the given tag.\n\n```go\nfunc (r *RedisProvider) Exists(ctx context.Context, key string) bool\n```\nExists checks if a key exists in the cache.\n\n```go\nfunc (r *RedisProvider) Get(ctx context.Context, key string) ([]byte, bool)\n```\nGet retrieves a value from the cache by key.\n\n```go\nfunc (r *RedisProvider) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error\n```\nSet stores a value in the cache with the specified TTL.\n\n```go\nfunc (r *RedisProvider) SetWithTags(ctx context.Context, key string, value []byte, ttl time.Duration, tags []string) error\n```\nSetWithTags stores a value in the cache with the specified TTL and tags.\n\n```go\nfunc (r *RedisProvider) Stats(ctx context.Context) (*CacheStats, error)\n```\nStats returns statistics about the cache provider. Only an allowlist of numeric counters from INFO is exposed, not the raw output.\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/cache","name":"cache","synopsis":"","isRedistributable":true}
