cache.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 modules
  15. import (
  16. "fmt"
  17. "sync"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  22. )
  23. const (
  24. cacheValidPerviod = 1 * time.Minute
  25. )
  26. type tCachedStatus int
  27. const (
  28. cacheInit = tCachedStatus(0)
  29. cacheFetching = tCachedStatus(1)
  30. cacheComplete = tCachedStatus(2)
  31. )
  32. type sCachedResource struct {
  33. key string
  34. cachedAt time.Time
  35. lock *sync.Cond
  36. object jsonutils.JSONObject
  37. status tCachedStatus
  38. }
  39. type sCachedResourceManager struct {
  40. lock *sync.Mutex
  41. resourceCache map[string]sCachedResource
  42. }
  43. var cachedResourceManager *sCachedResourceManager
  44. func init() {
  45. cachedResourceManager = &sCachedResourceManager{
  46. lock: &sync.Mutex{},
  47. resourceCache: make(map[string]sCachedResource),
  48. }
  49. }
  50. func cacheKey(manager modulebase.Manager, idstr string) string {
  51. return fmt.Sprintf("%s-%s", manager.KeyString(), idstr)
  52. }
  53. func (cm *sCachedResourceManager) getLocked(key string) *sCachedResource {
  54. cm.lock.Lock()
  55. defer cm.lock.Unlock()
  56. return cm.getUnlocked(key, true)
  57. }
  58. func (cm *sCachedResourceManager) getUnlocked(key string, alloc bool) *sCachedResource {
  59. _, ok := cm.resourceCache[key]
  60. if !ok {
  61. if !alloc {
  62. return nil
  63. }
  64. cm.resourceCache[key] = sCachedResource{
  65. key: key,
  66. lock: sync.NewCond(&sync.Mutex{}),
  67. status: cacheInit,
  68. }
  69. }
  70. obj := cm.resourceCache[key]
  71. return &obj
  72. }
  73. func (cm *sCachedResourceManager) getById(manager modulebase.Manager, session *mcclient.ClientSession, idstr string) (jsonutils.JSONObject, error) {
  74. key := cacheKey(manager, idstr)
  75. cacheObj := cm.getLocked(key)
  76. obj := cacheObj.tryGet()
  77. if obj != nil {
  78. return obj, nil
  79. }
  80. obj, err := manager.GetById(session, idstr, nil)
  81. if err != nil {
  82. cacheObj.notifyFail()
  83. return nil, err
  84. }
  85. cacheObj.notifyComplete(obj)
  86. return obj, nil
  87. }
  88. func (cr *sCachedResource) isValid() bool {
  89. now := time.Now()
  90. return cr.status == cacheComplete && now.Sub(cr.cachedAt) <= cacheValidPerviod && cr.object != nil
  91. }
  92. func (cr *sCachedResource) tryGet() jsonutils.JSONObject {
  93. cr.lock.L.Lock()
  94. defer cr.lock.L.Unlock()
  95. if cr.isValid() {
  96. return cr.object
  97. }
  98. for cr.status == cacheFetching {
  99. cr.lock.Wait()
  100. }
  101. if cr.status == cacheComplete {
  102. return cr.object
  103. }
  104. cr.status = cacheFetching
  105. return nil
  106. }
  107. func (cr *sCachedResource) notifyFail() {
  108. cr.lock.L.Lock()
  109. defer cr.lock.L.Unlock()
  110. cr.status = cacheInit
  111. cr.object = nil
  112. cr.lock.Signal()
  113. }
  114. func (cr *sCachedResource) notifyComplete(obj jsonutils.JSONObject) {
  115. cr.lock.L.Lock()
  116. defer cr.lock.L.Unlock()
  117. cr.status = cacheComplete
  118. cr.object = obj
  119. cr.cachedAt = time.Now()
  120. time.AfterFunc(cacheValidPerviod, func() {
  121. cachedResourceManager.lock.Lock()
  122. defer cachedResourceManager.lock.Unlock()
  123. cacheObj := cachedResourceManager.getUnlocked(cr.key, false)
  124. if cacheObj == nil {
  125. return
  126. }
  127. cacheObj.lock.L.Lock()
  128. defer cacheObj.lock.L.Unlock()
  129. if !cacheObj.isValid() {
  130. delete(cachedResourceManager.resourceCache, cr.key)
  131. }
  132. })
  133. cr.lock.Signal()
  134. }