Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.24
go-version: 1.25

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.24-alpine AS builder
FROM golang:1.25-alpine AS builder
LABEL maintainer="github:@rusq"

WORKDIR /build
Expand Down
19 changes: 10 additions & 9 deletions addr.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package aklapi

import (
"context"
"encoding/json"
"errors"
"log"
"log/slog"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -36,19 +37,19 @@ func (s Address) String() string {
}

// AddressLookup is a convenience function to get addresses.
func AddressLookup(addr string) (*AddrResponse, error) {
return MatchingPropertyAddresses(&AddrRequest{SearchText: addr, PageSize: 10})
func AddressLookup(ctx context.Context, addr string) (*AddrResponse, error) {
return MatchingPropertyAddresses(ctx, &AddrRequest{SearchText: addr, PageSize: 10})
}

// MatchingPropertyAddresses wrapper around the AKL Council API.
func MatchingPropertyAddresses(addrReq *AddrRequest) (*AddrResponse, error) {
func MatchingPropertyAddresses(ctx context.Context, addrReq *AddrRequest) (*AddrResponse, error) {
cachedAr, ok := addrCache.Lookup(addrReq.SearchText)
if ok {
log.Printf("cached address result: %q", cachedAr)
slog.DebugContext(ctx, "found cached address result", "addr", cachedAr)
return cachedAr, nil
}

req, err := http.NewRequest("GET", addrURI, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, addrURI, nil)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +67,7 @@ func MatchingPropertyAddresses(addrReq *AddrRequest) (*AddrResponse, error) {
return nil, err
}
defer resp.Body.Close()
log.Printf("address call complete in %s", time.Since(start))
slog.DebugContext(ctx, "address call complete", "duration", time.Since(start))

if resp.StatusCode != http.StatusOK {
return nil, errors.New("address API returned status code: " + strconv.Itoa(resp.StatusCode))
Expand All @@ -82,8 +83,8 @@ func MatchingPropertyAddresses(addrReq *AddrRequest) (*AddrResponse, error) {
return &apiResp, nil
}

func oneAddress(addr string) (*Address, error) {
resp, err := AddressLookup(addr)
func oneAddress(ctx context.Context, addr string) (*Address, error) {
resp, err := AddressLookup(ctx, addr)
if err != nil {
return nil, err
}
Expand Down
17 changes: 0 additions & 17 deletions addr_cache.go

This file was deleted.

92 changes: 0 additions & 92 deletions addr_cache_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestMatchingPropertyAddresses(t *testing.T) {
oldURI := addrURI
defer func() { addrURI = oldURI }()
addrURI = tt.testSrv.URL
got, err := MatchingPropertyAddresses(tt.args.addrReq)
got, err := MatchingPropertyAddresses(t.Context(), tt.args.addrReq)
if (err != nil) != tt.wantErr {
t.Errorf("MatchingPropertyAddresses() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestAddress(t *testing.T) {
oldURI := addrURI
defer func() { addrURI = oldURI }()
addrURI = tt.testSrv.URL
got, err := AddressLookup(tt.args.addr)
got, err := AddressLookup(t.Context(), tt.args.addr)
if (err != nil) != tt.wantErr {
t.Errorf("Address() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -160,7 +160,7 @@ func Test_oneAddress(t *testing.T) {
oldURI := addrURI
defer func() { addrURI = oldURI }()
addrURI = tt.testSrv.URL
got, err := oneAddress(tt.args.addr)
got, err := oneAddress(t.Context(), tt.args.addr)
if (err != nil) != tt.wantErr {
t.Errorf("oneAddress() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
2 changes: 0 additions & 2 deletions aklapi.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package aklapi

import (
"regexp"
"time"
)

var (
defaultLoc, _ = time.LoadLocation("Pacific/Auckland") // Auckland is in NZ.
dow = regexp.MustCompile("Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday")
)
62 changes: 62 additions & 0 deletions caches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package aklapi

import (
"github.com/phuslu/lru"
)

const defCacheSz = 100 // seems reasonable.
var (
addrCache = newLRUCache[string, *AddrResponse](defCacheSz)
rubbishCache = rubbishResultCache{lc: newLRUCache[string, *CollectionDayDetailResult](defCacheSz)}
)

type lruCache[K comparable, V any] struct {
cache *lru.LRUCache[K, V]
}

func newLRUCache[K comparable, V any](size int) *lruCache[K, V] {
return &lruCache[K, V]{
cache: lru.NewLRUCache[K, V](size),
}
}

func (c *lruCache[K, V]) Lookup(key K) (resp V, ok bool) {
var nothing V
if NoCache {
return nothing, false
}
return c.cache.Get(key)
}

func (c *lruCache[K, V]) Add(key K, value V) {
c.cache.Set(key, value)
}

func (c *lruCache[K, V]) Delete(key K) {
c.cache.Delete(key)
}

type rubbishResultCache struct {
lc *lruCache[string, *CollectionDayDetailResult]
}

func (c *rubbishResultCache) Lookup(searchText string) (result *CollectionDayDetailResult, ok bool) {
result, ok = c.lc.Lookup(searchText)
if !ok {
return nil, false
}

today := now()
for _, res := range result.Collections {
if today.After(res.Date) || res.Date.IsZero() {
// invalidate from cache.
c.lc.Delete(searchText)
return nil, false
}
}
return
}

func (c *rubbishResultCache) Add(searchText string, result *CollectionDayDetailResult) {
c.lc.Add(searchText, result)
}
Loading