cache

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

缓存空间功能, 将特定请求的响应缓存分类整理好 便于后续其他请求复用响应

Index

Constants

View Source
const (

	// MaxCacheSize 缓存最大大小 (Byte)
	//
	// 这里的大小指的是响应体大小, 实际占用大小可能略大一些
	MaxCacheSize int64 = 100 * 1024 * 1024

	// MaxCacheNum 最多缓存多少个请求信息
	MaxCacheNum = 8092

	// HeaderKeyExpired 缓存过期响应头, 用于覆盖默认的缓存过期时间
	HeaderKeyExpired = "Expired"
)
View Source
const (

	// HeaderKeySpace 缓存空间 key
	HeaderKeySpace = "Space"

	// HeaderKeySpaceKey 缓存空间内部 key
	HeaderKeySpaceKey = "Space-Key"
)

Variables

View Source
var CacheKeyIgnoreParams = map[string]struct{}{

	"StartTimeTicks": {}, "X-Playback-Session-Id": {},

	"PlaySessionId": {},

	"Range": {}, "Host": {}, "Referrer": {}, "Connection": {},
	"Accept": {}, "Accept-Encoding": {}, "Accept-Language": {}, "Cache-Control": {},
	"Upgrade-Insecure-Requests": {}, "Referer": {}, "Origin": {}, "User-Agent": {},
	"Pragma": {}, "Priority": {}, "Cookie": {},

	"Sec-Fetch-Dest": {}, "Sec-Fetch-Mode": {}, "Sec-Fetch-Site": {},
	"Sec-Ch-Ua": {}, "Sec-Ch-Ua-Mobile": {}, "Sec-Ch-Ua-Platform": {},
	"Content-Type": {}, "Content-Length": {},

	"X-Emby-Token": {}, "X-Emby-Authorization": {}, "Authorization": {},
	"X-Emby-Device-Id": {}, "X-Emby-Device-Name": {},

	"X-Emby-Client-IP": {}, "X-Real-IP": {}, "X-Forwarded-For": {},
	"X-Forwarded-Proto": {}, "X-Forwarded-Host": {}, "X-Forwarded-Port": {},
	"Via": {}, "X-Via": {},
	"Cf-Ray": {}, "Cf-Visitor": {}, "Cf-Connecting-Ip": {}, "Cf-Ipcountry": {},
	"Ali-Cdn-Real-Ip": {}, "Ali-Swift-Log-Host": {},
}

CacheKeyIgnoreParams 忽略的请求头或者参数

如果请求地址包含列表中的请求头或者参数, 则不参与 cacheKey 运算

View Source
var DefaultExpired = func() time.Duration { return config.C.Cache.ExpiredDuration() }

DefaultExpired 默认的请求过期时间

可通过设置 "Expired" 响应头进行覆盖

Functions

func CacheableRouteMarker

func CacheableRouteMarker() gin.HandlerFunc

CacheableRouteMarker 缓存白名单 只有匹配上正则表达式的路由才会被缓存

func Duration

func Duration(d time.Duration) string

Duration 将一个标准的时间转换成适用于缓存时间的字符串

func RequestCacher

func RequestCacher() gin.HandlerFunc

RequestCacher 请求缓存中间件

func WaitingForHandleChan

func WaitingForHandleChan()

WaitingForHandleChan 等待预缓存通道被处理完毕

Types

type RespCache

type RespCache interface {
	// Code 响应码
	Code() int

	// Body 克隆一个响应体, 转换为缓冲区
	Body() *bytes.Buffer

	// BodyBytes 克隆一个响应体
	BodyBytes() []byte

	// JsonBody 将响应体转化成 json 返回
	JsonBody() (*jsons.Item, error)

	// Header 获取响应头属性
	Header(key string) string

	// Headers 获取克隆响应头
	Headers() http.Header

	// Space 获取缓存空间名称
	Space() string

	// SpaceKey 获取缓存空间 key
	SpaceKey() string

	// Update 更新缓存
	//
	// code 传递零值时, 会自动忽略更新
	//
	// body 传递 nil 时, 会自动忽略更新,
	// 传递空切片时, 会认为是一个空响应体进行更新
	//
	// header 传递 nil 时, 会自动忽略更新,
	// 不为 nil 时, 缓存的响应头会被清空, 并设置为新值
	Update(code int, body []byte, header http.Header)
}

RespCache 公开对外暴露的缓存接口

func GetSpaceCache

func GetSpaceCache(space, spaceKey string) (RespCache, bool)

GetSpaceCache 获取缓存空间的缓存对象

Jump to

Keyboard shortcuts

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