-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_path_cache.go
44 lines (35 loc) · 1.03 KB
/
schema_path_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package configx
import (
"crypto/sha256"
"fmt"
"github.com/clinia/x/jsonschemax"
"github.com/dgraph-io/ristretto"
"github.com/ory/jsonschema/v3"
)
var schemaPathCacheConfig = &ristretto.Config{
// Hold up to 25 schemas in cache. Usually we only need one.
MaxCost: 250,
NumCounters: 2500,
BufferItems: 64,
Metrics: false,
IgnoreInternalCost: true,
}
var schemaPathCache, _ = ristretto.NewCache(schemaPathCacheConfig)
func getSchemaPaths(rawSchema []byte, schema *jsonschema.Schema) ([]jsonschemax.Path, error) {
key := fmt.Sprintf("%x", sha256.Sum256(rawSchema))
if val, found := schemaPathCache.Get(key); found {
if validator, ok := val.([]jsonschemax.Path); ok {
return validator, nil
}
schemaPathCache.Del(key)
}
keys, err := jsonschemax.ListPathsWithInitializedSchemaAndArraysIncluded(schema)
if err != nil {
return nil, err
}
schemaPathCache.Set(key, keys, 1)
schemaPathCache.Wait()
return keys, nil
}