Skip to content

Commit f8508ed

Browse files
committed
Remove redundant cache
singleflight.Group has contained a `map` holding all the SQLRunners. The lru.Cache is basically meaningless.
1 parent 1ed74bf commit f8508ed

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

main.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
sqlrunner "github.com/database-playground/sqlrunner/lib"
14-
lru "github.com/hashicorp/golang-lru/v2"
1514
"golang.org/x/sync/singleflight"
1615
)
1716

@@ -21,15 +20,7 @@ func main() {
2120
addr = ":" + os.Getenv("PORT")
2221
}
2322

24-
runnersCache, err := lru.New[string, *sqlrunner.SQLRunner](20)
25-
if err != nil {
26-
slog.Error("failed to create LRU cache for runners", slog.Any("error", err))
27-
os.Exit(1)
28-
}
29-
30-
service := &SqlQueryService{
31-
runnersCache: runnersCache,
32-
}
23+
service := &SqlQueryService{}
3324
http.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
3425
w.WriteHeader(http.StatusOK)
3526
_, _ = w.Write([]byte("OK"))
@@ -45,8 +36,7 @@ func main() {
4536
}
4637

4738
type SqlQueryService struct {
48-
runnersCache *lru.Cache[string, *sqlrunner.SQLRunner]
49-
sfgroup singleflight.Group
39+
sfgroup singleflight.Group
5040
}
5141

5242
func (s *SqlQueryService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -86,19 +76,12 @@ func (s *SqlQueryService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8676
}
8777

8878
func (s *SqlQueryService) findRunner(schema string) (*sqlrunner.SQLRunner, error) {
89-
// If we have already prepared a runner for this schema, return it.
90-
runner, ok := s.runnersCache.Get(schema)
91-
if ok {
92-
return runner, nil
93-
}
94-
9579
result, err, _ := s.sfgroup.Do(schema, func() (any, error) {
9680
newRunner, err := sqlrunner.NewSQLRunner(schema)
9781
if err != nil {
9882
return nil, fmt.Errorf("create SQLRunner: %w", err)
9983
}
10084

101-
s.runnersCache.Add(schema, newRunner)
10285
return newRunner, nil
10386
})
10487
if err != nil {

0 commit comments

Comments
 (0)