Use akyoto/cache

This commit is contained in:
Eduard Urbach 2019-05-11 23:12:36 +09:00
parent 68859b71cf
commit d2e797f211
5 changed files with 21 additions and 17 deletions

View File

@ -14,7 +14,7 @@ steps:
- pack
- go build -v
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0
- golangci-lint run --disable-all --enable deadcode --enable misspell --enable prealloc
- golangci-lint run --disable-all --enable deadcode --enable misspell --enable prealloc --enable typecheck --enable varcheck
- go test -v -coverprofile=coverage.txt .
# - go mod download
# - make tools

6
go.mod
View File

@ -10,7 +10,7 @@ require (
github.com/aerogo/api v0.1.6
github.com/aerogo/codetree v1.2.2 // indirect
github.com/aerogo/crawler v0.2.1
github.com/aerogo/graphql v0.3.4
github.com/aerogo/graphql v0.3.6
github.com/aerogo/http v1.0.3
github.com/aerogo/layout v0.1.5
github.com/aerogo/log v0.2.4
@ -23,6 +23,7 @@ require (
github.com/aerogo/session-store-nano v0.1.3
github.com/aerogo/sitemap v0.1.1
github.com/akyoto/autoimport v0.6.2 // indirect
github.com/akyoto/cache v1.0.0
github.com/akyoto/color v1.8.3
github.com/animenotifier/anilist v0.2.2
github.com/animenotifier/arn v1.1.18
@ -44,7 +45,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mssola/user_agent v0.5.0
github.com/pariz/gountries v0.0.0-20171019111738-adb00f6513a3
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/shirou/gopsutil v2.18.12+incompatible
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
github.com/smartystreets/assertions v0.0.0-20190401211740-f487f9de1cd3 // indirect
@ -59,6 +59,4 @@ require (
gopkg.in/yaml.v2 v2.2.2 // indirect
)
replace github.com/aerogo/graphql => /home/eduard/projects/aerogo/graphql
exclude github.com/logpacker/PayPal-Go-SDK v2.0.0+incompatible

6
go.sum
View File

@ -43,6 +43,8 @@ github.com/aerogo/csp v0.1.5 h1:4l8AaMWndSb0t/03onf4lc0th2zoaapmtKgysDIznW4=
github.com/aerogo/csp v0.1.5/go.mod h1:KNqnTFffuDwIPJxBEFTl3baBx+x3Vw+9kcMfu5APFJA=
github.com/aerogo/flow v0.1.2 h1:fZ3V7Bo7jwBjqnM1mWxYbg5O/FlQX2XovzMZU2D+o5M=
github.com/aerogo/flow v0.1.2/go.mod h1:xXIb7GY0AKouhbp4/ViCsiOmvvgRGonqPG30d6FnACI=
github.com/aerogo/graphql v0.3.6 h1:6rAM4yFXa0qLaNz7bnLja3JAyV5fu2kq833Nsyzjizo=
github.com/aerogo/graphql v0.3.6/go.mod h1:bcAaQk3IGODFLF2gFwVszP3hyumWOhwKKuOh1xClSeI=
github.com/aerogo/http v1.0.0 h1:15a7Px8wGZbmgFqJMR1kBy3fWFkDfMZWexJzxhmFKe4=
github.com/aerogo/http v1.0.0/go.mod h1:B1igUmMLpE6KabMpc9reHCJJNUOJ2U/PR9s1fF3TpPQ=
github.com/aerogo/http v1.0.1 h1:KTzYarhp5yougurxuAhJKFh3YvZ7R7CfaDVwJv5+xIs=
@ -105,6 +107,8 @@ github.com/akyoto/autoimport v0.6.1 h1:Udg0g6QscjeSniLuCGTRGGdSJ56A3JZXwwr5jlgPk
github.com/akyoto/autoimport v0.6.1/go.mod h1:bW8Dn5oxnbF7pkAIECGV9RMb3d+DD92fKoMWq9M9q2o=
github.com/akyoto/autoimport v0.6.2 h1:HEJ3WshEAbO7IrQsSiVQibqX6wSZCpWoLvuvNWxdSNU=
github.com/akyoto/autoimport v0.6.2/go.mod h1:bW8Dn5oxnbF7pkAIECGV9RMb3d+DD92fKoMWq9M9q2o=
github.com/akyoto/cache v1.0.0 h1:nFKwB1bd/KPIHnw7WJOrsmwzhVr2FTJo9UrSSxDlH5g=
github.com/akyoto/cache v1.0.0/go.mod h1:MgYroBUaHREY9mmTcavctH4NDzQohCr4WMWPUKv7pq4=
github.com/akyoto/color v1.8.1 h1:HpoHQMGnBYZNpNuayXDKJMlGKvbvGipLYRtGV24SOgM=
github.com/akyoto/color v1.8.1/go.mod h1:M0u9nnLt9zM2qp08X40EVVFeR+FgNI2NG0InuiDBTQ0=
github.com/akyoto/color v1.8.2 h1:FOTpvfQkCk00yCl8oU/wjO3+jBi3FWpwUogLkeyzWfU=
@ -221,8 +225,6 @@ github.com/mssola/user_agent v0.5.0 h1:gRF7/x8cKt8qzAosYGsBNyirta+F8fvYDlJrgXws9
github.com/mssola/user_agent v0.5.0/go.mod h1:UFiKPVaShrJGW93n4uo8dpPdg1BSVpw2P9bneo0Mtp8=
github.com/pariz/gountries v0.0.0-20171019111738-adb00f6513a3 h1:lmQNznFSupyfCDE9d7wdKOU8UiVwWEoYwv8wo6rSgy0=
github.com/pariz/gountries v0.0.0-20171019111738-adb00f6513a3/go.mod h1:U0ETmPPEsfd7CpUKNMYi68xIOL8Ww4jPZlaqNngcwqs=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/profile v1.3.0/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@ -1,8 +1,17 @@
package middleware
// import (
// "strings"
// "time"
// "github.com/aerogo/aero"
// "github.com/akyoto/cache"
// "github.com/animenotifier/notify.moe/utils"
// )
// const requestThreshold = 10
// var ipToStats = cache.New(15*time.Minute, 15*time.Minute)
// var ipToStats = cache.New(15 * time.Minute)
// // IPStats captures the statistics for a single IP.
// type IPStats struct {
@ -31,7 +40,7 @@ package middleware
// Requests: []string{},
// }
// ipToStats.Set(ip, stats, cache.DefaultExpiration)
// ipToStats.Set(ip, stats, 15*time.Minute)
// }
// // Add requested URI to the list of requests

View File

@ -2,20 +2,15 @@ package middleware
import (
"net"
"sync"
"time"
cache "github.com/patrickmn/go-cache"
"github.com/akyoto/cache"
)
var ipToHosts = cache.New(60*time.Minute, 30*time.Minute)
var ipToHostsMutex sync.Mutex
var ipToHosts = cache.New(60 * time.Minute)
// GetHostsForIP returns all host names for the given IP (if cached).
func GetHostsForIP(ip string) ([]string, bool) {
ipToHostsMutex.Lock()
defer ipToHostsMutex.Unlock()
hosts, found := ipToHosts.Get(ip)
if !found {
@ -42,7 +37,7 @@ func findHostsForIP(ip string) []string {
}
// Cache host names
ipToHosts.Set(ip, hosts, cache.DefaultExpiration)
ipToHosts.Set(ip, hosts, 60*time.Minute)
return hosts
}