cache

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

缓存操作工具

缓存工具概述

gotool 提供了基于 redis 的缓存操作工具,可操作缓存类型包含 : string、 list 类型2种类型;

缓存配置

缓存类型

请打开(不存在则创建 ./config.ini),添加或修改配置

[cache]
# 支持 redis 和 map
Type=redis
Prefix=gotool_
Redis 相关配置

请打开(不存在则创建 ./config.ini),添加或修改如下配置

[RedisCache]
Host=127.0.0.1
Port=6379
Password=
DefaultDB=0
Prefix=gotool_

Map 缓存设置

[MapCache]
# 缓存垃圾回收间隔时间,单位 秒
GCIntervalTime=900

工具加载

import (
	"github.com/cnlesscode/gotool/cache"
)

Get() any

函数功能 : 获取缓存不存在则创建缓存 参数说明 :

  1. keyName string 缓存件名称
  2. expiration int 有效时间 单位 秒, -1 代表永久有效
  3. cacheFunc CacheFunc 获取缓存数据的函数
  4. cacheParameters ... 动态缓存参数 返回格式 : [] any 演示代码 :
package main

import (
	"fmt"
	"time"

	"github.com/cnlesscode/gotool/cache"
)

func getTest(args ...any) any {
	// 此处可以执行耗时操作
	// 如连接数据库 获取数据 等 ....
	fmt.Printf("args: %v %v\n", args[0], args[1])
	return args
}

func main() {
	res := cache.Get("test1", 100, getTest, 1, 2, 3, "hi")
	fmt.Printf("1 获得缓存 : %v\n", res)
	time.Sleep(time.Second * 2)
	res = cache.Get("test1", 100, getTest, 1, 2, 3, "hi")
	fmt.Printf("2 获得缓存 : %v\n", res)
	time.Sleep(time.Second * 10)
	res = cache.Get("test1", 100, getTest, 1, 2, 3, "hi")
	fmt.Printf("3 获得缓存 : %v\n", res)
}

remove()

函数功能 : 删除缓存 [ 模糊查找模式 ] 参数说明 : keyName string 缓存件名称 返回格式 : 无返回值 演示代码 :

package main

import (
	"github.com/cnlesscode/gotool/cache"
)

func main() {
	cache.Remove("test")
}

Clear()

函数功能 : 清空缓存 返回格式 : 无返回值 演示代码 :

package main

import (
	"github.com/cnlesscode/gotool/cache"
)

func main() {
	cache.Clear()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CacheNamePrefix string = ""

缓存变量前缀

View Source
var CacheType string = ""
View Source
var MapCacher sync.Map
View Source
var MapCacherExpiration sync.Map

Functions

func Clear

func Clear()

清空缓存

func Get

func Get(keyName string, expiration int, cacheFunc CacheFunc, cacheParameters ...any) any

获取一个缓存 不存在则创建

func InitCacheName

func InitCacheName(keyName string, cacheParameters ...any) string

组合缓存名称

func MapCacheGet

func MapCacheGet(keyName string, expiration int, cacheFunc CacheFunc, cacheParameters ...any) any

获取变量 不存在则自动设置

func MapCacheRemove

func MapCacheRemove(name string)

删除 模糊搜索方式

func MapCacheSet

func MapCacheSet(keyName string, expiration int, data any)

设置缓存

func MapCacherClear

func MapCacherClear()

清空

func Remove

func Remove(name string)

删除缓存 模糊搜索模式

Types

type CacheFunc

type CacheFunc func(args ...any) any

缓存数据读取函数

type RedisCacherStruct

type RedisCacherStruct struct {
	RedisClient *redis.Client
}
var RedisCacher RedisCacherStruct

func (*RedisCacherStruct) Clear

func (cacher *RedisCacherStruct) Clear()

清空缓存

func (*RedisCacherStruct) Get

func (cacher *RedisCacherStruct) Get(keyName string, expiration int, cacheFunc CacheFunc, cacheParameters ...any) any

获取变量 不存在则自动设置

func (*RedisCacherStruct) Remove

func (cacher *RedisCacherStruct) Remove(name string)

删除 模糊搜索方式

Jump to

Keyboard shortcuts

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