New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache lowest/highest value (GET /info Only) #22

Merged
merged 3 commits into from Oct 20, 2018
Jump to file or symbol
Failed to load files and symbols.
+55 −4
Diff settings

Always

Just for now

@@ -96,6 +96,7 @@ func (h *Handler) InternalInitialize(w http.ResponseWriter, r *http.Request, _ h
model.DemarkRunTrade()
model.InitTcMap(h.db)
model.InitUserCache()
model.InitCaches()
if err != nil {
h.handleError(w, err, 500)
@@ -270,24 +271,24 @@ func (h *Handler) Info(w http.ResponseWriter, r *http.Request, _ httprouter.Para
return
}
lowestSellOrder, err := model.GetLowestSellOrder(h.db)
lowestSellOrderPrice, err := model.GetLowestSellPriceOnly(h.db)
switch {
case err == sql.ErrNoRows:
case err != nil:
h.handleError(w, errors.Wrap(err, "model.GetLowestSellOrder"), 500)
return
default:
res["lowest_sell_price"] = lowestSellOrder.Price
res["lowest_sell_price"] = lowestSellOrderPrice
}
highestBuyOrder, err := model.GetHighestBuyOrder(h.db)
highestBuyOrderPrice, err := model.GetHighestBuyPriceOnly(h.db)
switch {
case err == sql.ErrNoRows:
case err != nil:
h.handleError(w, errors.Wrap(err, "model.GetHighestBuyOrder"), 500)
return
default:
res["highest_buy_price"] = highestBuyOrder.Price
res["highest_buy_price"] = highestBuyOrderPrice
}
// TODO: trueにするとシェアボタンが有効になるが、アクセスが増えてヤバイので一旦falseにしておく
res["enable_share"] = true
@@ -58,6 +58,55 @@ func getOrderByIDWithLock(tx *sql.Tx, id int64) (*Order, error) {
return scanOrder(tx.Query("SELECT * FROM orders WHERE id = ? FOR UPDATE", id))
}
type CachedInt64 struct {
lastUpdated time.Time
val int64
}
var (
lowestPrice CachedInt64
highestPrice CachedInt64
)
func InitCaches() {
lowestPrice.lastUpdated = time.Date(2000, 1, 1, 1, 1, 1, 0, time.Local)
highestPrice.lastUpdated = time.Date(2000, 1, 1, 1, 1, 1, 0, time.Local)
}
func GetLowestSellPriceOnly(d QueryExecutor) (int64, error) {
curTime := time.Now()
if lowestPrice.lastUpdated.Add(time.Millisecond * 300).After(curTime) {
return lowestPrice.val, nil
}
order, err := GetLowestSellOrder(d)
if err != nil {
return -1, nil
}
lowestPrice.lastUpdated = curTime
lowestPrice.val = order.Price
return lowestPrice.val, nil
}
func GetHighestBuyPriceOnly(d QueryExecutor) (int64, error) {
curTime := time.Now()
if highestPrice.lastUpdated.Add(time.Millisecond * 300).After(curTime) {
return highestPrice.val, nil
}
order, err := GetHighestBuyOrder(d)
if err != nil {
return -1, nil
}
highestPrice.lastUpdated = curTime
highestPrice.val = order.Price
return highestPrice.val, nil
}
func GetLowestSellOrder(d QueryExecutor) (*Order, error) {
return scanOrder(d.Query("SELECT * FROM orders WHERE type = ? AND closed_at IS NULL ORDER BY price ASC, created_at ASC LIMIT 1", OrderTypeSell))
}
@@ -62,6 +62,7 @@ func main() {
h := controller.NewHandler(db, store)
model.InitTcMap(db)
model.StartRunTradeGoRoutine(db)
model.InitCaches()
router := httprouter.New()
router.POST("/initialize", h.Initialize)
ProTip! Use n and p to navigate between commits in a pull request.