11package database
22
33import (
4- "encoding/json"
54 "errors"
65 "strconv"
76 "time"
87
98 "github.com/0xb10c/memo/api/config"
9+ "github.com/0xb10c/memo/api/types"
1010 "github.com/gomodule/redigo/redis"
11+ jsoniter "github.com/json-iterator/go"
1112)
1213
13- var (
14- Pool * redis.Pool
15- )
14+ var Pool * redis.Pool
15+ var json = jsoniter .ConfigCompatibleWithStandardLibrary
1616
1717func newPool () * redis.Pool {
1818 dbUser := config .GetString ("redis.user" )
@@ -81,7 +81,7 @@ func GetMempool() (timestamp time.Time, feerateMapJSON string, megabyteMarkersJS
8181 return
8282}
8383
84- func GetRecentBlocks () (blocks []RecentBlock , err error ) {
84+ func GetRecentBlocks () (blocks []types. RecentBlock , err error ) {
8585 c := Pool .Get ()
8686 defer c .Close ()
8787
@@ -90,9 +90,9 @@ func GetRecentBlocks() (blocks []RecentBlock, err error) {
9090 return
9191 }
9292
93- blocks = make ([]RecentBlock , 0 )
93+ blocks = make ([]types. RecentBlock , 0 )
9494 for index := range reJSON {
95- block := RecentBlock {}
95+ block := types. RecentBlock {}
9696 err = json .Unmarshal ([]byte (reJSON [index ]), & block )
9797 if err != nil {
9898 return
@@ -110,7 +110,7 @@ type MempoolState struct {
110110 DataInBuckets []float64 `json:"dataInBuckets"`
111111}
112112
113- func GetHistorical (timeframe int , by string ) (hmds []HistoricalMempoolData , err error ) {
113+ func GetHistorical (timeframe int , by string ) (hmds []types. HistoricalMempoolData , err error ) {
114114 c := Pool .Get ()
115115 defer c .Close ()
116116
@@ -149,9 +149,9 @@ func GetHistorical(timeframe int, by string) (hmds []HistoricalMempoolData, err
149149 return
150150 }
151151
152- hmds = make ([]HistoricalMempoolData , 0 )
152+ hmds = make ([]types. HistoricalMempoolData , 0 )
153153 for index := range hmdsJSON {
154- hmd := HistoricalMempoolData {}
154+ hmd := types. HistoricalMempoolData {}
155155 err = json .Unmarshal ([]byte (hmdsJSON [index ]), & hmd )
156156 if err != nil {
157157 return
@@ -162,61 +162,102 @@ func GetHistorical(timeframe int, by string) (hmds []HistoricalMempoolData, err
162162 return
163163}
164164
165- // GetTimeInMempool gets the TimeInMempool data from the database
166- func GetTimeInMempool () (timestamp int64 , timeAxis [] int , feerateAxis [] float64 , err error ) {
165+ // GetTransactionStats gets the Transaction Stats data from the database
166+ func GetTransactionStats () (tss []types. TransactionStat , err error ) {
167167 c := Pool .Get ()
168168 defer c .Close ()
169169
170- prefix := "timeInMempool"
171-
172- response , err := redis .Strings (c .Do ("MGET" , prefix + ":timeAxis" , prefix + ":feerateAxis" , prefix + ":utcTimestamp" ))
170+ tssJSON , err := redis .Strings (c .Do ("LRANGE" , "transactionStats" , 0 , 180 ))
173171 if err != nil {
174172 return
175173 }
176174
177- err = json .Unmarshal ([]byte (response [0 ]), & timeAxis )
178- if err != nil {
179- return
175+ type transactionStat struct {
176+ SegwitCount int `json:"segwitCount"`
177+ RbfCount int `json:"rbfCount"`
178+ TxCount int `json:"txCount"`
179+ Timestamp int64 `json:"timestamp"`
180180 }
181181
182- err = json .Unmarshal ([]byte (response [1 ]), & feerateAxis )
183- if err != nil {
184- return
182+ tss = make ([]types.TransactionStat , 0 )
183+ for index := range tssJSON {
184+ ts := types.TransactionStat {}
185+ err = json .Unmarshal ([]byte (tssJSON [index ]), & ts )
186+ if err != nil {
187+ return
188+ }
189+ tss = append (tss , ts )
185190 }
186191
187- err = json .Unmarshal ([]byte (response [2 ]), & timestamp )
192+ return
193+ }
194+
195+ // GetMempoolEntries gets the last x mempool Entries from the database
196+ func GetMempoolEntries () (mes []types.MempoolEntry , err error ) {
197+ c := Pool .Get ()
198+ defer c .Close ()
199+
200+ // gets recent entries from 0 to 19999 (20k)
201+ mesJSON , err := redis .Strings (c .Do ("ZREVRANGE" , "mempoolEntries" , 0 , 29999 ))
188202 if err != nil {
189203 return
190204 }
191205
206+ mes = make ([]types.MempoolEntry , 0 )
207+ for index := range mesJSON {
208+ me := types.MempoolEntry {}
209+ err = json .Unmarshal ([]byte (mesJSON [index ]), & me )
210+ if err != nil {
211+ return
212+ }
213+ mes = append (mes , me )
214+ }
215+
192216 return
193217}
194218
195- // GetTransactionStats gets the Transaction Stats data from the database
196- func GetTransactionStats ( ) (tss [] TransactionStat , err error ) {
219+ // SetMempoolEntriesCache SETs the response of a recent GetMempoolEntries() as a cache
220+ func SetMempoolEntriesCache ( mesJSON string ) (err error ) {
197221 c := Pool .Get ()
198222 defer c .Close ()
199223
200- tssJSON , err := redis .Strings (c .Do ("LRANGE" , "transactionStats" , 0 , 180 ))
224+ _ , err = c .Do ("SET" , "cache:mempoolEntries" , mesJSON )
225+ if err != nil {
226+ return err
227+ }
228+ return nil
229+ }
230+
231+ // GetMempoolEntriesCache GET the cached response of a recent GetMempoolEntries() call
232+ func GetMempoolEntriesCache () (mesJSON string , err error ) {
233+ c := Pool .Get ()
234+ defer c .Close ()
235+
236+ mesJSON , err = redis .String (c .Do ("GET" , "cache:mempoolEntries" ))
201237 if err != nil {
202238 return
203239 }
240+ return
241+ }
204242
205- type transactionStat struct {
206- SegwitCount int `json:"segwitCount"`
207- RbfCount int `json:"rbfCount"`
208- TxCount int `json:"txCount"`
209- Timestamp int64 `json:"timestamp"`
243+ // GetRecentFeerateAPIEntries returns the recent feeRate API entrys from Redis
244+ func GetRecentFeerateAPIEntries () (entries []types.FeeRateAPIEntry , err error ) {
245+ c := Pool .Get ()
246+ defer c .Close ()
247+
248+ entrysJSON , err := redis .Strings (c .Do ("LRANGE" , "feerateAPIEntries" , 0 , 100 ))
249+ if err != nil {
250+ return
210251 }
211252
212- tss = make ([]TransactionStat , 0 )
213- for index := range tssJSON {
214- ts := TransactionStat {}
215- err = json .Unmarshal ([]byte (tssJSON [index ]), & ts )
253+ entries = make ([]types. FeeRateAPIEntry , 0 )
254+ for index := range entrysJSON {
255+ entry := types. FeeRateAPIEntry {}
256+ err = json .Unmarshal ([]byte (entrysJSON [index ]), & entry )
216257 if err != nil {
217258 return
218259 }
219- tss = append (tss , ts )
260+ entries = append (entries , entry )
220261 }
221262
222263 return
0 commit comments