From 824e444bc7151d9ed954f6e5b41ff2a2704fe9fa Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 10 Oct 2018 22:21:02 +0800 Subject: [PATCH] add redis cache ci tests --- .drone.yml | 17 +++++++++++++++++ interface.go | 1 + xorm_test.go | 27 +++++++++++++++++---------- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/.drone.yml b/.drone.yml index d30f1c1ea..d83363a56 100644 --- a/.drone.yml +++ b/.drone.yml @@ -25,6 +25,9 @@ services: when: event: [ push, tag, pull_request ] + redis: + image: redis:3.0 + #mssql: # image: microsoft/mssql-server-linux:2017-CU11 # environment: @@ -55,12 +58,21 @@ pipeline: psql -U postgres -d xorm_test -h pgsql \ -c "create schema xorm;" + init_redis: + image: redis:3.0 + commands: + - sleep 5 + - redis-cli -h redis ping + - redis-cli -h redis set HELLO hello + - redis-cli -h redis get HELLO + build: image: golang:${GO_VERSION} commands: - go get -t -d -v ./... - go get -u github.com/go-xorm/core - go get -u github.com/go-xorm/builder + - go get -u github.com/go-xorm/xorm-redis-cache - go build -v when: event: [ push, pull_request ] @@ -71,6 +83,7 @@ pipeline: - go get -u github.com/wadey/gocovmerge - go test -v -race -db="sqlite3" -conn_str="./test.db" -coverprofile=coverage1-1.txt -covermode=atomic - go test -v -race -db="sqlite3" -conn_str="./test.db" -cache=true -coverprofile=coverage1-2.txt -covermode=atomic + - go test -v -race -db="sqlite3" -conn_str="./test.db" -cache=true -cache_redis_server="redis:6379" -coverprofile=coverage1-2.txt -covermode=atomic when: event: [ push, pull_request ] @@ -79,6 +92,7 @@ pipeline: commands: - go test -v -race -db="mysql" -conn_str="root:@tcp(mysql)/xorm_test" -coverprofile=coverage2-1.txt -covermode=atomic - go test -v -race -db="mysql" -conn_str="root:@tcp(mysql)/xorm_test" -cache=true -coverprofile=coverage2-2.txt -covermode=atomic + - go test -v -race -db="mysql" -conn_str="root:@tcp(mysql)/xorm_test" -cache=true -cache_redis_server="redis:6379" -coverprofile=coverage2-2.txt -covermode=atomic when: event: [ push, pull_request ] @@ -87,6 +101,7 @@ pipeline: commands: - go test -v -race -db="mymysql" -conn_str="tcp:mysql:3306*xorm_test/root/" -coverprofile=coverage3-1.txt -covermode=atomic - go test -v -race -db="mymysql" -conn_str="tcp:mysql:3306*xorm_test/root/" -cache=true -coverprofile=coverage3-2.txt -covermode=atomic + - go test -v -race -db="mymysql" -conn_str="tcp:mysql:3306*xorm_test/root/" -cache=true -cache_redis_server="redis:6379" -coverprofile=coverage3-2.txt -covermode=atomic when: event: [ push, pull_request ] @@ -95,6 +110,7 @@ pipeline: commands: - go test -v -race -db="postgres" -conn_str="postgres://postgres:@pgsql/xorm_test?sslmode=disable" -coverprofile=coverage4-1.txt -covermode=atomic - go test -v -race -db="postgres" -conn_str="postgres://postgres:@pgsql/xorm_test?sslmode=disable" -cache=true -coverprofile=coverage4-2.txt -covermode=atomic + - go test -v -race -db="postgres" -conn_str="postgres://postgres:@pgsql/xorm_test?sslmode=disable" -cache=true -cache_redis_server="redis:6379" -coverprofile=coverage4-2.txt -covermode=atomic when: event: [ push, pull_request ] @@ -103,6 +119,7 @@ pipeline: commands: - go test -v -race -db="postgres" -conn_str="postgres://postgres:@pgsql/xorm_test?sslmode=disable" -schema=xorm -coverprofile=coverage5-1.txt -covermode=atomic - go test -v -race -db="postgres" -conn_str="postgres://postgres:@pgsql/xorm_test?sslmode=disable" -schema=xorm -cache=true -coverprofile=coverage5-2.txt -covermode=atomic + - go test -v -race -db="postgres" -conn_str="postgres://postgres:@pgsql/xorm_test?sslmode=disable" -schema=xorm -cache=true -cache_redis_server="redis:6379" -coverprofile=coverage5-2.txt -covermode=atomic - gocovmerge coverage1-1.txt coverage1-2.txt coverage2-1.txt coverage2-2.txt coverage3-1.txt coverage3-2.txt coverage4-1.txt coverage4-2.txt coverage5-1.txt coverage5-2.txt > coverage.txt when: event: [ push, pull_request ] diff --git a/interface.go b/interface.go index 33d2078e4..575ab7741 100644 --- a/interface.go +++ b/interface.go @@ -84,6 +84,7 @@ type EngineInterface interface { GetTableMapper() core.IMapper GetTZDatabase() *time.Location GetTZLocation() *time.Location + Logger() core.ILogger MapCacher(interface{}, core.Cacher) error NewSession() *Session NoAutoTime() *Session diff --git a/xorm_test.go b/xorm_test.go index 4e88dc408..29c2a51b5 100644 --- a/xorm_test.go +++ b/xorm_test.go @@ -10,6 +10,7 @@ import ( _ "github.com/denisenkom/go-mssqldb" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/core" + "github.com/go-xorm/xorm-redis-cache" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" _ "github.com/ziutek/mymysql/godrv" @@ -20,14 +21,15 @@ var ( dbType string connString string - db = flag.String("db", "sqlite3", "the tested database") - showSQL = flag.Bool("show_sql", true, "show generated SQLs") - ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string") - mapType = flag.String("map_type", "snake", "indicate the name mapping") - cache = flag.Bool("cache", false, "if enable cache") - cluster = flag.Bool("cluster", false, "if this is a cluster") - splitter = flag.String("splitter", ";", "the splitter on connstr for cluster") - schema = flag.String("schema", "", "specify the schema") + db = flag.String("db", "sqlite3", "the tested database") + showSQL = flag.Bool("show_sql", true, "show generated SQLs") + ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string") + mapType = flag.String("map_type", "snake", "indicate the name mapping") + cache = flag.Bool("cache", false, "if enable cache") + cacheRedisServer = flag.String("cache_redis_server", "127.0.0.1:6379", "if cache enabled this will enable redis cache mode") + cluster = flag.Bool("cluster", false, "if this is a cluster") + splitter = flag.String("splitter", ";", "the splitter on connstr for cluster") + schema = flag.String("schema", "", "specify the schema") ) func createEngine(dbType, connStr string) error { @@ -49,8 +51,13 @@ func createEngine(dbType, connStr string) error { testEngine.ShowSQL(*showSQL) testEngine.SetLogLevel(core.LOG_DEBUG) if *cache { - cacher := NewLRUCacher(NewMemoryStore(), 100000) - testEngine.SetDefaultCacher(cacher) + if *cacheRedisServer == "" { + cacher := NewLRUCacher(NewMemoryStore(), 100000) + testEngine.SetDefaultCacher(cacher) + } else { + cacher := xormrediscache.NewRedisCacher(*cacheRedisServer, "", xormrediscache.DEFAULT_EXPIRATION, testEngine.Logger()) + testEngine.SetDefaultCacher(cacher) + } } if len(*mapType) > 0 {