cache

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Cache

Cache is a lightweight, flexible, and efficient caching library for Go, designed to support multiple backends, including in-memory, file-based, and Redis. It provides a unified interface for caching operations, making it simple to switch between storage solutions without changing your application logic.

Whether you need a fast, ephemeral cache for high-performance applications or a persistent cache for long-term storage, this library has you covered.

With a focus on simplicity and ease of use, Cache enables developers to manage cached data effortlessly, improving application performance and reducing latency.

This package implements github.com/gouef/standards.

Static Badge

GoDoc GitHub stars Go Report Card codecov

Versions

Stable Version GitHub Release GitHub Release

Installation

To use this package in your project, add it using Go modules:

go get -u github.com/gouef/cache

Usages

Contributing

Read Contributing

Contributors

JanGalek actions-user

Documentation

Index

Constants

View Source
const FILE_EXTENSION = ".cache"
View Source
const KeepTTL = standards.KeepTTL

Variables

This section is empty.

Functions

func NewFile

func NewFile(dir string) (standards.Cache, error)

NewFile create new instance of File and check if directory exists.

func NewRedis

func NewRedis(client *redisLib.Client) standards.Cache

Types

type File

type File struct {
	Dir string
	Mu  sync.RWMutex
}

func (*File) Clear

func (c *File) Clear() error

func (*File) Commit

func (c *File) Commit() error

func (*File) DeleteItem

func (c *File) DeleteItem(key string) error

func (*File) DeleteItems

func (c *File) DeleteItems(keys ...string) error

func (*File) GetItem

func (c *File) GetItem(key string) standards.CacheItem

func (*File) GetItems

func (c *File) GetItems(keys ...string) []standards.CacheItem

func (*File) HasItem

func (c *File) HasItem(key string) bool

func (*File) Save

func (c *File) Save(item standards.CacheItem) error

func (*File) SaveDeferred

func (c *File) SaveDeferred(item standards.CacheItem) error

type FileItem

type FileItem struct {
	Key        string    `json:"key"`
	Value      any       `json:"value"`
	Expiration time.Time `json:"expiration"`
	KeepTTL    bool
}

func NewFileItem

func NewFileItem(key string) *FileItem

func (*FileItem) ExpiresAfter

func (i *FileItem) ExpiresAfter(t time.Duration)

func (*FileItem) ExpiresAt

func (i *FileItem) ExpiresAt(expiration time.Time) (standards.CacheItem, error)

func (*FileItem) Get

func (i *FileItem) Get() any

func (*FileItem) GetKey

func (i *FileItem) GetKey() string

func (*FileItem) IsHit

func (i *FileItem) IsHit() bool

func (*FileItem) Set

func (i *FileItem) Set(value any, ttl time.Duration) (standards.CacheItem, error)

type FileSimple added in v1.0.2

type FileSimple struct {
	Dir             string
	Mu              sync.RWMutex
	AllowDefaultNil bool
	KeepTTL         bool
}

func NewFileSimple added in v1.0.2

func NewFileSimple(dir string) (*FileSimple, error)

NewFileSimple create FileSimple instance with not allowed default value nil

func NewFileSimpleWithDefaultNil added in v1.0.2

func NewFileSimpleWithDefaultNil(dir string, allowDefaultNil bool) (*FileSimple, error)

NewFileSimpleWithDefaultNil create FileSimple instance

func (*FileSimple) Clear added in v1.0.2

func (c *FileSimple) Clear() error

Clear Deletes all cache's keys.

func (*FileSimple) Delete added in v1.0.2

func (c *FileSimple) Delete(key string) error

Delete Remove an item from the cache.

func (*FileSimple) DeleteMultiply added in v1.0.2

func (c *FileSimple) DeleteMultiply(keys ...string) error

DeleteMultiply Removes multiple items in a single operation.

func (*FileSimple) Get added in v1.0.2

func (c *FileSimple) Get(key string, defaultValue any) any

Get Returns a value from the cache.

func (*FileSimple) GetMultiply added in v1.0.2

func (c *FileSimple) GetMultiply(keys []string, defaultValue any) []any

GetMultiply Returns a list of cache items.

func (*FileSimple) Has added in v1.0.2

func (c *FileSimple) Has(key string) bool

Has Determines whether an item is present in the cache.

func (*FileSimple) Set added in v1.0.2

func (c *FileSimple) Set(key string, item any, ttl time.Duration) error

Set Persists a cache item.

func (*FileSimple) SetMultiply added in v1.0.2

func (c *FileSimple) SetMultiply(values map[string]any, ttl time.Duration) error

SetMultiply Persists a cache items.

type Memory

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

func NewMemory

func NewMemory() *Memory

func (*Memory) Clear

func (c *Memory) Clear() error

func (*Memory) Commit

func (c *Memory) Commit() error

func (*Memory) DeleteItem

func (c *Memory) DeleteItem(key string) error

func (*Memory) DeleteItems

func (c *Memory) DeleteItems(keys ...string) error

func (*Memory) GetItem

func (c *Memory) GetItem(key string) standards.CacheItem

func (*Memory) GetItems

func (c *Memory) GetItems(keys ...string) []standards.CacheItem

func (*Memory) HasItem

func (c *Memory) HasItem(key string) bool

func (*Memory) Save

func (c *Memory) Save(item standards.CacheItem) error

func (*Memory) SaveDeferred

func (c *Memory) SaveDeferred(item standards.CacheItem) error

type MemoryItem

type MemoryItem struct {
	KeepTTL bool
	// contains filtered or unexported fields
}

func NewMemoryItem

func NewMemoryItem(key string) *MemoryItem

func (*MemoryItem) ExpiresAfter

func (m *MemoryItem) ExpiresAfter(t time.Duration)

func (*MemoryItem) ExpiresAt

func (m *MemoryItem) ExpiresAt(expiration time.Time) (standards.CacheItem, error)

func (*MemoryItem) Get

func (m *MemoryItem) Get() any

func (*MemoryItem) GetKey

func (m *MemoryItem) GetKey() string

func (*MemoryItem) IsHit

func (m *MemoryItem) IsHit() bool

func (*MemoryItem) Set

func (m *MemoryItem) Set(value any, ttl time.Duration) (standards.CacheItem, error)

type Redis

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

func (*Redis) Clear

func (c *Redis) Clear() error

func (*Redis) Commit

func (c *Redis) Commit() error

func (*Redis) DeleteItem

func (c *Redis) DeleteItem(key string) error

func (*Redis) DeleteItems

func (c *Redis) DeleteItems(keys ...string) error

func (*Redis) GetItem

func (c *Redis) GetItem(key string) standards.CacheItem

func (*Redis) GetItems

func (c *Redis) GetItems(keys ...string) []standards.CacheItem

func (*Redis) HasItem

func (c *Redis) HasItem(key string) bool

func (*Redis) Save

func (c *Redis) Save(item standards.CacheItem) error

func (*Redis) SaveDeferred

func (c *Redis) SaveDeferred(item standards.CacheItem) error

type RedisItem

type RedisItem struct {
	KeepTTL bool
	// contains filtered or unexported fields
}

func NewRedisItem

func NewRedisItem(key string) *RedisItem

func (*RedisItem) ExpiresAfter

func (r *RedisItem) ExpiresAfter(t time.Duration)

func (*RedisItem) ExpiresAt

func (r *RedisItem) ExpiresAt(expiration time.Time) (standards.CacheItem, error)

func (*RedisItem) Get

func (r *RedisItem) Get() any

func (*RedisItem) GetKey

func (r *RedisItem) GetKey() string

func (*RedisItem) IsHit

func (r *RedisItem) IsHit() bool

func (*RedisItem) Set

func (r *RedisItem) Set(value any, ttl time.Duration) (standards.CacheItem, error)

type Storage added in v1.0.3

type Storage struct {
	Storages map[string]standards.Cache
}

func GetStorage added in v1.0.6

func GetStorage() *Storage

GetStorage get storage (for global usages)

func NewStorage added in v1.0.3

func NewStorage() *Storage

NewStorage create new instance of Storage

func (*Storage) Add added in v1.0.3

func (s *Storage) Add(name string, cache standards.Cache) (standards.Cache, error)

Add add cache instance to list

func (*Storage) AddFile added in v1.0.3

func (s *Storage) AddFile(name, dir string) (standards.Cache, error)

AddFile create File cache instance and add it to list

func (*Storage) AddMemory added in v1.0.3

func (s *Storage) AddMemory(name string) (standards.Cache, error)

AddMemory create Memory cache instance and add it to list

func (*Storage) AddRedis added in v1.0.3

func (s *Storage) AddRedis(name string, client *redisLib.Client) (standards.Cache, error)

AddRedis create Redis cache instance and add it to list

func (*Storage) Get added in v1.0.3

func (s *Storage) Get(name string) (cache standards.Cache, exists bool)

Get return cache instance

Jump to

Keyboard shortcuts

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