-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,9 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"golang.org/x/sync/singleflight" | ||
) | ||
|
||
type boundary struct { | ||
minLat float32 | ||
maxLat float32 | ||
minLon float32 | ||
maxLon float32 | ||
|
||
cache Store | ||
datasource GridDataSource | ||
sfg singleflight.Group | ||
} | ||
|
||
func NewBoundary(minLat, maxLat, minLon, maxLon float32, datasource GridDataSource) GridCache { | ||
return &boundary{ | ||
minLat: minLat, | ||
maxLat: maxLat, | ||
minLon: minLon, | ||
maxLon: maxLon, | ||
datasource: datasource, | ||
cache: NewMapStore(), | ||
func NewBoundary(minLat, maxLat, minLon, maxLon float32, datasource GridDataSource, cacheStore Store) GridCache { | ||
inCache := func(lat, lon float32) bool { | ||
return lat >= minLat && lat <= maxLat && lon >= minLon && lon <= maxLon | ||
} | ||
} | ||
|
||
func (b *boundary) ReadGridAt(ctx context.Context, grid int, lat, lon float32) (float32, error) { | ||
if lat < b.minLat || lat > b.maxLat || lon < b.minLon || lon > b.maxLon { | ||
return b.datasource.ReadGridAt(ctx, grid) | ||
} | ||
|
||
v, err, _ := b.sfg.Do(strconv.Itoa(grid), func() (interface{}, error) { | ||
vFromCache, ok := b.cache.Get(ctx, grid) | ||
if !ok { | ||
vFromSource, err := b.datasource.ReadGridAt(ctx, grid) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
b.cache.Set(ctx, grid, vFromSource) | ||
vFromCache = vFromSource | ||
} | ||
|
||
return vFromCache, nil | ||
}) | ||
|
||
return v.(float32), err | ||
return NewCustom(inCache, datasource, cacheStore) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"golang.org/x/sync/singleflight" | ||
) | ||
|
||
type custom struct { | ||
inCache func(lat, lon float32) bool | ||
|
||
cache Store | ||
datasource GridDataSource | ||
sfg singleflight.Group | ||
} | ||
|
||
func NewCustom(inCache func(lat, lon float32) bool, datasource GridDataSource, cacheStore Store) GridCache { | ||
return &custom{inCache: inCache, datasource: datasource, cache: cacheStore} | ||
} | ||
|
||
func (c *custom) InCache(lat, lon float32) bool { | ||
return c.inCache(lat, lon) | ||
} | ||
|
||
func (c *custom) ReadGridAt(ctx context.Context, grid int, lat, lon float32) (float32, error) { | ||
if !c.InCache(lat, lon) { | ||
return c.datasource.ReadGridAt(ctx, grid) | ||
} | ||
|
||
v, err, _ := c.sfg.Do(strconv.Itoa(grid), func() (interface{}, error) { | ||
vFromCache, ok := c.cache.Get(ctx, grid) | ||
if !ok { | ||
vFromSource, err := c.datasource.ReadGridAt(ctx, grid) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
c.cache.Set(ctx, grid, vFromSource) | ||
vFromCache = vFromSource | ||
} | ||
|
||
return vFromCache, nil | ||
}) | ||
|
||
return v.(float32), err | ||
} |
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