Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing db caching functionality #211

Open
wants to merge 77 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
c0487d4
Implementing file cache functionality
mchavez Aug 20, 2024
51035e1
Open or Create file
mchavez Aug 22, 2024
7de39ef
Refactoring code
mchavez Aug 22, 2024
d039707
Adding dbcache functionality
mchavez Aug 23, 2024
d9cff90
Adding dependencies
mchavez Aug 23, 2024
45e0c57
Updating dbcache
mchavez Aug 23, 2024
7f71a7b
Updating dbcache
mchavez Aug 24, 2024
581048a
Updating dbcache
mchavez Aug 24, 2024
62ee3c1
Removing unnecessary code
mchavez Aug 24, 2024
c3491b4
Fix linter issues
mchavez Aug 24, 2024
e4331dc
Adding tests
mchavez Aug 24, 2024
df27ab5
Adding tests
mchavez Aug 24, 2024
10ce53e
Adding interface
mchavez Aug 26, 2024
4a902b0
Setting up db cache within uhttp pkg
mchavez Aug 26, 2024
d5d4cd3
Adding validations
mchavez Aug 26, 2024
be1035d
Adding expiration time
mchavez Aug 27, 2024
ba075a4
Adding expiration time
mchavez Aug 27, 2024
cbeef77
Refactoring code
mchavez Aug 27, 2024
057eb92
Refactoring code
mchavez Aug 27, 2024
b23a5be
Refactoring code
mchavez Aug 27, 2024
346525c
Refactoring code
mchavez Aug 27, 2024
5c49df9
Refactoring code
mchavez Aug 27, 2024
8709cfa
Refactoring code
mchavez Aug 27, 2024
efa0b53
Code review changes
mchavez Aug 27, 2024
d8f7490
Code review changes
mchavez Aug 27, 2024
68af3c0
Code review changes
mchavez Aug 27, 2024
699b317
Code review changes
mchavez Aug 27, 2024
8cf4ce1
Adding db transactions
mchavez Aug 28, 2024
64f1a50
Fix bug when database is locked
mchavez Aug 28, 2024
14547d6
Fix tests
mchavez Aug 28, 2024
37a6f7b
Adding BATON_IN_MEMORY_HTTP_CACHE env
mchavez Aug 28, 2024
8c7e193
Adding stats structure
mchavez Aug 28, 2024
fd51eed
Adding statistics
mchavez Aug 29, 2024
65f88f3
Adding statistics
mchavez Aug 29, 2024
ead6126
Adding statistics
mchavez Aug 29, 2024
addbac0
Adding statistics
mchavez Aug 29, 2024
c3e8bf3
Refactoring code
mchavez Aug 29, 2024
43cb126
Refactoring code
mchavez Aug 30, 2024
0e856ce
Refactoring code
mchavez Sep 2, 2024
fee7aa2
Refactoring code
mchavez Sep 3, 2024
174fbfb
Code review changes
mchavez Sep 3, 2024
7d16c40
Refactoring code
mchavez Sep 3, 2024
f75425a
Code review changes
mchavez Sep 3, 2024
3704291
Code review changes part 1
mchavez Sep 3, 2024
f1b7f7c
Code review changes part 2
mchavez Sep 3, 2024
58579cf
Code review changes part 3
mchavez Sep 3, 2024
55965b1
Code review changes part 3
mchavez Sep 3, 2024
5bdb0c6
Code review changes part 4
mchavez Sep 3, 2024
e45ecec
Code review changes part 4
mchavez Sep 3, 2024
de5965b
Code review changes part 5
mchavez Sep 3, 2024
3badc34
Code review changes part 5
mchavez Sep 3, 2024
192d7f9
Code review changes part 5
mchavez Sep 3, 2024
1a7c3b1
Code review changes part 5
mchavez Sep 4, 2024
d4b2196
Code review changes part 6
mchavez Sep 4, 2024
d7c5dff
Refactoring code
mchavez Sep 5, 2024
e80e492
Refactoring code
mchavez Sep 5, 2024
79fe469
Code review changes
mchavez Sep 5, 2024
173bfbb
Code review changes
mchavez Sep 5, 2024
ec18980
Code review changes
mchavez Sep 5, 2024
f151295
Refactoring code
mchavez Sep 6, 2024
e15ae3f
Refactoring code
mchavez Sep 6, 2024
d824854
Refactoring code
mchavez Sep 6, 2024
4a9364d
Refactoring code
mchavez Sep 6, 2024
7fe3505
Refactoring code
mchavez Sep 6, 2024
6d467b1
Refactoring code
mchavez Sep 6, 2024
1c534fa
Refactoring code
mchavez Sep 6, 2024
8aed3e2
Refactoring code
mchavez Sep 6, 2024
9924f4f
Use goqu for sqlite like we do elsewhere in baton-sdk, removing the d…
ggreer Sep 10, 2024
11c3fd3
Fix linter error
ggreer Sep 12, 2024
3a91397
Fix error where failing tests didn't cause CI to fail.
ggreer Sep 12, 2024
22e8053
Upgrade github action versions.
ggreer Sep 12, 2024
a33e3d9
Use latest version of golang-test-annotations. Output test results in…
ggreer Sep 12, 2024
0f54d7c
Truncate tables on db cache initialization. Test setting same cache k…
ggreer Sep 12, 2024
0cbc8f4
Update on conflict. Set default cache size to 50MB.
ggreer Sep 12, 2024
efe2055
Clean up cache interface. Accept request instead of having to generat…
ggreer Sep 12, 2024
cc17bc3
Clean up cache config. Add noop cache instead of initializing a disab…
ggreer Sep 12, 2024
d6c6b32
Move cache config stuff into cache file. Don't create cachekey since …
ggreer Sep 13, 2024
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
17 changes: 10 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.21.x
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Run linters
uses: golangci/golangci-lint-action@v6
with:
Expand All @@ -24,23 +24,26 @@ jobs:
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: go tests
run: go test -v -covermode=count -json ./... | tee test.json
run: go test -v -covermode=count -json ./... > test.json
- name: Print go test results
if: always()
run: cat test.json
- name: annotate go tests
if: always()
uses: guyarb/golang-test-annotations@v0.5.1
uses: guyarb/golang-test-annotations@v0.8.0
with:
test-results: test.json
buf-lint-and-breaking-change-detection:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup
uses: bufbuild/buf-setup-action@v1
with:
Expand Down
19 changes: 11 additions & 8 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup
uses: bufbuild/buf-setup-action@v1
with:
Expand All @@ -25,13 +25,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.21.x
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Run linters
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: latest
args: --timeout=3m
Expand All @@ -44,15 +44,18 @@ jobs:
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: go tests
run: go test -v -covermode=count -json ./... | tee test.json
run: go test -v -covermode=count -json ./... > test.json
- name: Print go test results
if: always()
run: cat test.json
- name: annotate go tests
if: always()
uses: guyarb/golang-test-annotations@v0.5.1
uses: guyarb/golang-test-annotations@v0.8.0
with:
test-results: test.json
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
github.com/lufia/plan9stats v0.0.0-20240408141607-282e7b5d6b74 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
Expand Down
53 changes: 53 additions & 0 deletions pkg/uhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package uhttp

import (
"context"
"crypto/sha256"
"crypto/tls"
"fmt"
"net/http"
"sort"
"strings"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -72,3 +76,52 @@ func NewClient(ctx context.Context, options ...Option) (*http.Client, error) {
httpClient.Transport = t
return httpClient, nil
}

type icache interface {
Get(req *http.Request) (*http.Response, error)
Set(req *http.Request, value *http.Response) error
}

// CreateCacheKey generates a cache key based on the request URL, query parameters, and headers.
func CreateCacheKey(req *http.Request) (string, error) {
if req == nil {
return "", fmt.Errorf("request is nil")
}
var sortedParams []string
// Normalize the URL path
path := strings.ToLower(req.URL.Path)
// Combine the path with sorted query parameters
queryParams := req.URL.Query()
for k, v := range queryParams {
for _, value := range v {
sortedParams = append(sortedParams, fmt.Sprintf("%s=%s", k, value))
}
}

sort.Strings(sortedParams)
queryString := strings.Join(sortedParams, "&")
// Include relevant headers in the cache key
var headerParts []string
for key, values := range req.Header {
for _, value := range values {
if key == "Accept" || key == "Content-Type" || key == "Cookie" || key == "Range" {
headerParts = append(headerParts, fmt.Sprintf("%s=%s", key, value))
}
}
}

sort.Strings(headerParts)
headersString := strings.Join(headerParts, "&")
// Create a unique string for the cache key
cacheString := fmt.Sprintf("%s?%s&headers=%s", path, queryString, headersString)

// Hash the cache string to create a key
hash := sha256.New()
_, err := hash.Write([]byte(cacheString))
if err != nil {
return "", err
}

cacheKey := fmt.Sprintf("%x", hash.Sum(nil))
return cacheKey, nil
}
Loading
Loading