cache

package
v0.0.0-...-b6e34af Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IdTypeDevice = "device"
	IdTypeUser   = "user"
	// expiration of the device check in time - one week
	CheckInTimeExpiration = time.Duration(time.Hour * 24 * 7)
)

Variables

View Source
var (
	ErrNoPositiveInteger = errors.New("must be a positive integer")
	ErrNegativeInteger   = errors.New("cannot be a negative integer")

	ErrTooManyRequests = errors.New("too many requests")
)

Functions

func LimitsEmpty

func LimitsEmpty(l *ratelimits.ApiLimits) bool

TODO: move to go-lib-micro/ratelimits

Types

type Cache

type Cache interface {
	// Throttle applies desired api limits and retrieves a cached token.
	// These ops are bundled because the implementation will pipeline them for a single network
	// roundtrip for max performance.
	// Returns:
	// - the token (if any)
	// - potentially ErrTooManyRequests (other errors: internal)
	Throttle(
		ctx context.Context,
		rawToken string,
		l ratelimits.ApiLimits,
		tid,
		id,
		idtype,
		url,
		action string,
	) (string, error)

	// CacheToken caches the token under designated key, with expiration
	CacheToken(ctx context.Context, tid, id, idtype, token string, expireSec time.Duration) error

	// DeleteToken deletes the token for 'id'
	DeleteToken(ctx context.Context, tid, id, idtype string) error

	// GetLimits fetches limits for 'id'
	GetLimits(ctx context.Context, tid, id, idtype string) (*ratelimits.ApiLimits, error)

	// CacheLimits saves limits for 'id'
	CacheLimits(ctx context.Context, l ratelimits.ApiLimits, tid, id, idtype string) error

	// CacheCheckInTime caches the last device check in time
	CacheCheckInTime(ctx context.Context, t *time.Time, tid, id string) error

	// GetCheckInTime gets the last device check in time from cache
	GetCheckInTime(ctx context.Context, tid, id string) (*time.Time, error)

	// GetCheckInTimes gets the last device check in time from cache
	// for each device with id from the list of ids
	GetCheckInTimes(ctx context.Context, tid string, ids []string) ([]*time.Time, error)

	// SuspendTenant increment tenant key version
	// tenant key is used in all cache keys, this way, when we increment the key version,
	// all the keys are no longer accessible - in other words, be incrementing tenant key version
	// we invalidate all tenant keys
	SuspendTenant(ctx context.Context, tid string) error
}

type RedisCache

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

func NewRedisCache

func NewRedisCache(
	ctx context.Context,
	connectionString string,
	prefix string,
	limitsExpireSec int,
) (*RedisCache, error)

func (*RedisCache) CacheCheckInTime

func (rl *RedisCache) CacheCheckInTime(
	ctx context.Context,
	t *time.Time,
	tid,
	id string,
) error

func (*RedisCache) CacheLimits

func (rl *RedisCache) CacheLimits(
	ctx context.Context,
	l ratelimits.ApiLimits,
	tid,
	id,
	idtype string,
) error

func (*RedisCache) CacheToken

func (rl *RedisCache) CacheToken(
	ctx context.Context,
	tid,
	id,
	idtype,
	token string,
	expire time.Duration,
) error

func (*RedisCache) DeleteToken

func (rl *RedisCache) DeleteToken(ctx context.Context, tid, id, idtype string) error

func (*RedisCache) GetCheckInTime

func (rl *RedisCache) GetCheckInTime(
	ctx context.Context,
	tid,
	id string,
) (*time.Time, error)

func (*RedisCache) GetCheckInTimes

func (rl *RedisCache) GetCheckInTimes(
	ctx context.Context,
	tid string,
	ids []string,
) ([]*time.Time, error)

func (*RedisCache) GetLimits

func (rl *RedisCache) GetLimits(
	ctx context.Context,
	tid,
	id,
	idtype string,
) (*ratelimits.ApiLimits, error)

func (*RedisCache) KeyBurst

func (rl *RedisCache) KeyBurst(
	tid, id, idtype, url, action, intvlNum string, version int) string

func (*RedisCache) KeyCheckInTime

func (rl *RedisCache) KeyCheckInTime(tid, id, idtype string, version int) string

func (*RedisCache) KeyLimits

func (rl *RedisCache) KeyLimits(tid, id, idtype string, version int) string

func (*RedisCache) KeyQuota

func (rl *RedisCache) KeyQuota(tid, id, idtype, intvlNum string, version int) string

func (*RedisCache) KeyTenantVersion

func (rl *RedisCache) KeyTenantVersion(tid string) string

func (*RedisCache) KeyToken

func (rl *RedisCache) KeyToken(tid, id, idtype string, version int) string

func (*RedisCache) SuspendTenant

func (rl *RedisCache) SuspendTenant(
	ctx context.Context,
	tid string,
) error

func (*RedisCache) Throttle

func (rl *RedisCache) Throttle(
	ctx context.Context,
	rawToken string,
	l ratelimits.ApiLimits,
	tid,
	id,
	idtype,
	url,
	action string,
) (string, error)

func (*RedisCache) WithClock

func (rl *RedisCache) WithClock(c utils.Clock) *RedisCache

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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