Ai
1 Star 0 Fork 0

小于的个人工具集/https

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
init.go 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyutab 提交于 2023-12-13 15:54 +08:00 . 修改缓存命中和缓存编码逻辑
package https
import (
"time"
)
// 网址服务对象
type CURL struct {
Uri string `json:"uri"` // 请求网址
Param_quest map[string]string `json:"param"` // 请求参数
HttpCode int `json:"http_code"` // HTTP返回的code值
ParamJson_quest map[string]any `json:"param_json"` // JSON推送参数
Body string `json:"body"` // 返回值
Error error `json:"error"` // 错误信息
Header_quest map[string]string `json:"header"` // 请求Header头
CreateTime time.Time `json:"create_time"` // 实例创建时间
StartTime time.Time `json:"start_time"` // 请求开始时间
EndTime time.Time `json:"end_time"` // 请求结束时间
ClientIp string `json:"client_ip"` // 请求的客户端IP【兼容日志处理模块的预留字段】
Cookie_quest []string `json:"cookie"` // HTTP网址返回的Set-Cookie相应头
option *Option `json:"-"` // 请求的选项信息
cache_hit bool `json:"-"` // 是否命中缓存
}
// 外部选项传参结构[可选参数]
type Option struct {
https_continue bool // 是否跳过HTTPS证书效验
time_out time.Duration // 请求超时时间
cache_time time.Duration // 缓存时间【0表示不设置缓存】
ignore_header []string // 忽略请求头
}
// 选项设置参数,用于设置本次请求中的选项信息
type OptionFunc func(c *Option)
// 内部配置结构体
type config struct {
LogFunc []func(c *CURL) // 日志回写函数【缓存命中后将不记录此处的请求日志】
timeOut time.Duration // 默认超时时间
cacheTime time.Duration // 默认缓存时间
cacheInterface CacheInterface // 缓存配置项
}
// 缓存钩子
type CacheInterface interface {
Get(name string) string // 获取缓存值,缓存未找到的话请返回空字符串
Set(name string, val string, t time.Duration) // 设置缓存值,可能存在部分无法在string中正常显示的字符串
}
// 程序调用的默认值信息
var _default config = config{
timeOut: time.Second * 60,
cacheTime: 0,
cacheInterface: &cache{},
}
// 设置Quest请求日志
//
// f 日志记录函数【此函数会记录多个,所以请勿一直调用SetQuestLog进行插入日志记录函数】
func SetQuestLog(f func(c *CURL)) {
if f == nil {
return
}
_default.LogFunc = append(_default.LogFunc, f)
}
// 清除Quest请求日志记录的函数列表
func ClearQuestLog() {
_default.LogFunc = nil
}
// 设置默认的超时时间
//
// t 超时时间
func SetDefaultTimeOut(t time.Duration) {
_default.timeOut = t
}
// 设置默认的缓存时间
// 不建议设置此值,若确实需要缓存的话,建议针对URL地址进行配置缓存时间
//
// t 缓存时间
func SetDefaultCacheTime(t time.Duration) {
_default.cacheTime = t
}
// 设置缓存信息
//
// t 实现缓存方法的接口信息
func SetDefaultCacheFunc(t CacheInterface) {
_default.cacheInterface = t
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xgotool/https.git
git@gitee.com:xgotool/https.git
xgotool
https
https
v0.0.1

搜索帮助