New builds a Cache from the given Config. It returns (nil, nil) when caching is disabled, allowing callers to short-circuit with a simple nil check.
(cfg Config)
| 85 | // caching is disabled, allowing callers to short-circuit with a simple |
| 86 | // nil check. |
| 87 | func New(cfg Config) (*Cache, error) { |
| 88 | if !cfg.Enabled { |
| 89 | return nil, nil //nolint:nilnil // intentional: nil signals caching disabled |
| 90 | } |
| 91 | |
| 92 | c := &Cache{ |
| 93 | entries: make(map[string]string), |
| 94 | normalize: keyNormalizer(cfg.CaseSensitive, cfg.TrimSpaces), |
| 95 | path: cfg.Path, |
| 96 | } |
| 97 | |
| 98 | if cfg.Path != "" { |
| 99 | if err := loadFromFile(cfg.Path, c.entries); err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | c.mtime = mtimeOf(cfg.Path) |
| 103 | } |
| 104 | |
| 105 | return c, nil |
| 106 | } |
| 107 | |
| 108 | // Lookup returns the stored response for the given question and a |
| 109 | // boolean indicating whether the question was found. |