keystonecache.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package db
  15. import (
  16. "time"
  17. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  18. )
  19. type SKeystoneCacheObjectManager struct {
  20. SStandaloneResourceBaseManager
  21. }
  22. type SKeystoneCacheObject struct {
  23. SStandaloneResourceBase
  24. SPendingDeletedBase
  25. DomainId string `width:"128" charset:"ascii" nullable:"true"`
  26. Domain string `width:"128" charset:"utf8" nullable:"true"`
  27. LastCheck time.Time `nullable:"true"`
  28. Lang string `width:"8" charset:"ascii" nullable:"true" list:"domain" update:"domain" create:"domain_optional"`
  29. }
  30. func NewKeystoneCacheObjectManager(dt interface{}, tableName string, keyword string, keywordPlural string) SKeystoneCacheObjectManager {
  31. return SKeystoneCacheObjectManager{SStandaloneResourceBaseManager: NewStandaloneResourceBaseManager(dt, tableName, keyword, keywordPlural)}
  32. }
  33. func NewKeystoneCacheObject(id string, name string, domainId string, domain string) SKeystoneCacheObject {
  34. obj := SKeystoneCacheObject{}
  35. obj.Id = id
  36. obj.Name = name
  37. obj.Domain = domain
  38. obj.DomainId = domainId
  39. return obj
  40. }
  41. func (t *SKeystoneCacheObject) IsExpired() bool {
  42. if t.LastCheck.IsZero() {
  43. return true
  44. }
  45. now := time.Now().UTC()
  46. if t.LastCheck.Add(consts.GetTenantCacheExpireSeconds()).Before(now) {
  47. return true
  48. }
  49. return false
  50. }