Documentation ¶
Index ¶
- Variables
- type Cache
- func MustNewFile(ctx context.Context) Cache
- func MustNewFileWithConfig(ctx context.Context, cfg FileConfig) Cache
- func MustNewMemory(ctx context.Context) Cache
- func MustNewMemoryWithConfig(ctx context.Context, cfg MemoryConfig) Cache
- func NewFile(ctx context.Context) (Cache, error)
- func NewFileWithConfig(ctx context.Context, cfg FileConfig) (Cache, error)
- func NewMemory(ctx context.Context) (Cache, error)
- func NewMemoryWithConfig(ctx context.Context, cfg MemoryConfig) (Cache, error)
- func WithSingleFlight(c Cache) Cache
- type FileConfig
- type MemoryConfig
Constants ¶
This section is empty.
Variables ¶
var ( ErrEntryNotFound = errors.New("entry is not found") ErrEntryTooBig = errors.New("entry is too big") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { io.Closer Name() string // Set saves entry with the given key, // it returns an ErrEntryTooBig when entry is too big. Set(ctx context.Context, key string, entry []byte) error // Delete removes the given key. Delete(ctx context.Context, key string) ([]byte, error) // Get reads entry for the given key, // it returns an ErrEntryNotFound when no entry exists for the given key. Get(ctx context.Context, key string) ([]byte, error) }
Cache holds the action of caching.
func MustNewFile ¶
MustNewFile likes NewFile, but panic if error found.
func MustNewFileWithConfig ¶
func MustNewFileWithConfig(ctx context.Context, cfg FileConfig) Cache
MustNewFileWithConfig likes NewFileWithConfig, but panic if error found.
func MustNewMemory ¶
MustNewMemory likes NewMemory, but panic if error found.
func MustNewMemoryWithConfig ¶
func MustNewMemoryWithConfig(ctx context.Context, cfg MemoryConfig) Cache
MustNewMemoryWithConfig likes NewMemoryWithConfig, but panic if error found.
func NewFileWithConfig ¶
func NewFileWithConfig(ctx context.Context, cfg FileConfig) (Cache, error)
NewFileWithConfig returns a filesystem Cache implementation with given configuration.
func NewMemoryWithConfig ¶
func NewMemoryWithConfig(ctx context.Context, cfg MemoryConfig) (Cache, error)
NewMemoryWithConfig returns an in-memory Cache implementation with given configuration.
func WithSingleFlight ¶
type FileConfig ¶
type FileConfig struct { // Namespace indicates the operating workspace. Namespace string // EntryMaxAge indicates the maximum lifetime of each entry, // default is 15 mins. EntryMaxAge time.Duration // LazyEntryEviction indicates to evict an expired entry at next peeking, // by default, a background looping tries to evict expired entries per 3 mins. LazyEntryEviction bool // Buckets indicates the bucket number of cache, // value must be a power of two, // default is 12. Buckets int }
FileConfig holds the configuration of the filesystem cache, entry indexes by key and stores in one file.
func (*FileConfig) Default ¶
func (c *FileConfig) Default()
func (*FileConfig) Validate ¶
func (c *FileConfig) Validate() error
type MemoryConfig ¶
type MemoryConfig struct { // Namespace indicates the operating workspace. Namespace string // EntryMaxAge indicates the maximum lifetime of each entry, // default is 15 mins. EntryMaxAge time.Duration // LazyEntryEviction indicates to evict an expired entry at next peeking, // by default, a background looping tries to evict expired entries per 3 mins. LazyEntryEviction bool // Buckets indicates the bucket number of cache, // value must be a power of two, // default is 64. Buckets int // BucketCapacity indicates the maximum MB of each bucket, // default is 1 MB. BucketCapacity int // LazyBucketCapacityScale indicates to scale when the current bucket is not enough to put a new entry, // by default, create the bucket with the given capacity to avoid any array copying. // It's worth noticing that the bucket capacity can not exceed even configured LazyBucketCapacityScale to true. LazyBucketCapacityScale bool }
MemoryConfig holds the configuration of the in-memory cache, entry indexes by key and stores in one bucket, the total cache size is BucketCapacity * Buckets.
func (*MemoryConfig) Default ¶
func (c *MemoryConfig) Default()
func (*MemoryConfig) Validate ¶
func (c *MemoryConfig) Validate() error