Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func BenchmarkGetByPrimaryKey_RapidashWorst(b *testing.B) {
panic(err)
}
cache, err := New(
ServerAddrs([]string{"localhost:11211"}),
Servers(ServersConfig{Type: CacheServerTypeMemcached, Addrs: []string{"localhost:11211"}}),
SecondLevelCachePessimisticLock(false),
SecondLevelCacheOptimisticLock(false),
)
Expand Down Expand Up @@ -224,7 +224,7 @@ func BenchmarkGetByPrimaryKey_RapidashBest(b *testing.B) {
}
defer conn.Close()
cache, err := New(
ServerAddrs([]string{"localhost:11211"}),
Servers(ServersConfig{Type: CacheServerTypeMemcached, Addrs: []string{"localhost:11211"}}),
SecondLevelCachePessimisticLock(false),
SecondLevelCacheOptimisticLock(false),
)
Expand Down Expand Up @@ -336,7 +336,7 @@ func BenchmarkInsert_Rapidash(b *testing.B) {
}
defer conn.Close()
cache, err := New(
ServerAddrs([]string{"localhost:11211"}),
Servers(ServersConfig{Type: CacheServerTypeMemcached, Addrs: []string{"localhost:11211"}}),
SecondLevelCachePessimisticLock(false),
SecondLevelCacheOptimisticLock(false),
)
Expand Down Expand Up @@ -438,7 +438,7 @@ func BenchmarkUpdateByPrimaryKey_RapidashWorst(b *testing.B) {
}
defer conn.Close()
cache, err := New(
ServerAddrs([]string{"localhost:11211"}),
Servers(ServersConfig{Type: CacheServerTypeMemcached, Addrs: []string{"localhost:11211"}}),
SecondLevelCachePessimisticLock(false),
SecondLevelCacheOptimisticLock(false),
)
Expand Down Expand Up @@ -484,7 +484,7 @@ func BenchmarkUpdateByPrimaryKey_RapidashBest(b *testing.B) {
}
defer conn.Close()
cache, err := New(
ServerAddrs([]string{"localhost:11211"}),
Servers(ServersConfig{Type: CacheServerTypeMemcached, Addrs: []string{"localhost:11211"}}),
SecondLevelCachePessimisticLock(false),
SecondLevelCacheOptimisticLock(false),
)
Expand Down Expand Up @@ -598,7 +598,7 @@ func BenchmarkDeleteByPrimaryKey_Rapidash(b *testing.B) {
}
defer conn.Close()
cache, err := New(
ServerAddrs([]string{"localhost:11211"}),
Servers(ServersConfig{Type: CacheServerTypeMemcached, Addrs: []string{"localhost:11211"}}),
SecondLevelCachePessimisticLock(false),
SecondLevelCacheOptimisticLock(false),
)
Expand Down
38 changes: 26 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Config struct {
}

type RuleConfig struct {
Servers *[]string `yaml:"servers"`
Servers *ServersConfig `yaml:"servers"`
Logger *LoggerConfig `yaml:"logger"`
Retry *RetryConfig `yaml:"retry"`
CacheControl *CacheControlConfig `yaml:"cache_control"`
Expand All @@ -39,34 +39,44 @@ type CacheControlConfig struct {
}

type SLCConfig struct {
Servers *[]string `yaml:"servers"`
Servers *ServersConfig `yaml:"servers"`
Tables *map[string]*TableConfig `yaml:"tables"`
Expiration *time.Duration `yaml:"expiration"`
LockExpiration *time.Duration `yaml:"lock_expiration"`
}

type TableConfig struct {
ShardKey *string `yaml:"shard_key"`
Server *string `yaml:"server"`
Server *ServerConfig `yaml:"server"`
CacheControl *CacheControlConfig `yaml:"cache_control"`
Expiration *time.Duration `yaml:"expiration"`
LockExpiration *time.Duration `yaml:"lock_expiration"`
}

type LLCConfig struct {
Servers *[]string
Servers *ServersConfig `yaml:"servers"`
Tags *map[string]*TagConfig `yaml:"tags"`
CacheControl *CacheControlConfig `yaml:"cache_control"`
Expiration *time.Duration `yaml:"expiration"`
LockExpiration *time.Duration `yaml:"lock_expiration"`
}

type TagConfig struct {
Server *string `yaml:"server"`
Server *ServerConfig `yaml:"server"`
Expiration *time.Duration `yaml:"expiration"`
LockExpiration *time.Duration `yaml:"lock_expiration"`
}

type ServersConfig struct {
Type CacheServerType `yaml:"type"`
Addrs []string `yaml:"addrs"`
}

type ServerConfig struct {
Type CacheServerType `yaml:"type"`
Addr string `yaml:"addr"`
}

func NewConfig(path string) (*Config, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
Expand All @@ -82,15 +92,19 @@ func NewConfig(path string) (*Config, error) {
func (cfg *Config) Options() []OptionFunc {
opts := []OptionFunc{}
opts = append(opts, cfg.Rule.Options()...)
opts = append(opts, cfg.SLC.Options()...)
opts = append(opts, cfg.LLC.Options()...)
if cfg.SLC != nil {
opts = append(opts, cfg.SLC.Options()...)
}
if cfg.LLC != nil {
opts = append(opts, cfg.LLC.Options()...)
}
return opts
}

func (cfg *RuleConfig) Options() []OptionFunc {
opts := []OptionFunc{}
if cfg.Servers != nil {
opts = append(opts, ServerAddrs(*cfg.Servers))
opts = append(opts, Servers(*cfg.Servers))
}
if cfg.MaxIdleConnection != nil {
opts = append(opts, MaxIdleConnections(*cfg.MaxIdleConnection))
Expand Down Expand Up @@ -167,7 +181,7 @@ func (cfg *CacheControlConfig) TableOptions(table string) []OptionFunc {
func (cfg *SLCConfig) Options() []OptionFunc {
opts := []OptionFunc{}
if cfg.Servers != nil {
opts = append(opts, SecondLevelCacheServerAddrs(*cfg.Servers))
opts = append(opts, SecondLevelCacheServers(*cfg.Servers))
}
if cfg.Tables != nil {
for table, tableCfg := range *cfg.Tables {
Expand All @@ -189,7 +203,7 @@ func (cfg *TableConfig) Options(table string) []OptionFunc {
opts = append(opts, SecondLevelCacheTableShardKey(table, *cfg.ShardKey))
}
if cfg.Server != nil {
opts = append(opts, SecondLevelCacheTableServerAddr(table, *cfg.Server))
opts = append(opts, SecondLevelCacheTableServer(table, *cfg.Server))
}
if cfg.Expiration != nil {
opts = append(opts, SecondLevelCacheTableExpiration(table, *cfg.Expiration))
Expand All @@ -206,7 +220,7 @@ func (cfg *TableConfig) Options(table string) []OptionFunc {
func (cfg *LLCConfig) Options() []OptionFunc {
opts := []OptionFunc{}
if cfg.Servers != nil {
opts = append(opts, LastLevelCacheServerAddrs(*cfg.Servers))
opts = append(opts, LastLevelCacheServer(*cfg.Servers))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LastLevelCacheServers ?

}
if cfg.Tags != nil {
for tag, tagCfg := range *cfg.Tags {
Expand All @@ -228,7 +242,7 @@ func (cfg *LLCConfig) Options() []OptionFunc {
func (cfg *TagConfig) Options(tag string) []OptionFunc {
opts := []OptionFunc{}
if cfg.Server != nil {
opts = append(opts, LastLevelCacheTagServerAddr(tag, *cfg.Server))
opts = append(opts, LastLevelCacheTagServer(tag, *cfg.Server))
}
if cfg.Expiration != nil {
opts = append(opts, LastLevelCacheTagExpiration(tag, *cfg.Expiration))
Expand Down
129 changes: 121 additions & 8 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,30 @@ import (
func TestConfig(t *testing.T) {
cfg, err := NewConfig("testdata/cache.yml")
NoError(t, err)
testConfig(t, cfg)
}

func TestConfigWithSlcOptionAndLlcOption(t *testing.T) {
cfg, err := NewConfig("testdata/cache_use_slc_option_and_llc_option.yml")
NoError(t, err)
testConfig(t, cfg)
}

func TestConfigWithSlcTableOptionAndLlcTagOption(t *testing.T) {
cfg, err := NewConfig("testdata/cache_use_slc_table_option_and_llc_tag_option.yml")
NoError(t, err)
testConfig(t, cfg)
}

func testConfig(t *testing.T, cfg *Config) {
cache, err := New(cfg.Options()...)
NoError(t, err)
NoError(t, cache.Flush())
conn, err := sql.Open("mysql", "root:@tcp(localhost:3306)/rapidash?parseTime=true")
NoError(t, err)
NoError(t, cache.WarmUp(conn, userLoginType(), false))
t.Run("create new records", func(t *testing.T) {
NoError(t, cache.WarmUp(conn, userLogType(), false))
t.Run("create new records use redis cache when table option specify", func(t *testing.T) {
txConn, err := conn.Begin()
NoError(t, err)
tx, err := cache.Begin(txConn)
Expand All @@ -37,7 +54,7 @@ func TestConfig(t *testing.T) {
}
NoError(t, tx.Commit())
})
t.Run("get records", func(t *testing.T) {
t.Run("get records use redis cache when table option specify", func(t *testing.T) {
txConn, err := conn.Begin()
NoError(t, err)
tx, err := cache.Begin(txConn)
Expand All @@ -54,14 +71,110 @@ func TestConfig(t *testing.T) {
}
NoError(t, tx.Commit())
})
t.Run("last level cache", func(t *testing.T) {
t.Run("delete records use redis cache when table option specify", func(t *testing.T) {
txConn, err := conn.Begin()
NoError(t, err)
tx, err := cache.Begin(txConn)
NoError(t, err)
for i := 1001; i <= 1005; i++ {
builder := NewQueryBuilder("user_logins").
Eq("user_id", uint64(i)).
Eq("user_session_id", uint64(i))
NoError(t, tx.DeleteByQueryBuilder(builder))
}
NoError(t, tx.Commit())
})
t.Run("create new records use memcached cache when table option specify", func(t *testing.T) {
txConn, err := conn.Begin()
NoError(t, err)
tx, err := cache.Begin(txConn)
NoError(t, err)
now := time.Now()
for i := 1001; i <= 1005; i++ {
id := uint64(i)
if _, err := tx.CreateByTable("user_logs", &UserLog{
ID: id,
UserID: id,
ContentType: "web",
ContentID: id,
CreatedAt: &now,
UpdatedAt: &now,
}); err != nil {
t.Fatalf("%+v", err)
}
}
NoError(t, tx.Commit())
})
t.Run("get records use memcached cache when table option specify", func(t *testing.T) {
txConn, err := conn.Begin()
NoError(t, err)
tx, err := cache.Begin(txConn)
NoError(t, err)
for i := 1001; i <= 1005; i++ {
builder := NewQueryBuilder("user_logs").
Eq("id", uint64(i))
var foundUserLog UserLog
NoError(t, tx.FindByQueryBuilder(builder, &foundUserLog))
if foundUserLog.ID == 0 {
t.Fatal("cannot find record")
}
}
NoError(t, tx.Commit())
})
t.Run("delete records use memcached cache when table option specify", func(t *testing.T) {
txConn, err := conn.Begin()
NoError(t, err)
tx, err := cache.Begin(txConn)
NoError(t, err)
for i := 1001; i <= 1005; i++ {
builder := NewQueryBuilder("user_logs").
Eq("id", uint64(i))
NoError(t, tx.DeleteByQueryBuilder(builder))
}
NoError(t, tx.Commit())
})
t.Run("last level cache with tag use redis cache when tag option specify", func(t *testing.T) {
tx, err := cache.Begin()
NoError(t, err)
tag := "a"
for i := 1; i <= 10; i++ {
key := fmt.Sprintf("key_with_tag_%s_%d", tag, i)
NoError(t, tx.CreateWithTag(tag, key, Int(1)))
var a int
NoError(t, tx.FindWithTag(tag, key, IntPtr(&a)))
if a != 1 {
t.Fatal("cannot work set/get with tag")
}
}
NoError(t, tx.Commit())
})
t.Run("last level cache with tag use memcached cache when tag option specify", func(t *testing.T) {
tx, err := cache.Begin()
NoError(t, err)
NoError(t, tx.CreateWithTag("a", "key", Int(1)))
var a int
NoError(t, tx.FindWithTag("a", "key", IntPtr(&a)))
if a != 1 {
t.Fatal("cannot work set/get with tag")
tag := "b"
for i := 1; i <= 10; i++ {
key := fmt.Sprintf("key_with_tag_%s_%d", tag, i)
NoError(t, tx.CreateWithTag(tag, key, Int(1)))
var a int
NoError(t, tx.FindWithTag(tag, key, IntPtr(&a)))
if a != 1 {
t.Fatal("cannot work set/get with tag")
}
}
NoError(t, tx.Commit())
})
t.Run("last level cache without tag", func(t *testing.T) {
tx, err := cache.Begin()
NoError(t, err)
for i := 1; i <= 10; i++ {
key := fmt.Sprintf("key_with_not_tag_%d", i)
NoError(t, tx.Create(key, Int(1)))
var a int
NoError(t, tx.Find(key, IntPtr(&a)))
if a != 1 {
t.Fatal("cannot work set/get with tag")
}
}
NoError(t, tx.Commit())
})
}
Loading