jcache

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 5 Imported by: 2

README

JCache 使用Golang开发的多级缓存集成方案

项目由来

我们在开发项目中,少不了需要用到缓存,甚至是分布式缓存。我们用的最多的就是Redis,它是一个非常优秀的分布式缓存数据库。

但是如果在生产环境中,Redis挂了,导致某些业务无法再进行,甚至缓存雪崩,导致所有业务都无法进行。

所以一般情况下,我们是尽量不让redis这个分布式的缓存挂掉的,所以我们很多时候就会把自己服务实例上的内存拿来当缓存用,因为它快速、方便、基本无延迟。所以,我们每开发一个项目就会写上一堆缓存组件,很是麻烦。

现在,一个支持本机内存缓存并支持Redis等分布式缓存服务的集成方案产生了。我们开箱即用,非常方便。

架构

* 本项目方案采用Redis优先,当Redis无法获取到数据时降级成直接使用本地内存.
* golang是一个牛逼的语言,使用map可以写出一大堆很优秀的本地缓存框架

进度

  • Redis支持
  • 本机内存支持
    • 单机模式支持
    • 分布式模式支持

案例

import (
    "time"
	
    "github.com/jerbe/jcache"
    "github.com/jerbe/jcache/driver"
)

func main(){
	// driver.NewMemory() 实例化一个以内存作为缓存的驱动
	
	// 支持所有操作的客户端,包括 String,Hash,List 
	client := jcache.NewClient(driver.NewMemory())
	client.Set(context.Background(),"hello","world", time.Hour)
	client.Get(context.Background(),"hello")
	client.MGet(context.Background(),"hello","hi")
	...
		
	// 仅支持 String 操作的客户端 
	stringClient := jcache.NewStringClien(driver.NewMemory()); 
	stringClient.Set(context.Background(),"hello","world", time.Hour)
	stringClient.Get(context.Background(),"hello")
	stringClient.MGet(context.Background(),"hello","hi")
	...
	
	// 仅支持 Hash 操作的客户端
	hashClient := jcache.NewHashClient(driver.NewMemory()); 
	hashClient.HSet(context.Background(),"hello","world","boom")
	hashClient.HGet(context.Background(),"hello","world")
	...
	
	// 仅支持 List 操作的客户端 
	listClient := jcache.NewListClient(driver.NewMemory());
	listClient.Push(context.Background(),"hello","world")
	listClient.Pop(context.Background(),"hello")
	listClient.Shift(context.Background(),"hello")
}

Documentation

Index

Constants

View Source
const (
	// KeepTTL 保持原先的过期时间(TTL)
	KeepTTL = -1

	// DefaultEmptySetNXDuration 默认空对象设置过期时效
	DefaultEmptySetNXDuration = time.Second * 10

	// DefaultExpirationDuration 默认缓存过期时效
	DefaultExpirationDuration = time.Hour
)

Variables

View Source
var (
	ErrEmpty         = errors.New("empty")
	ErrNoCacheClient = errors.New("no cache client init")
	ErrNoRecord      = errors.New("no record")
	ErrCanceled      = context.Canceled
)

Functions

func RandomExpirationDuration

func RandomExpirationDuration() time.Duration

RandomExpirationDuration 以 DefaultExpirationDuration 为基础,返回一个 DefaultExpirationDuration ± 30m 内的时间长度

Types

type Client added in v1.1.0

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

func NewClient added in v1.1.0

func NewClient(drivers ...driver.Cache) *Client

func (*Client) CheckAndGet added in v1.1.0

func (cli *Client) CheckAndGet(ctx context.Context, key string) (string, error)

CheckAndGet 检测并获取数据

func (*Client) CheckAndScan added in v1.1.0

func (cli *Client) CheckAndScan(ctx context.Context, dst any, key string) error

CheckAndScan 获取数据

func (*Client) Del added in v1.1.0

func (cli *Client) Del(ctx context.Context, keys ...string) error

Del 删除键

func (*Client) Exists added in v1.1.0

func (cli *Client) Exists(ctx context.Context, keys ...string) (int64, error)

Exists 判断某个Key是否存在

func (*Client) Expire added in v1.1.0

func (cli *Client) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置某个Key的TTL时长

func (*Client) ExpireAt added in v1.1.0

func (cli *Client) ExpireAt(ctx context.Context, key string, at time.Time) error

ExpireAt 设置某个key在指定时间内到期

func (*Client) Get added in v1.1.0

func (cli *Client) Get(ctx context.Context, key string) (string, error)

Get 获取数据

func (*Client) GetAndScan added in v1.1.0

func (cli *Client) GetAndScan(ctx context.Context, dst any, key string) error

GetAndScan 获取并扫描

func (*Client) HDel added in v1.1.0

func (cli *Client) HDel(ctx context.Context, key string, fields ...string) error

HDel 删除hash数据

func (*Client) HGet added in v1.1.0

func (cli *Client) HGet(ctx context.Context, key, field string) (string, error)

HGet 获取Hash表指定字段的值

func (*Client) HGetAndScan added in v1.1.0

func (cli *Client) HGetAndScan(ctx context.Context, dst any, key, field string) error

HGetAndScan 获取Hash表指定字段的值

func (*Client) HKeys added in v1.1.0

func (cli *Client) HKeys(ctx context.Context, key string) ([]string, error)

HKeys 获取Hash表的所有键

func (*Client) HKeysAndScan added in v1.1.0

func (cli *Client) HKeysAndScan(ctx context.Context, dst any, key string) error

HKeysAndScan 获取Hash表的所有键并扫描到dst中

func (*Client) HLen added in v1.1.0

func (cli *Client) HLen(ctx context.Context, key string) (int64, error)

HLen 获取Hash表的所有键个数

func (*Client) HMGet added in v1.1.0

func (cli *Client) HMGet(ctx context.Context, key string, fields ...string) ([]any, error)

HMGet 获取Hash表指定字段的值

func (*Client) HMGetAndScan added in v1.1.0

func (cli *Client) HMGetAndScan(ctx context.Context, dst any, key string, fields ...string) error

HMGetAndScan 获取Hash表指定字段的值并扫描进入到dst中

func (*Client) HSet added in v1.1.0

func (cli *Client) HSet(ctx context.Context, key string, values ...any) error

HSet 写入hash数据

func (*Client) HVals added in v1.1.0

func (cli *Client) HVals(ctx context.Context, key string) ([]string, error)

HVals 获取Hash表的所有值

func (*Client) HValsAndScan added in v1.1.0

func (cli *Client) HValsAndScan(ctx context.Context, dst any, key string) error

HValsAndScan 获取Hash表的所有值并扫如dst中

func (*Client) MGet added in v1.1.0

func (cli *Client) MGet(ctx context.Context, keys ...string) ([]any, error)

MGet 获取多个Keys的值

func (*Client) MGetAndScan added in v1.1.0

func (cli *Client) MGetAndScan(ctx context.Context, dst any, keys ...string) error

MGetAndScan 获取多个Keys的值并扫描进dst中

func (*Client) Pop added in v1.1.0

func (cli *Client) Pop(ctx context.Context, key string) (string, error)

Pop 取出列表内的第一个数据

func (*Client) PopAndScan added in v1.1.0

func (cli *Client) PopAndScan(ctx context.Context, dst any, key string) error

PopAndScan 通过扫描方式取出列表内的第一个数据

func (*Client) Push added in v1.1.0

func (cli *Client) Push(ctx context.Context, key string, data ...any) error

Push 推送数据

func (*Client) Rang added in v1.1.0

func (cli *Client) Rang(ctx context.Context, key string, start, stop int64) ([]string, error)

Rang 获取列表内的范围数据

func (*Client) RangAndScan added in v1.1.0

func (cli *Client) RangAndScan(ctx context.Context, dst any, key string, start, stop int64) error

RangAndScan 通过扫描方式获取列表内的范围内数据

func (*Client) Set added in v1.1.0

func (cli *Client) Set(ctx context.Context, key string, data any, expiration time.Duration) error

Set 设置数据

func (*Client) SetNX added in v1.1.0

func (cli *Client) SetNX(ctx context.Context, key string, data any, expiration time.Duration) error

SetNX 设置数据,如果key不存在的话

func (*Client) Trim added in v1.1.0

func (cli *Client) Trim(ctx context.Context, key string, start, stop int64) error

Trim 获取列表内的范围数据

type HashClient added in v1.1.0

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

func NewHashClient added in v1.1.0

func NewHashClient(drivers ...driver.Hash) *HashClient

func (*HashClient) Del added in v1.1.0

func (cli *HashClient) Del(ctx context.Context, keys ...string) error

Del 删除键

func (*HashClient) Exists added in v1.1.0

func (cli *HashClient) Exists(ctx context.Context, keys ...string) (int64, error)

Exists 判断某个Key是否存在

func (*HashClient) Expire added in v1.1.0

func (cli *HashClient) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置某个Key的TTL时长

func (*HashClient) ExpireAt added in v1.1.0

func (cli *HashClient) ExpireAt(ctx context.Context, key string, at time.Time) error

ExpireAt 设置某个key在指定时间内到期

func (*HashClient) HDel added in v1.1.0

func (cli *HashClient) HDel(ctx context.Context, key string, fields ...string) error

HDel 删除hash数据

func (*HashClient) HGet added in v1.1.0

func (cli *HashClient) HGet(ctx context.Context, key, field string) (string, error)

HGet 获取Hash表指定字段的值

func (*HashClient) HGetAndScan added in v1.1.0

func (cli *HashClient) HGetAndScan(ctx context.Context, dst any, key, field string) error

HGetAndScan 获取Hash表指定字段的值

func (*HashClient) HKeys added in v1.1.0

func (cli *HashClient) HKeys(ctx context.Context, key string) ([]string, error)

HKeys 获取Hash表的所有键

func (*HashClient) HKeysAndScan added in v1.1.0

func (cli *HashClient) HKeysAndScan(ctx context.Context, dst any, key string) error

HKeysAndScan 获取Hash表的所有键并扫描到dst中

func (*HashClient) HLen added in v1.1.0

func (cli *HashClient) HLen(ctx context.Context, key string) (int64, error)

HLen 获取Hash表的所有键个数

func (*HashClient) HMGet added in v1.1.0

func (cli *HashClient) HMGet(ctx context.Context, key string, fields ...string) ([]any, error)

HMGet 获取Hash表指定字段的值

func (*HashClient) HMGetAndScan added in v1.1.0

func (cli *HashClient) HMGetAndScan(ctx context.Context, dst any, key string, fields ...string) error

HMGetAndScan 获取Hash表指定字段的值并扫描进入到dst中

func (*HashClient) HSet added in v1.1.0

func (cli *HashClient) HSet(ctx context.Context, key string, values ...any) error

HSet 写入hash数据

func (*HashClient) HVals added in v1.1.0

func (cli *HashClient) HVals(ctx context.Context, key string) ([]string, error)

HVals 获取Hash表的所有值

func (*HashClient) HValsAndScan added in v1.1.0

func (cli *HashClient) HValsAndScan(ctx context.Context, dst any, key string) error

HValsAndScan 获取Hash表的所有值并扫如dst中

type ListClient added in v1.1.0

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

func NewListClient added in v1.1.0

func NewListClient(drivers ...driver.List) *ListClient

func (*ListClient) Del added in v1.1.0

func (cli *ListClient) Del(ctx context.Context, keys ...string) error

Del 删除键

func (*ListClient) Exists added in v1.1.0

func (cli *ListClient) Exists(ctx context.Context, keys ...string) (int64, error)

Exists 判断某个Key是否存在

func (*ListClient) Expire added in v1.1.0

func (cli *ListClient) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置某个Key的TTL时长

func (*ListClient) ExpireAt added in v1.1.0

func (cli *ListClient) ExpireAt(ctx context.Context, key string, at time.Time) error

ExpireAt 设置某个key在指定时间内到期

func (*ListClient) Pop added in v1.1.0

func (cli *ListClient) Pop(ctx context.Context, key string) (string, error)

Pop 取出列表内的第一个数据

func (*ListClient) PopAndScan added in v1.1.0

func (cli *ListClient) PopAndScan(ctx context.Context, dst any, key string) error

PopAndScan 通过扫描方式取出列表内的第一个数据

func (*ListClient) Push added in v1.1.0

func (cli *ListClient) Push(ctx context.Context, key string, data ...any) error

Push 推送数据

func (*ListClient) Rang added in v1.1.0

func (cli *ListClient) Rang(ctx context.Context, key string, start, stop int64) ([]string, error)

Rang 获取列表内的范围数据

func (*ListClient) RangAndScan added in v1.1.0

func (cli *ListClient) RangAndScan(ctx context.Context, dst any, key string, start, stop int64) error

RangAndScan 通过扫描方式获取列表内的范围内数据

func (*ListClient) Trim added in v1.1.0

func (cli *ListClient) Trim(ctx context.Context, key string, start, stop int64) error

Trim 获取列表内的范围数据

type StringClient added in v1.1.0

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

func NewStringClient added in v1.1.0

func NewStringClient(drivers ...driver.String) *StringClient

func (*StringClient) CheckAndGet added in v1.1.0

func (cli *StringClient) CheckAndGet(ctx context.Context, key string) (string, error)

CheckAndGet 检测并获取数据

func (*StringClient) CheckAndScan added in v1.1.0

func (cli *StringClient) CheckAndScan(ctx context.Context, dst any, key string) error

CheckAndScan 获取数据

func (*StringClient) Del added in v1.1.0

func (cli *StringClient) Del(ctx context.Context, keys ...string) error

Del 删除键

func (*StringClient) Exists added in v1.1.0

func (cli *StringClient) Exists(ctx context.Context, keys ...string) (int64, error)

Exists 判断某个Key是否存在

func (*StringClient) Expire added in v1.1.0

func (cli *StringClient) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire 设置某个Key的TTL时长

func (*StringClient) ExpireAt added in v1.1.0

func (cli *StringClient) ExpireAt(ctx context.Context, key string, at time.Time) error

ExpireAt 设置某个key在指定时间内到期

func (*StringClient) Get added in v1.1.0

func (cli *StringClient) Get(ctx context.Context, key string) (string, error)

Get 获取数据

func (*StringClient) GetAndScan added in v1.1.0

func (cli *StringClient) GetAndScan(ctx context.Context, dst any, key string) error

GetAndScan 获取并扫描

func (*StringClient) MGet added in v1.1.0

func (cli *StringClient) MGet(ctx context.Context, keys ...string) ([]any, error)

MGet 获取多个Keys的值

func (*StringClient) MGetAndScan added in v1.1.0

func (cli *StringClient) MGetAndScan(ctx context.Context, dst any, keys ...string) error

MGetAndScan 获取多个Keys的值并扫描进dst中

func (*StringClient) Set added in v1.1.0

func (cli *StringClient) Set(ctx context.Context, key string, data any, expiration time.Duration) error

Set 设置数据

func (*StringClient) SetNX added in v1.1.0

func (cli *StringClient) SetNX(ctx context.Context, key string, data any, expiration time.Duration) error

SetNX 设置数据,如果key不存在的话

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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