Skip to content

Commit bb594c1

Browse files
Merge pull request #51 from dineshgowda24/refresh-cache-in-bg
refresh expired cache in bg
2 parents 7835d99 + 5651b68 commit bb594c1

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ An Alfred workflow that lets you instantly open Google Cloud services or search
4141
3. Every command fetches only the non sensitive data it needs, keeping it lightweight and efficient.
4242
4. Set your `gcloud` path once, and the workflow runs smoothly without surprises.
4343

44-
> If you notice anything that doesn't align with this, it's unintentionalplease open an issue.
44+
> If you notice anything that doesn't align with this, it's unintentional, please open an issue 🙏
4545
4646
## Usage
4747

@@ -80,7 +80,7 @@ _This is not an exhaustive list: if changing the region has no effect, the resou
8080
1. Type `gcp tools:` to list the maintenance options powered by [AwGo Magic Actions](https://pkg.go.dev/github.com/deanishe/awgo#MagicAction).You can use <kbd>Tab</kbd> to navigate between actions like clearing the cache, viewing logs, or resetting internal data folders, then press <kbd>Enter</kbd> to run the selected action. <br>
8181
<img src="./assets/docs/tools.png" alt="Tools" width="500"/><br>
8282
_Example: Maintenance tools screen with options like clear cache and view logs._ <br>
83-
2. The workflow supports [Fuzzy Filtering](https://pkg.go.dev/github.com/deanishe/awgo/fuzzy), so you can quickly find services or resources even with partial or out-of-order matches.
83+
2. The workflow supports [Fuzzy Filtering](https://pkg.go.dev/github.com/deanishe/awgo/fuzzy), so you can quickly find services or resources, even with partial or out-of-order matches.
8484
3. By default, resource searches are cached for 7 days. To customize this duration, set the `ALFRED_GCP_WORKFLOW_CACHE_TTL_SECONDS` environment variable in Alfred. Learn how to do this here: [Alfred Environment Variables Guide](https://www.alfredapp.com/help/workflows/advanced/variables/#environment).
8585

8686

@@ -123,4 +123,4 @@ If this workflow saved you time or made your day a little easier, consider showi
123123

124124
This workflow is inspired by the amazing [aws-alfred-workflow](https://github.com/rkoval/alfred-aws-console-services-workflow).
125125
As a past user of that workflow before switching to GCP, I wanted to create a similar experience for GCP users.
126-
Huge thanks to the original author for the idea and inspiration without which this workflow wouldn't exist!
126+
Huge thanks to the original author for the idea and inspiration, without which this workflow wouldn't exist!

workflow/resource/resource.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,37 @@ func (b *Builder[T]) showCachedErr() bool {
204204
}
205205

206206
func (b *Builder[T]) load() ([]T, error) {
207-
if b.wf.Cache.Expired(b.cacheKey(), cacheTTL) {
208-
return nil, ErrCacheExpired
207+
key := b.cacheKey()
208+
if !b.wf.Cache.Expired(key, cacheTTL) {
209+
log.Debug("cache found")
210+
return b.loadJSONCache()
209211
}
210212

211-
var resources []T
212-
if err := b.wf.Cache.LoadJSON(b.cacheKey(), &resources); err != nil {
213+
// Cache expired, but an expired cache file exists.
214+
// Run the background job to refresh the cache and return the expired cache.
215+
// This allows the user to see the stale data while the background job fetches fresh data.
216+
if b.wf.Cache.Exists(key) {
217+
log.Info("cache expired, refreshing in background")
218+
b.runBackgroundJob()
219+
log.Debug("returning expired cache")
220+
return b.loadJSONCache()
221+
}
222+
223+
log.Debug("cache expired or not found")
224+
return nil, ErrCacheExpired
225+
}
226+
227+
func (b *Builder[T]) loadJSONCache() ([]T, error) {
228+
v := make([]T, 0)
229+
if err := b.wf.Cache.LoadJSON(b.cacheKey(), &v); err != nil {
213230
log.Error("failed to load cache:", err)
214231
if b.delCacheKey() != nil {
215232
return nil, err
216233
}
217234
return nil, err
218235
}
219-
return resources, nil
236+
237+
return v, nil
220238
}
221239

222240
func (b *Builder[T]) delCacheKey() error {

0 commit comments

Comments
 (0)