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 ¶
- Variables
- func CleanOldState(state *State, maxAge time.Duration)
- func DetectModulePath() (string, error)
- func GetImportPath(modulePath, filePath string) string
- func IsFileModified(path string, info os.FileInfo, state *State) bool
- func SaveState(state *State) error
- func UpdateFileState(state *State, path string, info os.FileInfo)
- type AnnotationInfo
- type Config
- type ConstructorInfo
- type FieldInfo
- type FileInfo
- type FileState
- type Generator
- type InterfaceInfo
- type MatchResult
- type Matcher
- func (m *Matcher) GetInterfaceCandidates(svcName string, interfaces map[string]*InterfaceInfo) []string
- func (m *Matcher) GetServiceByInterface(ifaceName string) string
- func (m *Matcher) Match(interfaces map[string]*InterfaceInfo, services map[string]*ServiceInfo) map[string]*MatchResult
- func (m *Matcher) MatchAllFiles(fileInfo *FileInfo) map[string]*MatchResult
- type MethodInfo
- type MethodSpec
- type PackageInfo
- type ParamSpec
- type Result
- type Scanner
- type ServiceInfo
- type State
Constants ¶
This section is empty.
Variables ¶
View Source
var Now = func() time.Time { return time.Now() }
Now 获取当前时间(便于测试)
Functions ¶
func GetImportPath ¶
GetImportPath 根据模块路径和文件路径获取导入路径
func IsFileModified ¶
IsFileModified 检查文件是否被修改
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 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 Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator 代码生成器
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 (*Matcher) GetInterfaceCandidates ¶
func (m *Matcher) GetInterfaceCandidates(svcName string, interfaces map[string]*InterfaceInfo) []string
GetInterfaceCandidates 获取可能的接口候选
func (*Matcher) GetServiceByInterface ¶
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 ¶
MethodSpec 方法规格
type PackageInfo ¶
type PackageInfo struct {
ImportPath string // 导入路径
Dir string // 文件系统路径
Name string // 包名
Files map[string]*FileInfo // 文件路径 -> 文件信息
}
PackageInfo 包信息
type Result ¶
type Result struct {
ModulePath string // 模块路径(从 go.mod 获取)
Packages map[string]*PackageInfo // 包路径 -> 包信息
Generated time.Time // 生成时间
}
Result 扫描结果
type ServiceInfo ¶
type ServiceInfo struct {
TypeName string // 类型名(小写开头)
Implements string // 实现的接口名(匹配后设置)
Methods map[string]*MethodInfo // 方法名 -> 方法信息
Constructor *ConstructorInfo // 构造函数信息(可选)
Fields []*FieldInfo // 结构体字段信息(用于智能初始化)
}
ServiceInfo 服务实现信息
Click to show internal directories.
Click to hide internal directories.