cache

package
v1.24.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 18 Imported by: 141

Documentation

Index

Constants

View Source
const (

	// SlowCacheThreshold marks cache tests as slow
	// set to 30ms per discussion: https://github.com/go-gitea/gitea/issues/33190
	// TODO: Replace with metrics histogram
	SlowCacheThreshold = 30 * time.Millisecond
)

Variables

This section is empty.

Functions

func GetInt64

func GetInt64(key string, getFunc func() (int64, error)) (int64, error)

GetInt64 returns key value from cache with callback when no key exists in cache

func GetString added in v1.12.0

func GetString(key string, getFunc func() (string, error)) (string, error)

GetString returns the key value from cache with callback when no key exists in cache

func GetWithContextCache added in v1.19.0

func GetWithContextCache[T, K any](ctx context.Context, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error)

GetWithContextCache returns the cache value of the given key in the given context. FIXME: in some cases, the "context cache" should not be used, because it has uncontrollable behaviors For example, these calls: * GetWithContextCache(TargetID) -> OtherCodeCreateModel(TargetID) -> GetWithContextCache(TargetID) Will cause the second call is not able to get the correct created target. UNLESS it is certain that the target won't be changed during the request, DO NOT use it.

func GetWithEphemeralCache

func GetWithEphemeralCache[T, K any](ctx context.Context, c *EphemeralCache, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error)

func Init added in v1.22.0

func Init() error

Init start cache service

func Remove

func Remove(key string)

Remove key from cache

func Test added in v1.23.0

func Test() (time.Duration, error)

Test performs delete, put and get operations on a predefined key returns

func WithCacheContext added in v1.19.0

func WithCacheContext(ctx context.Context) context.Context

Types

type EphemeralCache

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

EphemeralCache is a cache that can be used to store data in a request level context This is useful for caching data that is expensive to calculate and is likely to be used multiple times in a request.

func GetContextCache

func GetContextCache(ctx context.Context) *EphemeralCache

func NewEphemeralCache

func NewEphemeralCache(checkLifeTime ...time.Duration) *EphemeralCache

func (*EphemeralCache) Delete

func (cc *EphemeralCache) Delete(tp, key any)

func (*EphemeralCache) Get

func (cc *EphemeralCache) Get(tp, key any) (any, bool)

func (*EphemeralCache) Put

func (cc *EphemeralCache) Put(tp, key, value any)

type GetJSONError added in v1.22.0

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

func (*GetJSONError) ToError added in v1.22.0

func (e *GetJSONError) ToError() error

type MemoryItem added in v1.15.0

type MemoryItem struct {
	Val     any
	Created int64
	Timeout int64
}

MemoryItem represents a memory cache item.

type RedisCacher added in v1.13.0

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

RedisCacher represents a redis cache adapter implementation.

func (*RedisCacher) Decr added in v1.13.0

func (c *RedisCacher) Decr(key string) error

Decr decreases cached int-type value by given key as a counter.

func (*RedisCacher) Delete added in v1.13.0

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

Delete deletes cached value by given key.

func (*RedisCacher) Flush added in v1.13.0

func (c *RedisCacher) Flush() error

Flush deletes all cached data.

func (*RedisCacher) Get added in v1.13.0

func (c *RedisCacher) Get(key string) any

Get gets cached value by given key.

func (*RedisCacher) Incr added in v1.13.0

func (c *RedisCacher) Incr(key string) error

Incr increases cached int-type value by given key as a counter.

func (*RedisCacher) IsExist added in v1.13.0

func (c *RedisCacher) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*RedisCacher) Ping added in v1.17.0

func (c *RedisCacher) Ping() error

Ping tests if the cache is alive.

func (*RedisCacher) Put added in v1.13.0

func (c *RedisCacher) Put(key string, val any, expire int64) error

Put puts value (string type) into cache with key and expire time. If expired is 0, it lives forever.

func (*RedisCacher) StartAndGC added in v1.13.0

func (c *RedisCacher) StartAndGC(opts cache.Options) error

StartAndGC starts GC routine based on config string settings. AdapterConfig: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180,hset_name=MacaronCache,prefix=cache:

type StringCache added in v1.22.0

type StringCache interface {
	Ping() error

	Get(key string) (string, bool)
	Put(key, value string, ttl int64) error
	Delete(key string) error
	IsExist(key string) bool

	PutJSON(key string, v any, ttl int64) error
	GetJSON(key string, ptr any) (exist bool, err *GetJSONError)

	ChiCache() chi_cache.Cache
}

func GetCache added in v1.14.0

func GetCache() StringCache

GetCache returns the currently configured cache

func NewStringCache added in v1.22.0

func NewStringCache(cacheConfig setting.Cache) (StringCache, error)

type TwoQueueCache added in v1.15.0

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

TwoQueueCache represents a LRU 2Q cache adapter implementation

func (*TwoQueueCache) Decr added in v1.15.0

func (c *TwoQueueCache) Decr(key string) error

Decr decreases cached int-type value by given key as a counter.

func (*TwoQueueCache) Delete added in v1.15.0

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

Delete deletes cached value by given key.

func (*TwoQueueCache) Flush added in v1.15.0

func (c *TwoQueueCache) Flush() error

Flush deletes all cached data.

func (*TwoQueueCache) Get added in v1.15.0

func (c *TwoQueueCache) Get(key string) any

Get gets cached value by given key.

func (*TwoQueueCache) Incr added in v1.15.0

func (c *TwoQueueCache) Incr(key string) error

Incr increases cached int-type value by given key as a counter.

func (*TwoQueueCache) IsExist added in v1.15.0

func (c *TwoQueueCache) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*TwoQueueCache) Ping added in v1.17.0

func (c *TwoQueueCache) Ping() error

Ping tests if the cache is alive.

func (*TwoQueueCache) Put added in v1.15.0

func (c *TwoQueueCache) Put(key string, val any, timeout int64) error

Put puts value into cache with key and expire time.

func (*TwoQueueCache) StartAndGC added in v1.15.0

func (c *TwoQueueCache) StartAndGC(opts mc.Options) error

StartAndGC starts GC routine based on config string settings.

type TwoQueueCacheConfig added in v1.15.0

type TwoQueueCacheConfig struct {
	Size        int     `ini:"SIZE" json:"size"`
	RecentRatio float64 `ini:"RECENT_RATIO" json:"recent_ratio"`
	GhostRatio  float64 `ini:"GHOST_RATIO" json:"ghost_ratio"`
}

TwoQueueCacheConfig describes the configuration for TwoQueueCache

Jump to

Keyboard shortcuts

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