scan

package
v0.0.0-...-3470e27 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package scan 提供编译时扫描和代码生成功能

扫描 Go 源文件中的缓存注解,并生成对应的缓存包装器代码。

使用方式:

// 在业务代码中定义接口和实现
type UserServiceInterface interface {
    GetUser(id int64) (*User, error)
}

type userService struct {
    db *sql.DB
}

// @cacheable(cache="users", key="#id", ttl="30m")
func (s *userService) GetUser(id int64) (*User, error) {
    return s.db.QueryUser(id)
}

// 执行扫描命令
// $ gocache scan ./...

// 使用生成的代码
func main() {
    service.InitUserService(db)
    user, _ := service.UserService.GetUser(1)
}

Index

Constants

This section is empty.

Variables

View Source
var Now = func() time.Time {
	return time.Now()
}

Now 获取当前时间(便于测试)

Functions

func CleanOldState

func CleanOldState(state *State, maxAge time.Duration)

CleanOldState 清理过期状态

func DetectModulePath

func DetectModulePath() (string, error)

DetectModulePath 检测 go.mod 获取模块路径

func GetImportPath

func GetImportPath(modulePath, filePath string) string

GetImportPath 根据模块路径和文件路径获取导入路径

func IsFileModified

func IsFileModified(path string, info os.FileInfo, state *State) bool

IsFileModified 检查文件是否被修改

func SaveState

func SaveState(state *State) error

SaveState 保存扫描状态

func UpdateFileState

func UpdateFileState(state *State, path string, info os.FileInfo)

UpdateFileState 更新文件状态

Types

type AnnotationInfo

type AnnotationInfo struct {
	Type      string // cacheable/cacheput/cacheevict
	CacheName string
	Key       string
	TTL       string
	Condition string
	Unless    string
	Before    bool
}

AnnotationInfo 注解信息(从注释解析)

type Config

type Config struct {
	Dirs    []string // 扫描目录
	Force   bool     // 强制全量扫描
	Verbose bool     // 详细输出
}

Config 扫描配置

type ConstructorInfo

type ConstructorInfo struct {
	Name    string       // 构造函数名
	Params  []*ParamSpec // 参数
	Returns []*ParamSpec // 返回值
}

ConstructorInfo 构造函数信息

type FieldInfo

type FieldInfo struct {
	Name string // 字段名
	Type string // 字段类型
	Kind string // 字段种类:map/slice/struct/pointer/basic
}

FieldInfo 字段信息

type FileInfo

type FileInfo struct {
	Path       string                    // 文件路径
	Imports    map[string]string         // 导入路径 -> 别名 (e.g., "github.com/xxx/model" -> "model")
	Interfaces map[string]*InterfaceInfo // 接口名 -> 接口信息
	Services   map[string]*ServiceInfo   // 类型名 -> 服务信息
}

FileInfo 文件信息

type FileState

type FileState struct {
	ModTime time.Time `json:"mod_time"`
	Hash    string    `json:"hash"`
}

FileState 文件状态

type Generator

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

Generator 代码生成器

func NewGenerator

func NewGenerator(config *Config) *Generator

NewGenerator 创建生成器

func (*Generator) Generate

func (g *Generator) Generate(result *Result) error

Generate 生成缓存包装器代码

type InterfaceInfo

type InterfaceInfo struct {
	Name    string        // 接口名
	Methods []*MethodSpec // 方法定义
}

InterfaceInfo 接口信息

type MatchResult

type MatchResult struct {
	Interface *InterfaceInfo // 接口信息
	Service   *ServiceInfo   // 服务实现信息
}

MatchResult 匹配结果

type Matcher

type Matcher struct{}

Matcher 接口与实现匹配器

func NewMatcher

func NewMatcher() *Matcher

NewMatcher 创建匹配器

func (*Matcher) GetInterfaceCandidates

func (m *Matcher) GetInterfaceCandidates(svcName string, interfaces map[string]*InterfaceInfo) []string

GetInterfaceCandidates 获取可能的接口候选

func (*Matcher) GetServiceByInterface

func (m *Matcher) GetServiceByInterface(ifaceName string) string

GetServiceByInterface 根据接口名获取服务实现名

func (*Matcher) Match

func (m *Matcher) Match(interfaces map[string]*InterfaceInfo, services map[string]*ServiceInfo) map[string]*MatchResult

Match 匹配接口与实现 返回:接口名 -> 匹配结果

func (*Matcher) MatchAllFiles

func (m *Matcher) MatchAllFiles(fileInfo *FileInfo) map[string]*MatchResult

MatchAllFiles 匹配文件中所有接口与实现

type MethodInfo

type MethodInfo struct {
	Name      string // 方法名
	Params    []*ParamSpec
	Returns   []*ParamSpec
	Operation string // cacheable/cacheput/cacheevict
	Cache     string // 缓存名
	Key       string // 缓存 key 表达式
	TTL       string // TTL
	Condition string // 条件表达式
	Unless    string // unless 表达式
	Before    bool   // cacheevict 的 before 标志
}

MethodInfo 方法信息(含注解)

type MethodSpec

type MethodSpec struct {
	Name    string       // 方法名
	Params  []*ParamSpec // 参数
	Returns []*ParamSpec // 返回值
}

MethodSpec 方法规格

type PackageInfo

type PackageInfo struct {
	ImportPath string               // 导入路径
	Dir        string               // 文件系统路径
	Name       string               // 包名
	Files      map[string]*FileInfo // 文件路径 -> 文件信息
}

PackageInfo 包信息

type ParamSpec

type ParamSpec struct {
	Name string // 参数名
	Type string // 参数类型
}

ParamSpec 参数规格

type Result

type Result struct {
	ModulePath string                  // 模块路径(从 go.mod 获取)
	Packages   map[string]*PackageInfo // 包路径 -> 包信息
	Generated  time.Time               // 生成时间
}

Result 扫描结果

type Scanner

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

Scanner 扫描器

func NewScanner

func NewScanner(config *Config) *Scanner

NewScanner 创建扫描器

func (*Scanner) Scan

func (s *Scanner) Scan(dirs []string) (*Result, error)

Scan 执行扫描

type ServiceInfo

type ServiceInfo struct {
	TypeName    string                 // 类型名(小写开头)
	Implements  string                 // 实现的接口名(匹配后设置)
	Methods     map[string]*MethodInfo // 方法名 -> 方法信息
	Constructor *ConstructorInfo       // 构造函数信息(可选)
	Fields      []*FieldInfo           // 结构体字段信息(用于智能初始化)
}

ServiceInfo 服务实现信息

type State

type State struct {
	LastScan time.Time            `json:"last_scan"`
	Files    map[string]FileState `json:"files"`
}

State 增量扫描状态

func LoadState

func LoadState() *State

LoadState 加载上次扫描状态

Jump to

Keyboard shortcuts

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