feature_config_with_sync_map.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //+build go1.9
  2. package jsoniter
  3. import (
  4. "reflect"
  5. "sync"
  6. )
  7. type frozenConfig struct {
  8. configBeforeFrozen Config
  9. sortMapKeys bool
  10. indentionStep int
  11. objectFieldMustBeSimpleString bool
  12. onlyTaggedField bool
  13. decoderCache sync.Map
  14. encoderCache sync.Map
  15. extensions []Extension
  16. streamPool chan *Stream
  17. iteratorPool chan *Iterator
  18. }
  19. func (cfg *frozenConfig) initCache() {
  20. cfg.decoderCache = sync.Map{}
  21. cfg.encoderCache = sync.Map{}
  22. }
  23. func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) {
  24. cfg.decoderCache.Store(cacheKey, decoder)
  25. }
  26. func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) {
  27. cfg.encoderCache.Store(cacheKey, encoder)
  28. }
  29. func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder {
  30. decoder, found := cfg.decoderCache.Load(cacheKey)
  31. if found {
  32. return decoder.(ValDecoder)
  33. }
  34. return nil
  35. }
  36. func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
  37. encoder, found := cfg.encoderCache.Load(cacheKey)
  38. if found {
  39. return encoder.(ValEncoder)
  40. }
  41. return nil
  42. }