gocache

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

gocache

版本
  • ≥ go 1.18
介绍

本地缓存实现: gob 其他: redis

使用说明
引入包
import (
	"gitee.com/yelhjt/gocache/config"
	"gitee.com/yelhjt/gocache"
)

  1. 本地缓存

	// 本地缓存配置
	local_config := config.LocalConfig{
		// 本地缓存的文件
		Path: "./runtime/cache",
		// 前缀
		Config: &config.Config{
			Prefix: "sss",
		},
	}
	// 根据配置选择驱动
	local := gocache.NewCache(local_config)
	// 添加缓存
	local.Add("测试1", "测试1", 20*time.Second)
	local.Set("测试2", "测试2", 20*time.Second)
	// 获缓存
	local.Get("测试1", "我是默认值")
	// 是否存在
	local.Has("测试1")
	local.Has("测试3")
	// 删除
	// local.Delete("测试21")
	// 清空
	local.Flush()
  1. Redis
	// redis 缓存配置
	redis_config := config.RedisConfig{
		Port:     "6379",
		DBnum:    2,
		Addr:     "127.0.0.1",
		Passwrod: "123123",
	}
	// 根据配置选择驱动
	redis := gocache.NewCache(redis_config)
	// 添加缓存
	redis.Add("测试11", "测试1", 1120*time.Second)
	redis.Set("测试21", "测试2", 2110*time.Second)
	// 获缓存
	redis.Get("测试11", "我是默认值")
	// 是否存在
	redis.Has("测试11")
	redis.Has("测试31")
	// 删除
	// redis.Delete("测试21")
	// 清空
	redis.Flush()
实现的接口
	// 设置值 参数1:key  , 参数2:value , 参数2:有效时间 ; 如果存在则直接覆盖
	Set(string, interface{}, time.Duration) bool

	// 设置值 参数1:key  , 参数2:value , 参数2:有效时间 ;如果存在则抛出异常
	Add(string, interface{}, time.Duration) error

	// 修改值 参数1:key  , 参数2:value , 参数2:有效时间 ; 如果存在则修改,不存在则抛出异常
	Replace(string, interface{}, time.Duration) error

	// 获取值 参数:key
	Get(string, interface{}) (interface{}, error)

	// 清空所有缓存
	Flush()

	// 删除某个缓存
	Delete(string) error

	// 判断键值是否存在
	Has(string) bool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCache

func NewCache[conf Config](c conf) (cache core.CacheInterface)

*

  • 实例化
  • 根据配置选择驱动

Types

type Config

type Config interface {
	config.LocalConfig | config.RedisConfig
}

unit 类型

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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