github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/table_counter.go (about) 1 // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package kvstore 4 5 type CounterTable[T int64 | uint64] struct { 6 *Table[T] 7 } 8 9 func NewCounterTable[T int64 | uint64](name string) (*CounterTable[T], error) { 10 table, err := NewTable[T](name, NewIntValueEncoder[T]()) 11 if err != nil { 12 return nil, err 13 } 14 15 return &CounterTable[T]{ 16 Table: table, 17 }, nil 18 } 19 20 func (this *CounterTable[T]) Increase(key string, delta T) (newValue T, err error) { 21 if this.isClosed { 22 err = NewTableClosedErr(this.name) 23 return 24 } 25 26 err = this.Table.WriteTx(func(tx *Tx[T]) error { 27 value, getErr := tx.Get(key) 28 if getErr != nil { 29 if !IsNotFound(getErr) { 30 return getErr 31 } 32 } 33 34 newValue = value + delta 35 return tx.Set(key, newValue) 36 }) 37 return 38 }