Skip to content

Commit

Permalink
Bugfix - Request caching
Browse files Browse the repository at this point in the history
* Split caches for search / info
* Wipe search-cache after settings change
  • Loading branch information
moson-mo committed Apr 6, 2022
1 parent e031316 commit b9be2dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ In the default configuration, [yay](https://github.com/Jguer/yay) is being used.

* [tview](https://github.com/rivo/tview) for the tui components
* [go-alpm](https://github.com/Jguer/go-alpm) for searching the package DB's
* [go-cache](https://github.com/patrickmn/go-cache) to cache search results and package data
* [goaurrpc](https://github.com/moson-mo/goaurrpc) self-hosted backend for the AUR REST API calls (to not stress the official aur.archlinux.org/rpc endpoint)

#### How to build / run / install
Expand Down Expand Up @@ -48,4 +49,5 @@ The settings form can be opened with CTRL+S.
#### ToDo's

* Improve test code
* Implement caching for package data to have less lookups (AUR)
* ~~Implement caching for package data to have less lookups (AUR)~~ - done
* Add config options for disabling caching and setting the expiry time for cached entries
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/Jguer/go-alpm/v2 v2.1.2
github.com/Morganamilo/go-pacmanconf v0.0.0-20210502114700-cff030e927a5
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Expand Down
17 changes: 10 additions & 7 deletions internal/pacseek/pacseek.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
colorHighlight = "[#1793d1]"
colorTitle = "[#00dfff]"

version = "1.0.0"
version = "1.0.1"
)

// UI is holding our application information and all tview components
Expand Down Expand Up @@ -53,7 +53,8 @@ type UI struct {
width int
selectedPackage *InfoRecord
settingsChanged bool
cache *cache.Cache
infoCache *cache.Cache
searchCache *cache.Cache
}

// New creates a UI object and makes sure everything is initialized
Expand All @@ -65,7 +66,8 @@ func New(config *config.Settings) (*UI, error) {
messageLocker: &sync.RWMutex{},
quitSpin: make(chan bool),
settingsChanged: false,
cache: cache.New(5*time.Minute, 1*time.Minute),
infoCache: cache.New(5*time.Minute, 1*time.Minute),
searchCache: cache.New(5*time.Minute, 1*time.Minute),
}

var err error
Expand Down Expand Up @@ -427,7 +429,7 @@ func (ps *UI) showPackageInfo(row, column int) {
source := ps.packages.GetCell(row, 1).Text

go func() {
infoCached, foundCached := ps.cache.Get(pkg)
infoCached, foundCached := ps.infoCache.Get(pkg)
if source == "AUR" && !foundCached {
time.Sleep(time.Duration(ps.conf.AurSearchDelay) * time.Millisecond)
}
Expand All @@ -453,7 +455,7 @@ func (ps *UI) showPackageInfo(row, column int) {
} else {
info = infoPacman(ps.alpmHandle, pkg)
}
ps.cache.Set(pkg, info, cache.DefaultExpiration)
ps.infoCache.Set(pkg, info, cache.DefaultExpiration)
} else {
info = infoCached.(RpcResult)
}
Expand Down Expand Up @@ -519,7 +521,7 @@ func (ps *UI) showPackages(text string) {
defer ps.app.QueueUpdate(func() { ps.showPackageInfo(1, 0) })

var packages []Package
packagesCache, foundCache := ps.cache.Get("[search]" + text)
packagesCache, foundCache := ps.searchCache.Get(text)

if !foundCache {
var err error
Expand Down Expand Up @@ -557,7 +559,7 @@ func (ps *UI) showPackages(text string) {
packages = packages[:ps.conf.MaxResults]
}

ps.cache.Set("[search]"+text, packages, cache.DefaultExpiration)
ps.searchCache.Set(text, packages, cache.DefaultExpiration)

} else {
packages = packagesCache.([]Package)
Expand Down Expand Up @@ -754,6 +756,7 @@ func (ps *UI) saveSettings(defaults bool) {
}
ps.showMessage(msg, false)
ps.settingsChanged = false
ps.searchCache.Flush()
}

// starts the spinner
Expand Down

0 comments on commit b9be2dd

Please sign in to comment.