podman

Форк
0
98 строк · 2.5 Кб
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

15
package spec
16

17
import (
18
	"sync"
19
)
20

21
// ResolutionCache a cache for resolving urls
22
type ResolutionCache interface {
23
	Get(string) (interface{}, bool)
24
	Set(string, interface{})
25
}
26

27
type simpleCache struct {
28
	lock  sync.RWMutex
29
	store map[string]interface{}
30
}
31

32
func (s *simpleCache) ShallowClone() ResolutionCache {
33
	store := make(map[string]interface{}, len(s.store))
34
	s.lock.RLock()
35
	for k, v := range s.store {
36
		store[k] = v
37
	}
38
	s.lock.RUnlock()
39

40
	return &simpleCache{
41
		store: store,
42
	}
43
}
44

45
// Get retrieves a cached URI
46
func (s *simpleCache) Get(uri string) (interface{}, bool) {
47
	s.lock.RLock()
48
	v, ok := s.store[uri]
49

50
	s.lock.RUnlock()
51
	return v, ok
52
}
53

54
// Set caches a URI
55
func (s *simpleCache) Set(uri string, data interface{}) {
56
	s.lock.Lock()
57
	s.store[uri] = data
58
	s.lock.Unlock()
59
}
60

61
var (
62
	// resCache is a package level cache for $ref resolution and expansion.
63
	// It is initialized lazily by methods that have the need for it: no
64
	// memory is allocated unless some expander methods are called.
65
	//
66
	// It is initialized with JSON schema and swagger schema,
67
	// which do not mutate during normal operations.
68
	//
69
	// All subsequent utilizations of this cache are produced from a shallow
70
	// clone of this initial version.
71
	resCache  *simpleCache
72
	onceCache sync.Once
73

74
	_ ResolutionCache = &simpleCache{}
75
)
76

77
// initResolutionCache initializes the URI resolution cache. To be wrapped in a sync.Once.Do call.
78
func initResolutionCache() {
79
	resCache = defaultResolutionCache()
80
}
81

82
func defaultResolutionCache() *simpleCache {
83
	return &simpleCache{store: map[string]interface{}{
84
		"http://swagger.io/v2/schema.json":       MustLoadSwagger20Schema(),
85
		"http://json-schema.org/draft-04/schema": MustLoadJSONSchemaDraft04(),
86
	}}
87
}
88

89
func cacheOrDefault(cache ResolutionCache) ResolutionCache {
90
	onceCache.Do(initResolutionCache)
91

92
	if cache != nil {
93
		return cache
94
	}
95

96
	// get a shallow clone of the base cache with swagger and json schema
97
	return resCache.ShallowClone()
98
}
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.