Skip to content

Commit

Permalink
chore: lock map
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry committed Dec 4, 2024
1 parent 3c81997 commit d7c44d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/kitagry/bqls

go 1.22
go 1.23

toolchain go1.23.3

require (
Expand Down
22 changes: 13 additions & 9 deletions langserver/internal/bigquery/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
)

type cache struct {
db *database
bqClient Client
tableMetadataCache map[string]*bigquery.TableMetadata
db *database
bqClient Client
tableMetadataCacheLock sync.Mutex
tableMetadataCache map[string]*bigquery.TableMetadata

onceListProjects *sync.Once
onceListDatasets map[string]*sync.Once
Expand All @@ -33,12 +34,13 @@ func newCache(bqClient Client) (*cache, error) {
}

return &cache{
db: db,
bqClient: bqClient,
tableMetadataCache: make(map[string]*bigquery.TableMetadata),
onceListProjects: &sync.Once{},
onceListDatasets: make(map[string]*sync.Once),
onceListTables: make(map[string]*sync.Once),
db: db,
bqClient: bqClient,
tableMetadataCacheLock: sync.Mutex{},
tableMetadataCache: make(map[string]*bigquery.TableMetadata),
onceListProjects: &sync.Once{},
onceListDatasets: make(map[string]*sync.Once),
onceListTables: make(map[string]*sync.Once),
}, nil
}

Expand Down Expand Up @@ -175,6 +177,8 @@ func (c *cache) callListTables(ctx context.Context, projectID, datasetID string)

func (c *cache) GetTableMetadata(ctx context.Context, projectID, datasetID, tableID string) (*bigquery.TableMetadata, error) {
cacheKey := fmt.Sprintf("%s:%s:%s", projectID, datasetID, tableID)
c.tableMetadataCacheLock.Lock()
defer c.tableMetadataCacheLock.Unlock()
cache, ok := c.tableMetadataCache[cacheKey]
if ok {
return cache, nil
Expand Down

0 comments on commit d7c44d4

Please sign in to comment.