code.vegaprotocol.io/vega@v0.79.0/core/matching/cache.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package matching 17 18 import ( 19 "code.vegaprotocol.io/vega/core/types" 20 "code.vegaprotocol.io/vega/libs/num" 21 ) 22 23 type BookCache struct { 24 indicativePrice cachedUint 25 indicativeVolume cachedUint64 26 indicativeUncrossingSide cachedSide 27 } 28 29 func NewBookCache() BookCache { 30 return BookCache{ 31 indicativePrice: cachedUint{ 32 value: num.UintZero(), 33 }, 34 } 35 } 36 37 type cachedUint struct { 38 valid bool 39 value *num.Uint 40 } 41 42 type cachedUint64 struct { 43 valid bool 44 value uint64 45 } 46 47 type cachedSide struct { 48 valid bool 49 value types.Side 50 } 51 52 func (c *cachedUint) Set(u *num.Uint) { 53 c.value = u 54 c.valid = true 55 } 56 57 func (c *cachedUint) Invalidate() { 58 c.valid = false 59 } 60 61 func (c *cachedUint) Get() (*num.Uint, bool) { 62 return c.value.Clone(), c.valid 63 } 64 65 func (c *cachedUint64) Set(u uint64) { 66 c.value = u 67 c.valid = true 68 } 69 70 func (c *cachedUint64) Invalidate() { 71 c.valid = false 72 } 73 74 func (c *cachedUint64) Get() (uint64, bool) { 75 return c.value, c.valid 76 } 77 78 func (c *cachedSide) Set(s types.Side) { 79 c.value = s 80 c.valid = true 81 } 82 83 func (c *cachedSide) Invalidate() { 84 c.valid = false 85 } 86 87 func (c *cachedSide) Get() (types.Side, bool) { 88 return c.value, c.valid 89 } 90 91 func (c *BookCache) Invalidate() { 92 c.indicativePrice.Invalidate() 93 c.indicativeVolume.Invalidate() 94 c.indicativeUncrossingSide.Invalidate() 95 } 96 97 func (c *BookCache) SetIndicativePrice(v *num.Uint) { 98 c.indicativePrice.Set(v) 99 } 100 101 func (c *BookCache) GetIndicativePrice() (*num.Uint, bool) { 102 return c.indicativePrice.Get() 103 } 104 105 func (c *BookCache) SetIndicativeVolume(v uint64) { 106 c.indicativeVolume.Set(v) 107 } 108 109 func (c *BookCache) GetIndicativeVolume() (uint64, bool) { 110 return c.indicativeVolume.Get() 111 } 112 113 func (c *BookCache) SetIndicativeUncrossingSide(s types.Side) { 114 c.indicativeUncrossingSide.Set(s) 115 } 116 117 func (c *BookCache) GetIndicativeUncrossingSide() (types.Side, bool) { 118 return c.indicativeUncrossingSide.Get() 119 }