Skip to content

Commit 540f5b3

Browse files
committed
dnfjson: Add dnf-json result cache to BaseSolver
This is used to cache the results of dump and search requests for 60s. Once the timeout has passed the request is repeated and the timeout reset. The timeout is *not* reset on every cache hit which prevents, for example, a request every 59 seconds from keeping the cache from updating. When the existing CleanCache() function is called to check the on-disk metadata cache it will also delete any expired entries from the resultCache in order to keep it from eventually consuming all memory.
1 parent 73c50c8 commit 540f5b3

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

internal/dnfjson/dnfjson.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ type BaseSolver struct {
3838

3939
// Path to the dnf-json binary and optional args (default: "/usr/libexec/osbuild-composer/dnf-json")
4040
dnfJsonCmd []string
41+
42+
resultCache *dnfCache
4143
}
4244

4345
// Create a new unconfigured BaseSolver (without platform information). It can
4446
// be used to create configured Solver instances with the NewWithConfig()
4547
// method.
4648
func NewBaseSolver(cacheDir string) *BaseSolver {
4749
return &BaseSolver{
48-
cache: newRPMCache(cacheDir, 524288000), // 500 MiB
49-
dnfJsonCmd: []string{"/usr/libexec/osbuild-composer/dnf-json"},
50+
cache: newRPMCache(cacheDir, 524288000), // 500 MiB
51+
dnfJsonCmd: []string{"/usr/libexec/osbuild-composer/dnf-json"},
52+
resultCache: NewDNFCache(60 * time.Second),
5053
}
5154
}
5255

@@ -80,6 +83,7 @@ func (bs *BaseSolver) NewWithConfig(modulePlatformID string, releaseVer string,
8083
// the total size of the cache falls below the configured maximum size (see
8184
// SetMaxCacheSize()).
8285
func (bs *BaseSolver) CleanCache() error {
86+
bs.resultCache.CleanCache()
8387
return bs.cache.shrink()
8488
}
8589

@@ -153,6 +157,11 @@ func (s *Solver) FetchMetadata(repos []rpmmd.RepoConfig) (rpmmd.PackageList, err
153157
s.cache.locker.RLock()
154158
defer s.cache.locker.RUnlock()
155159

160+
// Is this cached?
161+
if pkgs, ok := s.resultCache.Get(req.Hash()); ok {
162+
return pkgs, nil
163+
}
164+
156165
result, err := run(s.dnfJsonCmd, req)
157166
if err != nil {
158167
return nil, err
@@ -177,6 +186,9 @@ func (s *Solver) FetchMetadata(repos []rpmmd.RepoConfig) (rpmmd.PackageList, err
177186
sort.Slice(pkgs, func(i, j int) bool {
178187
return sortID(pkgs[i]) < sortID(pkgs[j])
179188
})
189+
190+
// Cache the results
191+
s.resultCache.Store(req.Hash(), pkgs)
180192
return pkgs, nil
181193
}
182194

@@ -191,6 +203,11 @@ func (s *Solver) SearchMetadata(repos []rpmmd.RepoConfig, packages []string) (rp
191203
s.cache.locker.RLock()
192204
defer s.cache.locker.RUnlock()
193205

206+
// Is this cached?
207+
if pkgs, ok := s.resultCache.Get(req.Hash()); ok {
208+
return pkgs, nil
209+
}
210+
194211
result, err := run(s.dnfJsonCmd, req)
195212
if err != nil {
196213
return nil, err
@@ -215,6 +232,9 @@ func (s *Solver) SearchMetadata(repos []rpmmd.RepoConfig, packages []string) (rp
215232
sort.Slice(pkgs, func(i, j int) bool {
216233
return sortID(pkgs[i]) < sortID(pkgs[j])
217234
})
235+
236+
// Cache the results
237+
s.resultCache.Store(req.Hash(), pkgs)
218238
return pkgs, nil
219239
}
220240

0 commit comments

Comments
 (0)