myredis

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: BSD-3-Clause Imports: 9 Imported by: 1

README

myredis 240527

Documentation

Index

Constants

View Source
const (
	OneSecond = 1
	OneMinute = 60 * OneSecond
	OneHour   = 60 * OneMinute
	OneDay    = OneHour * 24
)

Variables

View Source
var (
	ErrNotFound = fmt.Errorf("the key not found")
)
View Source
var ErrRecordNotFound = errors.New("record not found")

Functions

func StreamToObject

func StreamToObject[E any](b []byte) (ret E, err error)

Types

type Client

type Client interface {
	Set(key string, v any, ttl int) error //ttl==0 ,永不过期
	SetAsJson(key string, v any, ttl int) error
	AcquireLock(key string, ttl int) (bool, error)
	ReleaseLock(key string) error
	Get(key string, v any) error
	GetFromJson(key string, v any) error
	Del(key string) error
	Exist(key string) (bool, error)
	RemainingTTL(key string) (int64, error)
	Expire(key string, ttl int) error

	//增加Zset 标准封装
	// ZAdd 向有序集合添加一个或多个成员。
	// 如果成员已存在,则更新其 score。
	// ttl==0 表示不设置过期时间,否则写入后会给整个 key 设置过期时间。
	ZAdd(key string, ttl int, items ...ZItem) error
	// ZRem 从有序集合中删除一个或多个成员。
	// 不存在的成员会被忽略。
	ZRem(key string, members ...any) error

	// ZRemRangeByScore 删除指定 score 区间内的所有成员。
	// 常用于清理“过期时间作为 score”的数据。
	// min / max 可传数字,也可传 Redis 支持的区间表达式,如 "-inf"、"+inf"。
	ZRemRangeByScore(key, min, max any) error

	// ZCard 返回有序集合当前成员数量。
	ZCard(key string) (int64, error)

	// ZRange 按 score 升序返回指定下标范围内的成员。
	// start / stop 的语义与 Redis ZRANGE 一致,支持负数下标。
	ZRange(key string, start, stop int64) ([]string, error)

	// ZRangeByScore 按 score 区间返回成员列表,结果按 score 升序排列。
	// min / max 可传数字,也可传 Redis 支持的区间表达式,如 "-inf"、"+inf"。
	ZRangeByScore(key string, min, max any) ([]string, error)

	// ZScore 查询指定成员当前的 score。
	// 如果成员不存在,返回 ErrNotFound。
	ZScore(key string, member any) (float64, error)

	Conn() error
}

func NewClient

func NewClient(address string, password string, db int) Client

type DBListCache added in v1.1.2

type DBListCache[T any] struct {
	sync.Mutex // 新增
	// contains filtered or unexported fields
}

func NewDBListCache added in v1.1.2

func NewDBListCache[T any](c Client, key string, loadFn func() ([]T, int, error)) *DBListCache[T]

func (*DBListCache[T]) Find added in v1.1.2

func (r *DBListCache[T]) Find(cond func(T) bool) ([]T, error)

func (*DBListCache[T]) FindSorted added in v1.1.2

func (r *DBListCache[T]) FindSorted(
	cond func(T) bool,
	less func(a, b T) bool,
) ([]T, error)

func (*DBListCache[T]) First added in v1.1.2

func (r *DBListCache[T]) First(condition func(ret T) bool) (*T, error)

func (*DBListCache[T]) FirstSorted added in v1.1.2

func (r *DBListCache[T]) FirstSorted(
	cond func(T) bool,
	less func(a, b T) bool,
) (*T, error)

func (*DBListCache[T]) Invalidate added in v1.1.2

func (r *DBListCache[T]) Invalidate() error

func (*DBListCache[T]) List added in v1.1.2

func (r *DBListCache[T]) List() ([]T, error)

type DBMapCache added in v1.1.2

type DBMapCache[K comparable, V any] struct {
	sync.Mutex // 新增
	// contains filtered or unexported fields
}

func NewDBMapCache added in v1.1.2

func NewDBMapCache[K comparable, V any](c Client, key string, loadFn func() (map[K]V, int, error)) *DBMapCache[K, V]

func (*DBMapCache[K, V]) Find added in v1.1.3

func (r *DBMapCache[K, V]) Find(cond func(V) bool) ([]V, error)

func (*DBMapCache[K, V]) First added in v1.1.3

func (r *DBMapCache[K, V]) First(condition func(ret V) bool) (*V, error)

func (*DBMapCache[K, V]) Get added in v1.1.2

func (r *DBMapCache[K, V]) Get(key K) (*V, error)

func (*DBMapCache[K, V]) Invalidate added in v1.1.2

func (r *DBMapCache[K, V]) Invalidate() error

func (*DBMapCache[K, V]) Map added in v1.1.2

func (r *DBMapCache[K, V]) Map() (map[K]V, error)

type KVCache added in v1.0.8

type KVCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

KVCache 只负责通用缓存:Get / Set / Del

func NewKVCache added in v1.0.8

func NewKVCache[K comparable, V any](c Client, namespace string, ttlSec int) *KVCache[K, V]

func (*KVCache[K, V]) Del added in v1.0.8

func (c *KVCache[K, V]) Del(key K) error

func (*KVCache[K, V]) Expire added in v1.0.8

func (c *KVCache[K, V]) Expire(key K, exp ...int) error

func (*KVCache[K, V]) Get added in v1.0.8

func (c *KVCache[K, V]) Get(key K) (V, error)

func (*KVCache[K, V]) Set added in v1.0.8

func (c *KVCache[K, V]) Set(key K, value V) error

type ZItem added in v1.1.5

type ZItem struct {
	Score  float64
	Member any
}

Jump to

Keyboard shortcuts

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