Use akyoto/cache

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

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
}