-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added caching of results, expiration time 100sec
- Loading branch information
1 parent
8c66f0d
commit b1ce99a
Showing
12 changed files
with
155 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 15. caching of results | ||
|
||
Date: 2024-01-28 | ||
|
||
## Status | ||
|
||
Accepted | ||
|
||
## Context | ||
|
||
Using the external APIs from Plausible and GitHub is resource intensive, and their results don't change too often. | ||
|
||
## Decision | ||
|
||
Introduce caching, related to [ADR 0011 (rate limit)](./0011-rate-limiter-with-persistently-stored-last-query-time.md) | ||
|
||
For Golang, a few caching libraries/packages exist, most targeting large volumes of data and/or high-throughput applications. | ||
|
||
We tested the simple packages | ||
* [go-cache](https://github.com/patrickmn/go-cache) and | ||
* [zcache](https://github.com/arc242/zcache) | ||
|
||
as both hav both global and entry-specific expiration times and is simple to use. | ||
|
||
`zcache` is an updated fork of `go-cache`, and go-cache is no longer actively maintained. Therefore we use `zcache`. | ||
|
||
A small example can be found in /cmd/cache/try-caching.go. | ||
|
||
## Consequences | ||
|
||
* cache needs to be typed | ||
* expiration needs to be set when pushing data into the cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"zgo.at/zcache/v2" | ||
|
||
// go-cache is not actively maintained any longer | ||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
// small test of the caching package "/go-cache v2.1.0+incompatible" | ||
// source inspired by https://github.com/patrickmn/go-cache | ||
|
||
func main() { | ||
// Create a cache with a default expiration time of 5 minutes, and which | ||
// purges expired items every 10 minutes | ||
c := cache.New(5*time.Minute, 10*time.Minute) | ||
|
||
// in zcache we need to use the function `SetWithExpire` | ||
z := zcache.New[string, any](zcache.NoExpiration, zcache.NoExpiration) | ||
z.SetWithExpire("foo", "bar", zcache.DefaultExpiration) | ||
|
||
// Set the value of the key "foo" to "bar", with the default expiration time | ||
c.Set("foo", "bar", cache.DefaultExpiration) | ||
|
||
// set "exp" to expire after 5 Milliseconds | ||
c.Set("exp", "42", 3000*time.Millisecond) | ||
|
||
c.Set("baz", 42, cache.NoExpiration) | ||
|
||
// wait for 1 second | ||
time.Sleep(time.Second) | ||
|
||
// Get the string associated with the key "foo" from the cache | ||
foo, found := c.Get("foo") | ||
if found { | ||
fmt.Println(foo) | ||
} | ||
|
||
exp, found := c.Get("exp") | ||
if found { | ||
fmt.Println(exp) | ||
} else { | ||
fmt.Println("exp not found in cache, expired?") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters