Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 4 Imported by: 0

README

Cache

用于定时或者条件更新数据. 例如: 1.爬虫请求页面时间或者某个值 2.请求某表的内容信息, 减少频繁操作

使用方法

cache := New(func() interface{} {
    resp, _ := gcurl.Execute(`curl "http://httpbin.org/uuid"`)
    return string(resp.Content())
}) // 创建 cache 并添加请求方法. 默认为异步更新. 当到触发条件的时间. 返回值还是上次值. 更新完后再次调用才是最新值. cache.SetBlock 可以用这个函数设置为阻塞. 更新会让更新方法彻底完成后. 返回值.
cache.SetUpdateInterval(time.Millisecond * 50) // 设置更新时间
// cache.Update() 主动更新.  
log.Println(cache.Value()) // { "uuid": "fbbea7fc-3a71-4e65-bc18-6642eddf83a7" }
time.Sleep(time.Millisecond * 50)
log.Println(cache.Value()) // { "uuid": "125c0d4d-e598-4b37-b0da-c928d44b703d" }

Documentation

Overview

Package cache 缓存 异步更新

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	LastUpdate time.Time
	// contains filtered or unexported fields
}

Cache 缓存

func New

func New(interval time.Duration, u UpdateMehtod) *Cache

New 创建一个Cache对象

func NewWithBlock added in v1.0.0

func NewWithBlock(interval time.Duration, u UpdateMehtod) *Cache

New 创建一个Cache对象, 异步更新必须调用Destroy 销毁

func NewWithEvery added in v1.0.0

func NewWithEvery(u UpdateMehtod) *Cache

NewWithEvery 创建一个Cache对象. 必须每次都触发UpdateMethod可以自定以条件

func (*Cache) Destroy added in v1.0.0

func (cache *Cache) Destroy()

Destroy 异步更新必须调用Destroy, 销毁对象

func (*Cache) SetOnUpdateError added in v1.0.0

func (cache *Cache) SetOnUpdateError(errFunc func(err interface{}))

SetOnUpdateError 默认false

func (*Cache) SetShare added in v1.0.0

func (cache *Cache) SetShare(share interface{})

func (*Cache) Update

func (cache *Cache) Update()

Update 主动更新

func (*Cache) Value

func (cache *Cache) Value() interface{}

Value 获取缓存的值

type UpdateMehtod

type UpdateMehtod func(share interface{}) interface{}

UpdateMehtod 更新方法

Jump to

Keyboard shortcuts

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