本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

61 lines
1.6KB

  1. // Copyright 2015 go-swagger maintainers
  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 spec
  15. import "sync"
  16. // ResolutionCache a cache for resolving urls
  17. type ResolutionCache interface {
  18. Get(string) (interface{}, bool)
  19. Set(string, interface{})
  20. }
  21. type simpleCache struct {
  22. lock sync.RWMutex
  23. store map[string]interface{}
  24. }
  25. // Get retrieves a cached URI
  26. func (s *simpleCache) Get(uri string) (interface{}, bool) {
  27. debugLog("getting %q from resolution cache", uri)
  28. s.lock.RLock()
  29. v, ok := s.store[uri]
  30. debugLog("got %q from resolution cache: %t", uri, ok)
  31. s.lock.RUnlock()
  32. return v, ok
  33. }
  34. // Set caches a URI
  35. func (s *simpleCache) Set(uri string, data interface{}) {
  36. s.lock.Lock()
  37. s.store[uri] = data
  38. s.lock.Unlock()
  39. }
  40. var resCache ResolutionCache
  41. func init() {
  42. resCache = initResolutionCache()
  43. }
  44. // initResolutionCache initializes the URI resolution cache
  45. func initResolutionCache() ResolutionCache {
  46. return &simpleCache{store: map[string]interface{}{
  47. "http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),
  48. "http://json-schema.org/draft-04/schema": MustLoadJSONSchemaDraft04(),
  49. }}
  50. }
上海开阖软件有限公司 沪ICP备12045867号-1