cache

package module
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: MPL-2.0 Imports: 6 Imported by: 7

README

缓存引擎

缓存引擎定义,通过缓存引擎获取缓存客户端并进行数据缓存操作。系统根据业务类型划分了几个特定的缓存节点类型,每个类型的缓存节点可以分别指定自己的缓存实现方式,通过配置中心的配置示例如下:

{
     "Node" : "provider",
     "Node" : "provider"
}

其中NodeType对应缓存节点类型{@link CacheNode}的枚举值(要大写),其值对应缓存的实现,有如下选项:

* redis:基于单Redis实例的实现;
* redis-sentinel:基于Redis实例主从模式的高可用集群实现;
* mem:基于内存实现的缓存部分接口,以供测试使用.

配置中心的配置路径为:/system/base/cache/provider

缓存Redis单实例

基于Redis单实例的缓存实现(单机),配置中心或构造方法中参数的配置格式如下:

{
    "host" : "缓存服务器主机地址,必须",
    "port" : "缓存服务器端口号",
    "sasl" : "是否开启安全认证,true|false,可选,默认是没有",
    "password" : "开启安全认证后的登录密码,sasl如果指定为true则必须"
 }

快速使用

注意:

  1. 一定在导入缓存具体实现如redis
  2. 注意缓存接口与缓存实现的导入顺序

示例:

    _ "github.com/aluka-7/cache-redis"
    "github.com/aluka-7/cache"
获取缓存实体
prov:=cache.Engine().OptProvider()
判断缓存中是否存在指定的key
prov.Exists(key string) bool
根据给定的key从分布式缓存中读取数据并返回,如果不存在或已过期则返回Null。
prov.String(key string) string
使用给定的key从缓存中查询数据,如果查询不到则使用给定的数据提供器来查询数据,然后将数据存入缓存中再返回。
prov.GetByProvider(key string, provider DataProvider) string
使用指定的key将对象存入分布式缓存中,并使用缓存的默认过期设置,注意,存入的对象必须是可序列化的。
prov.Set(key, value string)
使用指定的key将对象存入分部式缓存中,并指定过期时间,注意,存入的对象必须是可序列化的
prov.SetExpires(key, value string, expires time.Duration) bool
从缓存中删除指定key的缓存数据。
prov.Delete(key string)
批量删除缓存中的key。
prov.BatchDelete(keys ...string)
将指定key的map数据的某个字段设置为给定的值。
prov.HSet(key, field, value string)
获取指定key的map数据某个字段的值,如果不存在则返回Null
prov.HGet(key, field string) string
获取指定key的map对象,如果不存在则返回Null
prov.HGetAll(key string) map[string]string
将指定key的map数据中的某个字段删除。
prov.HDelete(key string, fields ...string)
判断缓存中指定key的map是否存在指定的字段,如果key或字段不存在则返回false。
prov.HExists(key, field string) bool
对指定的key结果集执行指定的脚本并返回最终脚本执行的结果。
prov.Val(script string, keys []string, args ...interface{})
根据指定的key进行自增1
prov.AtomicIncrement(key string)int64
根据指定的key进行自减1
prov.AtomicDecrement(key string)int64
关闭客户端
prov.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, provider Driver)

Types

type Driver

type Driver interface {
	// New 初始化缓存接口
	New(ctx context.Context, cfg map[string]string) Provider
}

Driver 缓存驱动程序定义

func Read

func Read(key string) (Driver, bool)

type Provider

type Provider interface {
	Client() interface{}

	LRange(ctx context.Context, key string, start, stop int64) []string

	LIndex(ctx context.Context, key string, index int64) string

	RPush(ctx context.Context, key string, value ...interface{}) bool

	LPush(ctx context.Context, key string, value ...interface{}) bool

	LLen(ctx context.Context, key string) int64

	LPop(ctx context.Context, key string) string

	RPop(ctx context.Context, key string) string

	SAdd(ctx context.Context, key string, members ...interface{}) bool

	SCard(ctx context.Context, key string) int64

	SetNX(ctx context.Context, key, value string, expires time.Duration) bool

	SMembers(ctx context.Context, key string) []string

	// Exists
	// @description 判断缓存中是否存在指定的key
	// @param key
	// @return bool
	Exists(ctx context.Context, key string) bool

	// String
	// @description 根据给定的key从分布式缓存中读取数据并返回,如果不存在或已过期则返回Null。
	// @param key 缓存唯一键
	// @return string
	String(ctx context.Context, key string) string

	// Set
	// @description 使用指定的key将对象存入分布式缓存中,并使用缓存的默认过期设置,注意,存入的对象必须是可序列化的。
	// @param key   缓存唯一键
	// @param value 对应的值
	// @return bool
	Set(ctx context.Context, key, value string) bool

	// SetExpires
	// @description 使用指定的key将对象存入分部式缓存中,并指定过期时间,注意,存入的对象必须是可序列化的
	// @param key     缓存唯一键
	// @param value   对应的值
	// @param expires 过期时间,单位秒
	// @return bool
	SetExpires(ctx context.Context, key, value string, expires time.Duration) bool

	// Delete
	// @description 从缓存中删除指定key的缓存数据。
	// @param key
	// @return bool
	Delete(ctx context.Context, key string) bool

	// BatchDelete
	// @description 批量删除缓存中的key
	// @param keys
	BatchDelete(ctx context.Context, keys ...string) bool

	// HSet
	// @description 将指定key的map数据的某个字段设置为给定的值
	// @param key   map数据的键
	// @param field map的字段名称
	// @param value 要设置的字段值
	HSet(ctx context.Context, key, field, value string) bool

	// HGet
	// @description 获取指定key的map数据某个字段的值,如果不存在则返回Null
	// @param key   map数据的键
	// @param field map的字段名称
	// @return
	HGet(ctx context.Context, key, field string) string

	// HGetAll
	// @description 获取指定key的map对象,如果不存在则返回Null
	// @param key map数据的键
	// @return map[string]string
	HGetAll(ctx context.Context, key string) map[string]string

	// HDelete
	// 将指定key的map数据中的某个字段删除。
	// @param key   map数据的键
	// @param field map中的key名称
	HDelete(ctx context.Context, key string, fields ...string) bool

	// HExists
	// 判断缓存中指定key的map是否存在指定的字段,如果key或字段不存在则返回false。
	// @param key
	// @param field
	// @return bool
	HExists(ctx context.Context, key, field string) bool

	// Val
	// 对指定的key结果集执行指定的脚本并返回最终脚本执行的结果。
	// @param script 脚本
	// @param key    要操作的缓存key
	// @param args   脚本的参数列表
	// @return
	Val(ctx context.Context, script string, keys []string, args ...interface{}) string

	// Incr
	// 对指定的key执行自增操作
	// @param key
	// @return bool
	Incr(ctx context.Context, key string) bool

	// IncrExpires
	// 对指定的key执行自增操作,并设置过期时间
	// @param key
	// @param expires 过期时间,单位秒
	// @return bool
	IncrExpires(ctx context.Context, key string, expires time.Duration) bool

	// IncrByExpires
	// 对指定的key执行自增操作,并设置过期时间
	// @param key
	// @param value
	// @param expires 过期时间,单位秒
	// @return bool
	IncrByExpires(ctx context.Context, key string, value int64, expires time.Duration) bool

	// Operate
	// 通过直接调用缓存客户端进行缓存操作,该操作适用于高级操作,如果执行失败会返回Null。
	// @param operator
	// @return err
	Operate(ctx context.Context, cmd interface{}) error

	// Close
	// 关闭客户端
	Close()
}

Provider 分布式的缓存操作接口。

func Engine

func Engine(systemId string, cfg configuration.Configuration) (prov Provider)

缓存节点服务器的类型,不同节点类型缓存的数据及其目的有所差异,业务系统要根据实际情况进行选择处理。

Jump to

Keyboard shortcuts

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