cache

package
v0.0.0-...-aa0e36d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheInitialization = errors.New("mock cache initialization failed")

ErrCacheInitialization is returned when the mock cache is configured to fail

Functions

func Initialize

func Initialize() error

Initialize sets up the cache provider

func InvalidateCache

func InvalidateCache()

InvalidateCache removes all cached data

func ResetProvider

func ResetProvider()

ResetProvider resets the cache provider for testing

func SetCacheTTL

func SetCacheTTL(ttl time.Duration)

SetCacheTTL sets the cache time-to-live duration

func SetPaginatedTree

func SetPaginatedTree(page, pageSize int, response *PaginatedTreeResponse)

SetPaginatedTree stores the paginated tree in cache

func SetProvider

func SetProvider(p CacheProvider) error

SetProvider allows changing the cache provider at runtime

Types

type CacheItem

type CacheItem struct {
	Key       string         `dynamodbav:"key"`
	Data      []*models.Node `dynamodbav:"data"`
	Timestamp int64          `dynamodbav:"timestamp"`
	TTL       int64          `dynamodbav:"ttl"`
}

type CacheProvider

type CacheProvider interface {
	// GetPaginatedTree retrieves the paginated tree from cache if available.
	// Parameters:
	//   - page: The page number
	//   - pageSize: The size of each page
	// Returns:
	//   - The paginated tree response
	//   - A boolean indicating whether the response was found in cache
	GetPaginatedTree(page, pageSize int) (*PaginatedTreeResponse, bool)

	// SetPaginatedTree stores the paginated tree in cache.
	// Parameters:
	//   - page: The page number
	//   - pageSize: The size of each page
	//   - response: The paginated tree response to cache
	SetPaginatedTree(page, pageSize int, response *PaginatedTreeResponse)

	// InvalidateCache removes all cached data.
	// This is typically called when the tree structure is modified.
	InvalidateCache()

	// SetCacheTTL sets the cache time-to-live duration.
	// Parameters:
	//   - ttl: The duration after which cached data should expire
	SetCacheTTL(ttl time.Duration)

	// Initialize performs any necessary setup for the cache provider.
	// This may include establishing connections, creating cache instances,
	// or any other initialization required for the cache to function.
	// Returns an error if initialization fails.
	Initialize() error
}

CacheProvider defines the interface for cache implementations. It provides methods for caching and retrieving tree structures.

type DynamoDBAPI

type DynamoDBAPI interface {
	CreateTable(ctx context.Context, params *dynamodb.CreateTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error)
	DescribeTable(ctx context.Context, params *dynamodb.DescribeTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)
	GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
	PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
	DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
}

DynamoDBAPI defines the interface for DynamoDB operations

type DynamoDBCache

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

DynamoDBCache implements CacheProvider using DynamoDB

func NewDynamoDBCache

func NewDynamoDBCache() (*DynamoDBCache, error)

NewDynamoDBCache creates a new DynamoDB cache provider

func NewDynamoDBCacheWithClient

func NewDynamoDBCacheWithClient(client DynamoDBAPI) *DynamoDBCache

NewDynamoDBCacheWithClient creates a new DynamoDB cache provider with a custom client

func (*DynamoDBCache) GetTree

func (c *DynamoDBCache) GetTree() ([]*models.Node, bool)

GetTree retrieves the tree from DynamoDB cache if available

func (*DynamoDBCache) Initialize

func (c *DynamoDBCache) Initialize() error

Initialize creates the DynamoDB table if it doesn't exist

func (*DynamoDBCache) InvalidateCache

func (c *DynamoDBCache) InvalidateCache() error

InvalidateCache removes the tree from DynamoDB cache

func (*DynamoDBCache) SetCacheTTL

func (c *DynamoDBCache) SetCacheTTL(ttl time.Duration)

SetCacheTTL sets the cache time-to-live duration

func (*DynamoDBCache) SetTree

func (c *DynamoDBCache) SetTree(tree []*models.Node)

SetTree stores the tree in DynamoDB cache

type MemoryCache

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

MemoryCache implements CacheProvider using in-memory storage

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new in-memory cache provider

func (*MemoryCache) GetPaginatedTree

func (c *MemoryCache) GetPaginatedTree(page, pageSize int) (*PaginatedTreeResponse, bool)

GetPaginatedTree retrieves the paginated tree from cache if available

func (*MemoryCache) Initialize

func (c *MemoryCache) Initialize() error

Initialize performs any necessary setup for the cache provider

func (*MemoryCache) InvalidateCache

func (c *MemoryCache) InvalidateCache()

InvalidateCache removes all cached data

func (*MemoryCache) SetCacheTTL

func (c *MemoryCache) SetCacheTTL(ttl time.Duration)

SetCacheTTL sets the cache time-to-live duration

func (*MemoryCache) SetPaginatedTree

func (c *MemoryCache) SetPaginatedTree(page, pageSize int, response *PaginatedTreeResponse)

SetPaginatedTree stores the paginated tree in cache

type MockCache

type MockCache struct {
	GetTreeCalls    int
	SetTreeCalls    int
	InvalidateCalls int
	SetTTLCalls     int
	InitCalls       int
	ShouldFail      bool
	// contains filtered or unexported fields
}

MockCache is a cache provider that can be used for testing

func NewMockCache

func NewMockCache() *MockCache

NewMockCache creates a new mock cache provider

func (*MockCache) GetCallCounts

func (c *MockCache) GetCallCounts() (getTree, setTree, invalidate, setTTL, init int)

GetCallCounts returns the number of times each method was called

func (*MockCache) GetTree

func (c *MockCache) GetTree() ([]*models.Node, bool)

GetTree retrieves the tree from cache if available

func (*MockCache) Initialize

func (c *MockCache) Initialize() error

Initialize performs any necessary setup for the cache provider

func (*MockCache) InvalidateCache

func (c *MockCache) InvalidateCache()

InvalidateCache removes the tree from cache

func (*MockCache) Reset

func (c *MockCache) Reset()

Reset resets all counters and state

func (*MockCache) SetCacheTTL

func (c *MockCache) SetCacheTTL(ttl time.Duration)

SetCacheTTL sets the cache time-to-live duration

func (*MockCache) SetShouldFail

func (c *MockCache) SetShouldFail(shouldFail bool)

SetShouldFail makes the mock cache fail all operations

func (*MockCache) SetTree

func (c *MockCache) SetTree(tree []*models.Node)

SetTree stores the tree in cache

type MockDynamoDBClient

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

MockDynamoDBClient implements DynamoDBAPI for testing

func NewMockDynamoDBClient

func NewMockDynamoDBClient() *MockDynamoDBClient

NewMockDynamoDBClient creates a new mock DynamoDB client

func (*MockDynamoDBClient) CreateTable

func (m *MockDynamoDBClient) CreateTable(ctx context.Context, params *dynamodb.CreateTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.CreateTableOutput, error)

CreateTable mocks the CreateTable operation

func (*MockDynamoDBClient) DeleteItem

func (m *MockDynamoDBClient) DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)

DeleteItem mocks the DeleteItem operation

func (*MockDynamoDBClient) DescribeTable

func (m *MockDynamoDBClient) DescribeTable(ctx context.Context, params *dynamodb.DescribeTableInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error)

DescribeTable mocks the DescribeTable operation

func (*MockDynamoDBClient) GetItem

func (m *MockDynamoDBClient) GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)

GetItem mocks the GetItem operation

func (*MockDynamoDBClient) PutItem

func (m *MockDynamoDBClient) PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)

PutItem mocks the PutItem operation

type PaginatedTreeResponse

type PaginatedTreeResponse struct {
	Data       []*models.Node `json:"data"`
	Pagination struct {
		Page       int   `json:"page"`
		PageSize   int   `json:"pageSize"`
		Total      int64 `json:"total"`
		TotalPages int64 `json:"totalPages"`
		HasNext    bool  `json:"hasNext"`
		HasPrev    bool  `json:"hasPrev"`
	} `json:"pagination"`
}

PaginatedTreeResponse represents a paginated tree response

func GetPaginatedTree

func GetPaginatedTree(page, pageSize int) (*PaginatedTreeResponse, bool)

GetPaginatedTree retrieves the paginated tree from cache if available

type RedisCache

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

RedisCache implements CacheProvider using Redis

func NewRedisCache

func NewRedisCache() *RedisCache

NewRedisCache creates a new Redis cache provider

func (*RedisCache) Close

func (c *RedisCache) Close() error

Close closes the Redis connection

func (*RedisCache) GetPaginatedTree

func (c *RedisCache) GetPaginatedTree(page, pageSize int) (*PaginatedTreeResponse, bool)

GetPaginatedTree retrieves the paginated tree from cache if available

func (*RedisCache) Initialize

func (c *RedisCache) Initialize() error

Initialize performs any necessary setup for the cache provider

func (*RedisCache) InvalidateCache

func (c *RedisCache) InvalidateCache()

InvalidateCache removes all cached data

func (*RedisCache) SetCacheTTL

func (c *RedisCache) SetCacheTTL(ttl time.Duration)

SetCacheTTL sets the cache time-to-live duration

func (*RedisCache) SetPaginatedTree

func (c *RedisCache) SetPaginatedTree(page, pageSize int, response *PaginatedTreeResponse)

SetPaginatedTree stores the paginated tree in cache

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL