Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanvc committed Jan 16, 2024
1 parent 97f16df commit b5447dd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 3 additions & 2 deletions evohttp/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ func NewRouter() *Router {

func (r *Router) Find(httpMethod, urlPath string, params map[string]string) *RouteNode {
var mn *MethodRouteNode
for _, mn = range r.methodNodes {
if mn.HttpMethod == httpMethod {
for _, n := range r.methodNodes {
if n.HttpMethod == httpMethod {
mn = n
break
}
}
Expand Down
13 changes: 13 additions & 0 deletions evohttp/router_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package evohttp

import (
"context"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

Expand All @@ -24,3 +26,14 @@ func Test_getCommonPrefixLength(t *testing.T) {
require.Equal(t, 2, getCommonPrefixLength("nihao", "ni"))
require.Equal(t, 0, getCommonPrefixLength("nihao", "0"))
}

func TestRouter_Find(t *testing.T) {
b := NewRouterBuilder()
b.POST("/api/abc", NewStdHandlerF(
func(ctx context.Context, req *int) (*int, error) { return nil, nil }))
params := make(map[string]string)
n := b.router.Find(http.MethodGet, "/api/abc", params)
require.Nil(t, n)
n = b.router.Find(http.MethodPost, "/api/abc", params)
require.Equal(t, "/api/abc", n.FullPath)
}
15 changes: 14 additions & 1 deletion examples/metricapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/ethanvc/evo/base"
"github.com/ethanvc/evo/evohttp"
"github.com/ethanvc/evo/examples/metricapp/rediskey"
"github.com/ethanvc/evo/plog"
"github.com/ethanvc/evo/rediscli"
"github.com/redis/go-redis/v9"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (controller *userController) queryUserFromCache(c context.Context, req *Que
c, cancel := context.WithTimeoutCause(c, time.Millisecond*100,
base.New(codes.DeadlineExceeded, "GetFromRedisTimeout").Err())
defer cancel()
err = controller.redisCli.Get(c, fmt.Sprintf("a_%d", req.Uid), resp)
err = controller.redisCli.Get(c, rediskey.UserCacheKey(req.Uid), resp)
switch err {
case redis.Nil:
return nil, base.New(codes.NotFound, "UserNotFoundInCache").Err()
Expand Down Expand Up @@ -134,6 +135,18 @@ func (controller *userController) queryUserFromDb(c context.Context, req *QueryU
}
}

func (controller *userController) saveUserToCache(c context.Context, user *UserDto) (err error) {
c = plog.WithLogContext(c, nil)
defer func() { plog.RequestLog(c, err, user, nil) }()
err = controller.redisCli.Set(c, rediskey.UserCacheKey(user.Uid), user, 0)
switch err {
case nil:
return
default:
return errors.Join(base.New(codes.Unknown, "SaveUserToRedisErr").Err(), err)
}
}

func NewGormDb() (*gorm.DB, error) {
dsn := "root:@tcp(127.0.0.1:3306)/test_db?charset=utf8mb4"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
Expand Down
7 changes: 7 additions & 0 deletions examples/metricapp/rediskey/rediskey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rediskey

import "fmt"

func UserCacheKey(uid int64) string {
return fmt.Sprintf("a/%d", uid)
}

0 comments on commit b5447dd

Please sign in to comment.