Skip to content

Commit

Permalink
feature. add cache feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Apr 26, 2024
1 parent 4dfcd23 commit 0881981
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
_apiClient "github.com/openinfradev/tks-api/pkg/api-client"
argo "github.com/openinfradev/tks-api/pkg/argo-client"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/openinfradev/tks-batch/internal/application"
cloudAccount "github.com/openinfradev/tks-batch/internal/cloud-account"
"github.com/openinfradev/tks-batch/internal/cluster"
"github.com/openinfradev/tks-batch/internal/database"
"github.com/openinfradev/tks-batch/internal/organization"
systemNotificationRule "github.com/openinfradev/tks-batch/internal/system-notification-rule"
gcache "github.com/patrickmn/go-cache"

Check failure on line 18 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / unittest

missing go.sum entry for module providing package github.com/patrickmn/go-cache (imported by github.com/openinfradev/tks-batch/cmd/server); to add:

Check failure on line 18 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / unittest

missing go.sum entry for module providing package github.com/patrickmn/go-cache (imported by github.com/openinfradev/tks-batch/cmd/server); to add:
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const INTERVAL_SEC = 5
Expand All @@ -30,6 +30,7 @@ var (
organizationAccessor *organization.OrganizationAccessor
systemNotificationRuleAccessor *systemNotificationRule.SystemNotificationAccessor
apiClient _apiClient.ApiClient
cache *gcache.Cache
)

func init() {
Expand Down Expand Up @@ -84,6 +85,8 @@ func main() {
log.Fatal(context.TODO(), "failed to create tks-api client : ", err)
}

cache = gcache.New(5*time.Minute, 10*time.Minute)

for {
err = processClusterStatus()
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion cmd/server/thanos_ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (

"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
gcache "github.com/patrickmn/go-cache"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const LAST_UPDATED_MIN = 1
const LAST_UPDATED_MIN = 1000

func processReloadThanosRules() error {
organizationIds, err := systemNotificationRuleAccessor.GetRecentlyUpdatedOrganizations(LAST_UPDATED_MIN)
Expand Down Expand Up @@ -48,6 +49,13 @@ func processReloadThanosRules() error {
}

func GetThanosRulerUrl(primaryClusterId string) (url string, err error) {
const prefix = "CACHE_KEY_THANOS_RULER_URL"
value, found := cache.Get(prefix + primaryClusterId)
if found {
log.Info(context.TODO(), "Cache HIT [CACHE_KEY_THANOS_RULER_URL] ", value)
return value.(string), nil
}

clientset_admin, err := kubernetes.GetClientAdminCluster(context.TODO())
if err != nil {
return url, errors.Wrap(err, "Failed to get client set for user cluster")
Expand Down Expand Up @@ -80,6 +88,8 @@ func GetThanosRulerUrl(primaryClusterId string) (url string, err error) {
} else {
url = "http://" + string(secrets.Data["thanos-ruler"])
}

cache.Set(prefix+primaryClusterId, url, gcache.DefaultExpiration)
return url, nil
}

Expand Down

0 comments on commit 0881981

Please sign in to comment.