cache

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 5 Imported by: 0

README

Cache

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

使用方法

import (
	"log"
	"time"

	"github.com/474420502/cache"
	"github.com/474420502/gcurl"
)

func main() {
	// 实例
	cache := cache.New(
		time.Millisecond*50, // 每 50 millisecond 更新一次
		func(share interface{}) interface{} { // 更新的方法
			resp, err := gcurl.Execute(`curl "http://httpbin.org/uuid"`)
			if err != nil {
				log.Println(err)
			}
			return string(resp.Content()) // 返回更新的时间. 这个数据会在不更新期间缓存
		})
	defer cache.Destroy()

	old := cache.Value() // 旧值
	for i := 0; i < 2; i++ {
		time.Sleep(time.Millisecond * 70) // 70 Millisecond 查询一次值
		n := cache.Value()
		if old == n {
			t.Error("value should be updated", n, old)
		}
		old = cache.Value()
		if old != n { // 如果在 70 Millisecond 内值不一样就报错
			t.Error("value should be updated", n, old)
		}
	}

	// 初始化后只需要获取
	log.Println(cache.Value()) // 该值为 cache.UpdateMehtod 的值. 可用于缓存 一些远程更新数据 但是不需要频繁更新.
}

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 interface {
	SetShare(share interface{})
	SetOnUpdateError(func(err interface{}))
	Destroy()
	Value() interface{}
	GetUpdate() time.Time
}

func New

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

New 创建一个Cache对象

func NewBlockCache added in v1.1.0

func NewBlockCache(interval time.Duration, u UpdateMehtod) Cache

NewBlockCache 创建一个Cache对象

type CacheBlock added in v1.1.0

type CacheBlock struct {
	// contains filtered or unexported fields
}

CacheBlock 缓存 对比 默认 CacheInterval 的. 该方法有阻塞效果. 就是更新过程会阻塞

func (*CacheBlock) Destroy added in v1.1.0

func (cache *CacheBlock) Destroy()

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

func (*CacheBlock) GetUpdate added in v1.1.0

func (cache *CacheBlock) GetUpdate() time.Time

Value 获取缓存的值

func (*CacheBlock) SetOnUpdateError added in v1.1.0

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

SetOnUpdateError 默认false

func (*CacheBlock) SetShare added in v1.1.0

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

func (*CacheBlock) Value added in v1.1.0

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

Value 获取缓存的值

type CacheInterval added in v1.1.0

type CacheInterval struct {
	// contains filtered or unexported fields
}

CacheInterval 缓存

func (*CacheInterval) Destroy added in v1.1.0

func (cache *CacheInterval) Destroy()

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

func (*CacheInterval) GetUpdate added in v1.1.0

func (cache *CacheInterval) GetUpdate() time.Time

Value 获取缓存的值

func (*CacheInterval) SetOnUpdateError added in v1.1.0

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

SetOnUpdateError 默认false

func (*CacheInterval) SetShare added in v1.1.0

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

func (*CacheInterval) Value added in v1.1.0

func (cache *CacheInterval) 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