|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# CPE存储功能指南 |
| 7 | + |
| 8 | +CPE库提供了全面的存储功能,用于管理CPE和CVE数据。相关文件包括: |
| 9 | + |
| 10 | +- [storage.go](mdc:storage.go) - 定义存储接口和存储管理器 |
| 11 | +- [file_storage.go](mdc:file_storage.go) - 基于文件系统的存储实现 |
| 12 | +- [memory_storage.go](mdc:memory_storage.go) - 基于内存的存储实现 |
| 13 | +- [dictionary.go](mdc:dictionary.go) - CPE字典功能 |
| 14 | + |
| 15 | +## 核心接口 |
| 16 | + |
| 17 | +### Storage 接口 |
| 18 | + |
| 19 | +`Storage` 接口定义了CPE和CVE数据的存储和检索方法: |
| 20 | + |
| 21 | +```go |
| 22 | +type Storage interface { |
| 23 | + Initialize() error |
| 24 | + Close() error |
| 25 | + StoreCPE(cpe *CPE) error |
| 26 | + RetrieveCPE(id string) (*CPE, error) |
| 27 | + UpdateCPE(cpe *CPE) error |
| 28 | + DeleteCPE(id string) error |
| 29 | + SearchCPE(criteria *CPE, options *MatchOptions) ([]*CPE, error) |
| 30 | + AdvancedSearchCPE(criteria *CPE, options *AdvancedMatchOptions) ([]*CPE, error) |
| 31 | + StoreCVE(cve *CVEReference) error |
| 32 | + RetrieveCVE(cveID string) (*CVEReference, error) |
| 33 | + // 更多方法... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## 存储实现 |
| 38 | + |
| 39 | +### 文件存储 |
| 40 | + |
| 41 | +文件存储 (`FileStorage`) 将CPE和CVE数据持久化到文件系统中: |
| 42 | + |
| 43 | +```go |
| 44 | +fileStorage, err := cpe.NewFileStorage("/path/to/data", true) |
| 45 | +if err != nil { |
| 46 | + log.Fatalf("创建文件存储失败: %v", err) |
| 47 | +} |
| 48 | +err = fileStorage.Initialize() |
| 49 | +``` |
| 50 | + |
| 51 | +### 内存存储 |
| 52 | + |
| 53 | +内存存储 (`MemoryStorage`) 提供高性能但非持久化的存储: |
| 54 | + |
| 55 | +```go |
| 56 | +memStorage, err := cpe.NewMemoryStorage() |
| 57 | +err = memStorage.Initialize() |
| 58 | +``` |
| 59 | + |
| 60 | +## 存储管理器 |
| 61 | + |
| 62 | +`StorageManager` 是一个管理主存储和缓存存储的封装类,主要方法包括: |
| 63 | + |
| 64 | +- `NewStorageManager(primary Storage) *StorageManager` - 创建存储管理器 |
| 65 | +- `SetCache(cache Storage)` - 设置缓存存储 |
| 66 | +- `GetCPE(id string) (*CPE, error)` - 优先从缓存获取CPE |
| 67 | +- `StoreCPE(cpe *CPE) error` - 存储CPE到主存储和缓存 |
| 68 | +- `GetCVE(cveID string) (*CVEReference, error)` - 优先从缓存获取CVE |
| 69 | +- `Search(criteria *CPE, options *MatchOptions) ([]*CPE, error)` - 搜索CPE |
| 70 | +- `AdvancedSearch(criteria *CPE, options *AdvancedMatchOptions) ([]*CPE, error)` - 高级搜索CPE |
| 71 | +- `InvalidateCache(id string)` - 使指定CPE的缓存失效 |
| 72 | +- `ClearCache() error` - 清空缓存 |
| 73 | +- `GetStats() (*StorageStats, error)` - 获取统计信息 |
| 74 | + |
| 75 | +## 使用示例 |
| 76 | + |
| 77 | +创建存储管理器: |
| 78 | +```go |
| 79 | +// 创建主存储 |
| 80 | +fileStorage, _ := cpe.NewFileStorage("/path/to/data", true) |
| 81 | +fileStorage.Initialize() |
| 82 | + |
| 83 | +// 创建存储管理器 |
| 84 | +manager := cpe.NewStorageManager(fileStorage) |
| 85 | + |
| 86 | +// 可选: 添加内存缓存 |
| 87 | +memCache, _ := cpe.NewMemoryStorage() |
| 88 | +memCache.Initialize() |
| 89 | +manager.SetCache(memCache) |
| 90 | +``` |
| 91 | + |
| 92 | +检索CPE: |
| 93 | +```go |
| 94 | +cpeID := "cpe:2.3:o:microsoft:windows:10:*:*:*:*:*:*:*" |
| 95 | +windowsCPE, err := manager.GetCPE(cpeID) |
| 96 | +``` |
| 97 | + |
| 98 | +搜索CPE: |
| 99 | +```go |
| 100 | +// 搜索所有Microsoft Windows 10产品 |
| 101 | +criteria := &cpe.CPE{ |
| 102 | + Vendor: cpe.Vendor("microsoft"), |
| 103 | + ProductName: cpe.Product("windows"), |
| 104 | + Version: cpe.Version("10"), |
| 105 | +} |
| 106 | +options := &cpe.MatchOptions{} |
| 107 | +results, err := manager.Search(criteria, options) |
| 108 | +``` |
| 109 | + |
| 110 | +## 缓存策略 |
| 111 | + |
| 112 | +- 读取操作优先从缓存获取,缓存未命中再从主存储获取 |
| 113 | +- 写入操作同时更新主存储和缓存 |
| 114 | +- 支持单个缓存项失效和整个缓存清空 |
| 115 | +- 默认缓存过期时间为1小时,可通过CacheTTLSeconds字段调整 |
| 116 | + |
| 117 | +## 数据字典 |
| 118 | + |
| 119 | +`CPEDictionary` 提供了管理CPE字典的功能: |
| 120 | + |
| 121 | +```go |
| 122 | +// 创建字典 |
| 123 | +dict := &cpe.CPEDictionary{ |
| 124 | + Name: "NVD CPE Dictionary", |
| 125 | + Description: "National Vulnerability Database CPE Dictionary", |
| 126 | + Version: "1.0", |
| 127 | + Items: []*cpe.CPEDictionaryItem{...}, |
| 128 | +} |
| 129 | + |
| 130 | +// 存储字典 |
| 131 | +err := storage.StoreDictionary(dict) |
| 132 | + |
| 133 | +// 检索字典 |
| 134 | +dict, err := storage.RetrieveDictionary() |
| 135 | +``` |
0 commit comments