cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package cache is a generated GoMock package.

Index

Constants

View Source
const (
	TTLRedisOneSecond          = int64(1)
	TTLRedisOneMinute          = 60 * TTLRedisOneSecond
	TTLRedisFiveMinutes        = 5 * TTLRedisOneMinute
	TTLRedisTenMinutes         = 10 * TTLRedisOneMinute
	TTLRedisFifteenMinutes     = 15 * TTLRedisOneMinute
	TTLRedisThirtyMinutes      = 30 * TTLRedisOneMinute
	TTLRedisSeventyFiveMinutes = 75 * TTLRedisOneMinute
	TTLRedisOneHour            = 60 * TTLRedisOneMinute
	TTLRedisHalfDay            = 12 * TTLRedisOneHour
	TTLRedisOneDay             = 24 * TTLRedisOneHour
	TTLRedisThreeDays          = 3 * TTLRedisOneDay
	TTLRedisOneWeek            = 7 * TTLRedisOneDay
	TTLRedisOneMonth           = 30 * TTLRedisOneDay
)

Time use for redis ttl which by default is in time.Second.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoRedisCluster

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

GoRedisCluster returns a redis cluster client using go-redis library.

func NewGoRedisCluster

func NewGoRedisCluster(config *RedisConfiguration) (*GoRedisCluster, error)

NewGoRedisCluster returns a redis cluster client.

func (*GoRedisCluster) Close

func (r *GoRedisCluster) Close()

Close closes the client, releasing any open resources.

func (*GoRedisCluster) Del

func (r *GoRedisCluster) Del(ctx context.Context, key ...interface{}) (int64, error)

Del deletes a key.

func (*GoRedisCluster) Exists

func (r *GoRedisCluster) Exists(ctx context.Context, key string) (bool, error)

Exists checks whether the key exists in redis.

func (*GoRedisCluster) Expire

func (r *GoRedisCluster) Expire(
	ctx context.Context,
	key string,
	seconds int64,
) (bool, error)

Expire sets the ttl of a key to specified value in seconds.

func (*GoRedisCluster) Get

func (r *GoRedisCluster) Get(ctx context.Context, key string) ([]byte, error)

Get gets the value from redis in []byte form.

func (*GoRedisCluster) HExists

func (r *GoRedisCluster) HExists(ctx context.Context, key, field string) (bool, error)

HExists determines if a hash field exists.

func (*GoRedisCluster) HGet

func (r *GoRedisCluster) HGet(ctx context.Context, key, field string) ([]byte, error)

HGet gets the value of a hash field.

func (*GoRedisCluster) HSet

func (r *GoRedisCluster) HSet(ctx context.Context, key, field, value string) (bool, error)

HSet sets the string value of a hash field.

func (*GoRedisCluster) SetEX

func (r *GoRedisCluster) SetEX(
	ctx context.Context,
	key string,
	seconds int64,
	value string,
) error

SetEX sets the value to a key with timeout in seconds.

func (*GoRedisCluster) SetNX

func (r *GoRedisCluster) SetNX(
	ctx context.Context,
	key string,
	seconds int64,
	value string,
) (bool, error)

SetNX sets a value to a key with specified timeouts. SetNX returns false if the key exists.

func (*GoRedisCluster) TTL

func (r *GoRedisCluster) TTL(ctx context.Context, key string) (int64, error)

TTL gets the time to live of a key / expiry time.

type MockRedisClientItf

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

MockRedisClientItf is a mock of RedisClientItf interface.

func NewMockRedisClientItf

func NewMockRedisClientItf(ctrl *gomock.Controller) *MockRedisClientItf

NewMockRedisClientItf creates a new mock instance.

func (*MockRedisClientItf) Close

func (m *MockRedisClientItf) Close()

Close mocks base method.

func (*MockRedisClientItf) Del

func (m *MockRedisClientItf) Del(ctx context.Context, key ...interface{}) (int64, error)

Del mocks base method.

func (*MockRedisClientItf) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockRedisClientItf) Exists

func (m *MockRedisClientItf) Exists(ctx context.Context, key string) (bool, error)

Exists mocks base method.

func (*MockRedisClientItf) Expire

func (m *MockRedisClientItf) Expire(ctx context.Context, key string, seconds int64) (bool, error)

Expire mocks base method.

func (*MockRedisClientItf) Get

func (m *MockRedisClientItf) Get(ctx context.Context, key string) ([]byte, error)

Get mocks base method.

func (*MockRedisClientItf) HExists

func (m *MockRedisClientItf) HExists(ctx context.Context, key, field string) (bool, error)

HExists mocks base method.

func (*MockRedisClientItf) HGet

func (m *MockRedisClientItf) HGet(ctx context.Context, key, field string) ([]byte, error)

HGet mocks base method.

func (*MockRedisClientItf) HSet

func (m *MockRedisClientItf) HSet(ctx context.Context, key, field, value string) (bool, error)

HSet mocks base method.

func (*MockRedisClientItf) SetEX

func (m *MockRedisClientItf) SetEX(ctx context.Context, key string, seconds int64, value string) error

SetEX mocks base method.

func (*MockRedisClientItf) SetNX

func (m *MockRedisClientItf) SetNX(ctx context.Context, key string, seconds int64, value string) (bool, error)

SetNX mocks base method.

func (*MockRedisClientItf) TTL

func (m *MockRedisClientItf) TTL(ctx context.Context, key string) (int64, error)

TTL mocks base method.

type MockRedisClientItfMockRecorder

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

MockRedisClientItfMockRecorder is the mock recorder for MockRedisClientItf.

func (*MockRedisClientItfMockRecorder) Close

Close indicates an expected call of Close.

func (*MockRedisClientItfMockRecorder) Del

func (mr *MockRedisClientItfMockRecorder) Del(ctx interface{}, key ...interface{}) *gomock.Call

Del indicates an expected call of Del.

func (*MockRedisClientItfMockRecorder) Exists

func (mr *MockRedisClientItfMockRecorder) Exists(ctx, key interface{}) *gomock.Call

Exists indicates an expected call of Exists.

func (*MockRedisClientItfMockRecorder) Expire

func (mr *MockRedisClientItfMockRecorder) Expire(ctx, key, seconds interface{}) *gomock.Call

Expire indicates an expected call of Expire.

func (*MockRedisClientItfMockRecorder) Get

func (mr *MockRedisClientItfMockRecorder) Get(ctx, key interface{}) *gomock.Call

Get indicates an expected call of Get.

func (*MockRedisClientItfMockRecorder) HExists

func (mr *MockRedisClientItfMockRecorder) HExists(ctx, key, field interface{}) *gomock.Call

HExists indicates an expected call of HExists.

func (*MockRedisClientItfMockRecorder) HGet

func (mr *MockRedisClientItfMockRecorder) HGet(ctx, key, field interface{}) *gomock.Call

HGet indicates an expected call of HGet.

func (*MockRedisClientItfMockRecorder) HSet

func (mr *MockRedisClientItfMockRecorder) HSet(ctx, key, field, value interface{}) *gomock.Call

HSet indicates an expected call of HSet.

func (*MockRedisClientItfMockRecorder) SetEX

func (mr *MockRedisClientItfMockRecorder) SetEX(ctx, key, seconds, value interface{}) *gomock.Call

SetEX indicates an expected call of SetEX.

func (*MockRedisClientItfMockRecorder) SetNX

func (mr *MockRedisClientItfMockRecorder) SetNX(ctx, key, seconds, value interface{}) *gomock.Call

SetNX indicates an expected call of SetNX.

func (*MockRedisClientItfMockRecorder) TTL

func (mr *MockRedisClientItfMockRecorder) TTL(ctx, key interface{}) *gomock.Call

TTL indicates an expected call of TTL.

type MockRistrettoClientItf

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

MockRistrettoClientItf is a mock of RistrettoClientItf interface.

func NewMockRistrettoClientItf

func NewMockRistrettoClientItf(ctrl *gomock.Controller) *MockRistrettoClientItf

NewMockRistrettoClientItf creates a new mock instance.

func (*MockRistrettoClientItf) Clear

func (m *MockRistrettoClientItf) Clear(ctx context.Context)

Clear mocks base method.

func (*MockRistrettoClientItf) Close

func (m *MockRistrettoClientItf) Close()

Close mocks base method.

func (*MockRistrettoClientItf) Del

func (m *MockRistrettoClientItf) Del(ctx context.Context, key string)

Del mocks base method.

func (*MockRistrettoClientItf) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockRistrettoClientItf) Get

func (m *MockRistrettoClientItf) Get(ctx context.Context, key string) (interface{}, bool)

Get mocks base method.

func (*MockRistrettoClientItf) Set

func (m *MockRistrettoClientItf) Set(ctx context.Context, key string, value interface{}) bool

Set mocks base method.

func (*MockRistrettoClientItf) SetEX

func (m *MockRistrettoClientItf) SetEX(ctx context.Context, key string, value interface{}, TTL time.Duration) bool

SetEX mocks base method.

type MockRistrettoClientItfMockRecorder

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

MockRistrettoClientItfMockRecorder is the mock recorder for MockRistrettoClientItf.

func (*MockRistrettoClientItfMockRecorder) Clear

func (mr *MockRistrettoClientItfMockRecorder) Clear(ctx interface{}) *gomock.Call

Clear indicates an expected call of Clear.

func (*MockRistrettoClientItfMockRecorder) Close

Close indicates an expected call of Close.

func (*MockRistrettoClientItfMockRecorder) Del

func (mr *MockRistrettoClientItfMockRecorder) Del(ctx, key interface{}) *gomock.Call

Del indicates an expected call of Del.

func (*MockRistrettoClientItfMockRecorder) Get

func (mr *MockRistrettoClientItfMockRecorder) Get(ctx, key interface{}) *gomock.Call

Get indicates an expected call of Get.

func (*MockRistrettoClientItfMockRecorder) Set

func (mr *MockRistrettoClientItfMockRecorder) Set(ctx, key, value interface{}) *gomock.Call

Set indicates an expected call of Set.

func (*MockRistrettoClientItfMockRecorder) SetEX

func (mr *MockRistrettoClientItfMockRecorder) SetEX(ctx, key, value, TTL interface{}) *gomock.Call

SetEX indicates an expected call of SetEX.

type Redigo

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

Redigo returns a redis client using redigo library.

func NewRedigo

func NewRedigo(config *RedisConfiguration) (*Redigo, error)

NewRedigo return a redis client.

func (*Redigo) Close

func (r *Redigo) Close()

Close closes the client, releasing any open resources.

func (*Redigo) Decr

func (r *Redigo) Decr(_ context.Context, key string) (int64, error)

Decr decrements the integer value of a key by 1.

func (*Redigo) Del

func (r *Redigo) Del(_ context.Context, key ...interface{}) (int64, error)

Del deletes a key.

func (*Redigo) Exists

func (r *Redigo) Exists(_ context.Context, key string) (bool, error)

Exists checks whether the key exists in redis.

func (*Redigo) Expire

func (r *Redigo) Expire(_ context.Context, key string, seconds int64) (bool, error)

Expire sets the ttl of a key to specified value in seconds.

func (*Redigo) ExpireAt

func (r *Redigo) ExpireAt(_ context.Context, key string, timestamp int64) (bool, error)

ExpireAt sets the ttl of a key to a certain timestamp.

func (*Redigo) Get

func (r *Redigo) Get(_ context.Context, key string) ([]byte, error)

Get gets the value from redis in []byte form.

func (*Redigo) HDel

func (r *Redigo) HDel(_ context.Context, key string, fields ...string) (int64, error)

HDel deletes a hash field.

func (*Redigo) HExists

func (r *Redigo) HExists(_ context.Context, key, field string) (bool, error)

HExists determines if a hash field exists.

func (*Redigo) HGet

func (r *Redigo) HGet(_ context.Context, key, field string) ([]byte, error)

HGet gets the value of a hash field.

func (*Redigo) HGetAll

func (r *Redigo) HGetAll(
	_ context.Context,
	key string,
) (map[string]string, error)

HGetAll gets all the fields and values in a hash.

func (*Redigo) HKeys

func (r *Redigo) HKeys(_ context.Context, key string) ([]string, error)

HKeys gets all the fields in a hash.

func (*Redigo) HMGet

func (r *Redigo) HMGet(_ context.Context, key string, fields ...string) ([][]byte, error)

HMGet gets a value of multiple fields from hash key.

func (*Redigo) HSet

func (r *Redigo) HSet(
	_ context.Context,
	key, field, value string,
) (bool, error)

HSet sets the string value of a hash field.

func (*Redigo) Incr

func (r *Redigo) Incr(_ context.Context, key string) (int64, error)

Incr increments the integer value of a key by 1.

func (*Redigo) IncrByEx

func (r *Redigo) IncrByEx(
	_ context.Context,
	key string,
	by int64,
	expires int64,
) (int64, error)

IncrByEx increments redis key by adding expired.

func (*Redigo) LRange

func (r *Redigo) LRange(
	_ context.Context,
	key string,
	start, stop int64,
) ([][]byte, error)

LRange gets array range that we set using LPush between index Start and Stop.

func (*Redigo) LTrim

func (r *Redigo) LTrim(_ context.Context, key string, start, stop int64) error

Trim array value that we set using LPush between index start and stop.

func (*Redigo) Publish

func (r *Redigo) Publish(_ context.Context, topic, message string) (int, error)

Publish sends message to a topic and returns numbers of subscriber that receives the message.

func (*Redigo) SAdd

func (r *Redigo) SAdd(_ context.Context, key string, value ...string) (bool, error)

SAdd add the specified members to the set stored at key. It returns false if key and value combination exists.

func (*Redigo) SetEX

func (r *Redigo) SetEX(_ context.Context, key string, seconds int64, value string) error

SetEX sets the value to a key with timeout in seconds.

func (*Redigo) SetNX

func (r *Redigo) SetNX(
	_ context.Context,
	key string,
	seconds int64,
	value string,
) (bool, error)

SetNX sets a value to a key with specified timeouts. SetNX returns false if the key exists.

func (*Redigo) SimpleSet

func (r *Redigo) SimpleSet(_ context.Context, key, value string) error

SimpleSet sets value to key in redis without any additional options. Key doesn't have a TTL.

func (*Redigo) TTL

func (r *Redigo) TTL(_ context.Context, key string) (int64, error)

TTL gets the time to live of a key / expiry time.

type RedisClientItf

type RedisClientItf interface {
	// Get gets the value from redis in []byte form.
	Get(ctx context.Context, key string) ([]byte, error)
	// SetEX sets the value to a key with timeout in seconds.
	SetEX(ctx context.Context, key string, seconds int64, value string) error
	// SetNX sets a value to a key with specified timeouts.
	// SetNX returns false if the key exists.
	SetNX(ctx context.Context, key string, seconds int64, value string) (bool, error)
	// Exists checks whether the key exists in redis.
	Exists(ctx context.Context, key string) (bool, error)
	// Expire sets the TTL of a key to specified value in seconds.
	Expire(ctx context.Context, key string, seconds int64) (bool, error)
	// TTL gets the time to live of a key / expiry time.
	TTL(ctx context.Context, key string) (int64, error)
	// HGet gets the value of a hash field.
	HGet(ctx context.Context, key, field string) ([]byte, error)
	// HExists determines if a hash field exists.
	HExists(ctx context.Context, key, field string) (bool, error)
	// HSet sets the string value of a hash field.
	HSet(ctx context.Context, key, field, value string) (bool, error)
	// Del deletes a key.
	Del(ctx context.Context, key ...interface{}) (int64, error)
	// Close closes the client, releasing any open resources.
	Close()
}

RedisClientItf is a client to interact with Redis.

type RedisConfiguration

type RedisConfiguration struct {
	// IdleConnectionLimit describes maximum idle connection before it closed.
	IdleConnectionLimit int64
	// OpenConnectionLimit describes maximum open connection can be opened.
	OpenConnectionLimit int64
	// WaitOpenConnection describes if operation should wait
	// for open connection to do operation.
	WaitOpenConnection bool
	// Addresses list of available addresses for cluster connection.
	Addresses []string
	// Enables read-only commands on slave nodes.
	ReadOnly bool
	// Allows routing read-only commands to the closest master or slave node.
	// It automatically enables ReadOnly.
	RouteByLatency bool
	// Allows routing read-only commands to the random master or slave node.
	// It automatically enables ReadOnly.
	RouteRandomly bool
	// MaxRetries describes the maximum retry if command fails.
	// Too much retry will prevent other command from being executed.
	MaxRetries int64
	// DialTimeout is in seconds.
	DialTimeout int64
	// MaxConnAge is in seconds.
	MaxConnAge int64
	// IdleTimeout is in seconds.
	IdleTimeout int64
	// MinIdleConnection describes the minimum open idle connection to be keep at all time.
	MinIdleConnection int64
}

RedisConfiguration configuration.

type RedisMigrator

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

RedisMigrator is a redis client to migrate from one instance to another. All the create commands will be executed to the new instance. All the update and delete commands will be executed on both instances. While the read commands will read from the new instance first, and if the result is not found, it will attempt to read from the old one.

func NewRedisMigrator

func NewRedisMigrator(origin, destination RedisClientItf) (*RedisMigrator, error)

NewRedisMigrator return a redis migrator client.

func (*RedisMigrator) Close

func (r *RedisMigrator) Close()

Close closes the client, releasing any open resources.

func (*RedisMigrator) Del

func (r *RedisMigrator) Del(ctx context.Context, key ...interface{}) (int64, error)

Del deletes a key.

func (*RedisMigrator) Exists

func (r *RedisMigrator) Exists(ctx context.Context, key string) (bool, error)

Exists checks whether the key exists in redis.

func (*RedisMigrator) Expire

func (r *RedisMigrator) Expire(ctx context.Context, key string, seconds int64) (bool, error)

Expire sets the ttl of a key to specified value in seconds.

func (*RedisMigrator) Get

func (r *RedisMigrator) Get(ctx context.Context, key string) ([]byte, error)

Get gets the value from redis in []byte form.

func (*RedisMigrator) HExists

func (r *RedisMigrator) HExists(ctx context.Context, key, field string) (bool, error)

HExists determines if a hash field exists.

func (*RedisMigrator) HGet

func (r *RedisMigrator) HGet(ctx context.Context, key, field string) ([]byte, error)

HGet gets the value of a hash field.

func (*RedisMigrator) HSet

func (r *RedisMigrator) HSet(ctx context.Context, key, field, value string) (bool, error)

HSet sets the string value of a hash field.

func (*RedisMigrator) SetEX

func (r *RedisMigrator) SetEX(
	ctx context.Context,
	key string,
	seconds int64,
	value string,
) error

SetEX sets the value to a key with timeout in seconds.

func (*RedisMigrator) SetNX

func (r *RedisMigrator) SetNX(
	ctx context.Context,
	key string,
	seconds int64,
	value string,
) (bool, error)

SetNX sets a value to a key with specified timeouts. SetNX returns false if the key exists.

func (*RedisMigrator) TTL

func (r *RedisMigrator) TTL(ctx context.Context, key string) (int64, error)

TTL gets the time to live of a key / expiry time.

type Ristretto

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

Ristretto returns a in-process storage client using ristretto library.

func NewRistretto

func NewRistretto(config *RistrettoConfiguration) (*Ristretto, error)

NewRistretto return a redis client.

func (*Ristretto) Clear

func (r *Ristretto) Clear(_ context.Context)

Clear empties the cache store and zeroes all policy counters.

func (*Ristretto) Close

func (r *Ristretto) Close()

Close stops all goroutines and closes all channels.

func (*Ristretto) Del

func (r *Ristretto) Del(_ context.Context, key string)

Del deletes the key-value item from the cache if it exists.

func (*Ristretto) Get

func (r *Ristretto) Get(_ context.Context, key string) (res interface{}, exists bool)

Get returns the value (if any) and a boolean representing whether the value was found or not.

func (*Ristretto) Set

func (r *Ristretto) Set(_ context.Context, key string, value interface{}) bool

Set attempts to add the key-value item to the cache. If it returns false, then the Set was dropped and the key-value item isn't added to the cache. If it returns true, there's still a chance it could be dropped by the policy if its determined that the key-value item isn't worth keeping, but otherwise the item will be added and other items will be evicted in order to make room.

func (*Ristretto) SetEX

func (r *Ristretto) SetEX(
	_ context.Context,
	key string,
	value interface{},
	ttl time.Duration,
) bool

SetEX works like Set but adds a key-value pair to the cache that will expire after the specified TTL (time to live) has passed. A zero value means the value never expires, which is identical to calling Set. A negative value is a no-op and the value is discarded.

type RistrettoClientItf

type RistrettoClientItf interface {
	// Get returns the value (if any) and a boolean representing whether the value was found or not.
	Get(ctx context.Context, key string) (res interface{}, exists bool)
	// Set attempts to add the key-value item to the cache.
	// If it returns false, then the Set was dropped and the key-value item isn't added to the cache.
	// If it returns true, there's still a chance it could be dropped by the policy
	// if its determined that the key-value item isn't worth keeping,
	// but otherwise the item will be added and other items will be evicted in order to make room.
	Set(ctx context.Context, key string, value interface{}) bool
	// SetEX works like Set but adds a key-value pair to the cache
	// that will expire after the specified TTL (time to live) has passed.
	// A zero value means the value never expires, which is identical to calling Set.
	// A negative value is a no-op and the value is discarded.
	SetEX(ctx context.Context, key string, value interface{}, TTL time.Duration) bool
	// Del deletes the key-value item from the cache if it exists.
	Del(ctx context.Context, key string)
	// Clear empties the cache store and zeroes all policy counters.
	Clear(ctx context.Context)
	// Close stops all goroutines and closes all channels.
	Close()
}

RistrettoClientItf is a in-process or local cache storage client.

type RistrettoConfiguration

type RistrettoConfiguration struct {
	// NumCounters determines the number of counters (keys) to keep that hold
	// access frequency information. It's generally a good idea to have more
	// counters than the max cache capacity, as this will improve eviction
	// accuracy and subsequent hit ratios.
	//
	// For example, if you expect your cache to hold 1,000,000 items when full,
	// NumCounters should be 10,000,000 (10x). Each counter takes up 4 bits, so
	// keeping 10,000,000 counters would require 5MB of memory.
	NumCounters int64
	// MaxCost can be considered as the cache capacity, in whatever units you
	// choose to use.
	//
	// For example, if you want the cache to have a max capacity of 100MB, you
	// would set MaxCost to 100,000,000 and pass an item's number of bytes as
	// the `cost` parameter for calls to Set. If new items are accepted, the
	// eviction process will take care of making room for the new item and not
	// overflowing the MaxCost value.
	MaxCost int64
	// BufferItems determines the size of Get buffers.
	//
	// Unless you have a rare use case, using `64` as the BufferItems value
	// results in good performance.
	BufferItems int64
	// Metrics determines whether cache statistics are kept during the cache's
	// lifetime. There *is* some overhead to keeping statistics, so you should
	// only set this flag to true when testing or throughput performance isn't a
	// major factor.
	Metrics bool
}

RistrettoConfiguration configuration.

Jump to

Keyboard shortcuts

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