apcu

package
v1.1.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

apcu

提供了进程内缓存组件。

Install

go get github.com/go-spring/spring-base@v1.1.0-rc2 

Import

import "github.com/go-spring/spring-base/apcu"

Example

var i int
load, err := apcu.Load(ctx, "int", &i)
apcu.Store(ctx, "int", 3)
load, err := apcu.Load(ctx, "int", &i)
apcu.Delete(ctx, "int")

Documentation

Overview

Package apcu 提供了进程内缓存组件。

Index

Constants

View Source
const EmptyValue = "::empty::"

EmptyValue 流量录制时表示空值。

Variables

This section is empty.

Functions

func Delete

func Delete(ctx context.Context, key string)

Delete 删除 key 对应的缓存内容。

func Load

func Load(ctx context.Context, key string, out interface{}) (ok bool, err error)

Load 获取 key 对应的缓存值,注意 out 的类型必须和 Store 的时候存入的类 型一致,否则 Load 会失败。但是如果 Store 的时候存入的内容是一个字符串, 那么 out 可以是该字符串 JSON 反序列化之后的类型。

func Range

func Range(f func(key, value interface{}) bool)

Range 遍历缓存的内容。

func Store

func Store(ctx context.Context, key string, val interface{}, opts ...StoreOption)

Store 保存 key 及其对应的 val,支持对 key 设置 ttl (过期时间)。另外, 这里的 val 可以是任何值,因此要求 Load 的时候必须保证返回值和这里的 val 是相同类型的,否则 Load 会失败。 但是这里有一个例外情况,考虑到很多场景下,用户需要缓存一个由字符串反序列 化后的对象,所以该库提供了一个功能,就是用户可以 Store 一个字符串,然后 Load 的时候按照指定类型返回。

Types

type StoreArg

type StoreArg struct {
	TTL time.Duration
}

type StoreOption

type StoreOption func(arg *StoreArg)

func TTL

func TTL(ttl time.Duration) StoreOption

TTL 设置 key 的过期时间。

Jump to

Keyboard shortcuts

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