jcache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: MIT Imports: 6 Imported by: 2

README

JCache 一个简单的缓存集成方案

项目由来

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

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

所以一般情况下,当Redis坏掉了,可以再降级使用其他缓存方案,我们利用服务器的内存开发缓存系统是最优的选择.

如果每次开发一个项目都需要写一套缓存系统出来,那得多累人,所以,当前项目就是将缓存操作集成起来,进行了封装,减少重复开发,以免浪费时间。

架构

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

进度

-[x] Redis缓存支持 -[ ] 其他分布式缓存支持 -[ ] 本机内存支持

问题

  1. Q:怎么保持数据的一致性? A:各个服务之间可以通过服务发现方案建立之间的联系,如果有数据更新,其他订阅的服务实例也能同时接收到数据,并进行更新
  2. Q:如果有数据需要更新,是否所有在线的实例都需要更新? A:现行方案是这样的,所以可能会导致内存爆棚的问题,这个可能需要更加详细的方案设计。
  3. Q:这个方案是独立的服务还是嵌入到代码里面的? A:我们这个方案是嵌入到代码里面的,如果应用我们这个项目并使用,那么该应用也会成为一个缓存服务实例。
  4. Q:我们的这个方案是最牛逼的吗? A:不是的
  5. Q:这个库适合所有项目吗? A:并不,该库只是进行了简单的封装,其中逻辑较为简单,可能不适合一些较为大型的项目使用。如果需要使用,可以进行fork并执行对应的业务修改。

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
)
View Source
var BG_CTX = context.Background()

Functions

func CheckAndGet

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

CheckAndGet 检测并获取数据

func CheckAndScan

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

CheckAndScan 获取数据

func Del

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

Del 删除键

func Exists

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

Exists 判断某个Key是否存在

func Expire

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

Expire 设置某个Key的TTL时长

func Get

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

Get 获取数据

func GetAndScan

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

GetAndScan 获取并扫描

func HDel

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

HDel 删除hash数据

func HGet

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

HGet 获取Hash表指定字段的值

func HGetAndScan

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

HGetAndScan 获取Hash表指定字段的值

func HKeys

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

HKeys 获取Hash表的所有键

func HKeysAndScan

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

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

func HLen

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

HLen 获取Hash表的所有键个数

func HMGet

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

HMGet 获取Hash表指定字段的值

func HMGetAndScan

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

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

func HSet

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

HSet 写入hash数据

func HVals

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

HVals 获取Hash表的所有值

func HValsScan

func HValsScan(ctx context.Context, dst any, key string) error

HVals 获取Hash表的所有值

func Init

func Init(cfg *Config) error

Init 初始化缓存

func MGet

func MGet(ctx context.Context, keys ...string) ([]interface{}, error)

MGet 获取多个Keys的值

func MGetAndScan

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

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

func Pop

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

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

func PopAndScan

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

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

func Push

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

Push 推送数据

func RandomExpirationDuration

func RandomExpirationDuration() time.Duration

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

func Rang

func Rang(ctx context.Context, key string, limit int64) ([]string, error)

Rang 获取列表内的范围数据

func RangAndScan

func RangAndScan(ctx context.Context, dst any, key string, limit int64) error

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

func Redis

func Redis() redis.UniversalClient

func Set

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

Set 设置数据

func SetNX

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

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

Types

type Config

type Config struct {
	Redis *RedisConfig `json:"redis" yaml:"redis"`
}

type RedisConfig

type RedisConfig struct {
	// Mode 模式
	// 支持:single,sentinel,cluster
	Mode       string   `yaml:"mode"`
	MasterName string   `yaml:"master_name"`
	Addrs      []string `yaml:"addrs"`
	Database   string   `yaml:"database"`
	Username   string   `yaml:"username"`
	Password   string   `yaml:"password"`
}

Jump to

Keyboard shortcuts

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