Add TTL cache for stops index.
This commit is contained in:
parent
3caaf59506
commit
507d20810d
5 changed files with 118 additions and 59 deletions
87
pkg/cafes/common.go
Normal file
87
pkg/cafes/common.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package cafes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/dhconnelly/rtreego"
|
||||
)
|
||||
|
||||
type RefreshmentStop struct {
|
||||
Name string
|
||||
Url string
|
||||
Easting float64
|
||||
Northing float64
|
||||
}
|
||||
|
||||
func (s *RefreshmentStop) Bounds() *rtreego.Rect {
|
||||
p := rtreego.Point{s.Easting, s.Northing}
|
||||
return p.ToRect(100)
|
||||
}
|
||||
|
||||
func (s *RefreshmentStop) Contains(p rtreego.Point) bool {
|
||||
if len(p) != 2 {
|
||||
panic("Expected a 2-dimensional point")
|
||||
}
|
||||
bounds := s.Bounds()
|
||||
for i := 0; i < 2; i++ {
|
||||
if p[i] < bounds.PointCoord(i) || p[i] > bounds.PointCoord(i)+bounds.LengthsCoord(i) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TTL cache based on "9.7 Example: Concurrent Non-Blocking Cache" from
|
||||
// "The Go Programming Language", Alan A. A. Dovovan and Brian W. Kernighan
|
||||
|
||||
type result struct {
|
||||
value *rtreego.Rtree
|
||||
err error
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
res result
|
||||
expires time.Time
|
||||
ready chan struct{} // closed when res is ready
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
mu sync.Mutex
|
||||
entries map[string]*entry
|
||||
}
|
||||
|
||||
func New() *Cache {
|
||||
return &Cache{entries: make(map[string]*entry)}
|
||||
}
|
||||
|
||||
func (c *Cache) Get(k string) (*rtreego.Rtree, error) {
|
||||
c.mu.Lock()
|
||||
e := c.entries[k]
|
||||
if e == nil || e.expires.Before(time.Now()) {
|
||||
e = &entry{ready: make(chan struct{}), expires: time.Now().Add(4 * time.Hour)}
|
||||
c.entries[k] = e
|
||||
c.mu.Unlock()
|
||||
e.res.value, e.res.err = FetchStops(k)
|
||||
close(e.ready)
|
||||
} else {
|
||||
c.mu.Unlock()
|
||||
<-e.ready
|
||||
}
|
||||
return e.res.value, e.res.err
|
||||
}
|
||||
|
||||
var ErrInvalidStops = errors.New("invalid stops")
|
||||
|
||||
func FetchStops(k string) (*rtreego.Rtree, error) {
|
||||
switch k {
|
||||
case "ctccambridge":
|
||||
return FetchCtcCamIndex()
|
||||
case "cyclingmaps":
|
||||
return FetchCyclingMapsIndex()
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidStops, k)
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/dhconnelly/rtreego"
|
||||
|
@ -23,31 +24,6 @@ type Waypoints struct {
|
|||
Waypoints []Waypoint `xml:"wpt"`
|
||||
}
|
||||
|
||||
type RefreshmentStop struct {
|
||||
Name string
|
||||
Url string
|
||||
Easting float64
|
||||
Northing float64
|
||||
}
|
||||
|
||||
func (s *RefreshmentStop) Bounds() *rtreego.Rect {
|
||||
p := rtreego.Point{s.Easting, s.Northing}
|
||||
return p.ToRect(100)
|
||||
}
|
||||
|
||||
func (s *RefreshmentStop) Contains(p rtreego.Point) bool {
|
||||
if len(p) != 2 {
|
||||
panic("Expected a 2-dimensional point")
|
||||
}
|
||||
bounds := s.Bounds()
|
||||
for i := 0; i < 2; i++ {
|
||||
if p[i] < bounds.PointCoord(i) || p[i] > bounds.PointCoord(i)+bounds.LengthsCoord(i) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func BuildCtcCamIndex(r io.Reader) (*rtreego.Rtree, error) {
|
||||
dec := xml.NewDecoder(r)
|
||||
var wpt Waypoints
|
||||
|
@ -77,6 +53,7 @@ func BuildCtcCamIndex(r io.Reader) (*rtreego.Rtree, error) {
|
|||
}
|
||||
|
||||
func FetchCtcCamIndex() (*rtreego.Rtree, error) {
|
||||
log.Printf("Fetching %s", ctcCamWaypointsUrl)
|
||||
res, err := http.Get(ctcCamWaypointsUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting %s: %v", ctcCamWaypointsUrl, err)
|
||||
|
@ -89,5 +66,6 @@ func FetchCtcCamIndex() (*rtreego.Rtree, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("error building CTC Cambridge stops index: %v", err)
|
||||
}
|
||||
log.Printf("Loaded %d CTC Cambridge stops", index.Size())
|
||||
return index, nil
|
||||
}
|
||||
|
|
|
@ -54,6 +54,7 @@ func BuildCyclingMapsIndex(r io.Reader) (*rtreego.Rtree, error) {
|
|||
}
|
||||
|
||||
func FetchCyclingMapsIndex() (*rtreego.Rtree, error) {
|
||||
log.Printf("Fetching %s", cyclingMapsCafesUrl)
|
||||
res, err := http.Get(cyclingMapsCafesUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting %s: %v", cyclingMapsCafesUrl, err)
|
||||
|
@ -66,5 +67,6 @@ func FetchCyclingMapsIndex() (*rtreego.Rtree, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("error building cyclingmaps.net cafe stops index: %v", err)
|
||||
}
|
||||
log.Printf("Loaded %d cyclingmaps.net stops", index.Size())
|
||||
return index, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue