Skip to content

Commit

Permalink
Redis add Memory driver
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Feb 8, 2023
1 parent 5d9bf95 commit 478d992
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 104 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Golang developers quickly build their own applications.

- [ ] Custom .env path
- [ ] Database read-write separation
- [ ] Extend Redis driver

## Documentation

Expand Down
1 change: 0 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框

- [ ] 自定义 .env 路径
- [ ] 数据库读写分离
- [ ] 扩展 Redis 驱动

## 文档

Expand Down
10 changes: 10 additions & 0 deletions cache/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ func (app *Application) Init() cache.Store {
return redis
}

if driver == "memory" {
memory, err := NewMemory()
if err != nil {
color.Redf("[Cache] Init memory driver error: %v\n", err)
return nil
}

return memory
}

if driver == "custom" {
if custom, ok := facades.Config.Get("cache.stores." + defaultStore + ".via").(cache.Store); ok {
return custom
Expand Down
227 changes: 135 additions & 92 deletions cache/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ import (

type ApplicationTestSuite struct {
suite.Suite
stores []cache.Store
stores map[string]cache.Store
redisDocker *dockertest.Resource
}

func TestApplicationTestSuite(t *testing.T) {
redisPool, redisDocker, redisStore, err := getRedisDocker()
if err != nil {
log.Fatalf("Get redis error: %s", err)
log.Fatalf("Get redis store error: %s", err)
}
memoryStore, err := getMemoryStore()
if err != nil {
log.Fatalf("Get memory store error: %s", err)
}

suite.Run(t, &ApplicationTestSuite{
stores: []cache.Store{
redisStore,
stores: map[string]cache.Store{
"redis": redisStore,
"memory": memoryStore,
},
redisDocker: redisDocker,
})
Expand Down Expand Up @@ -88,145 +93,171 @@ func (s *ApplicationTestSuite) TestInitRedis() {
}

func (s *ApplicationTestSuite) TestAdd() {
for _, store := range s.stores {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.False(store.Add("name", "World", 1*time.Second))
s.True(store.Add("name1", "World", 1*time.Second))
s.True(store.Has("name1"))
time.Sleep(2 * time.Second)
s.False(store.Has("name1"))
s.True(store.Flush())
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.False(store.Add("name", "World", 1*time.Second))
s.True(store.Add("name1", "World", 1*time.Second))
s.True(store.Has("name1"))
time.Sleep(2 * time.Second)
s.False(store.Has("name1"))
s.True(store.Flush())
})
}
}

func (s *ApplicationTestSuite) TestForever() {
for _, store := range s.stores {
s.True(store.Forever("name", "Goravel"))
s.Equal("Goravel", store.Get("name", "").(string))
s.True(store.Flush())
for name, store := range s.stores {
s.Run(name, func() {
s.True(store.Forever("name", "Goravel"))
s.Equal("Goravel", store.Get("name", "").(string))
s.True(store.Flush())
})
}
}

func (s *ApplicationTestSuite) TestForget() {
for _, store := range s.stores {
val := store.Forget("test-forget")
s.True(val)

err := store.Put("test-forget", "goravel", 5*time.Second)
s.Nil(err)
s.True(store.Forget("test-forget"))
for name, store := range s.stores {
s.Run(name, func() {
val := store.Forget("test-forget")
s.True(val)

err := store.Put("test-forget", "goravel", 5*time.Second)
s.Nil(err)
s.True(store.Forget("test-forget"))
})
}
}

func (s *ApplicationTestSuite) TestFlush() {
for _, store := range s.stores {
s.Nil(store.Put("test-flush", "goravel", 5*time.Second))
s.Equal("goravel", store.Get("test-flush", nil).(string))
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("test-flush", "goravel", 5*time.Second))
s.Equal("goravel", store.Get("test-flush", nil).(string))

s.True(store.Flush())
s.False(store.Has("test-flush"))
s.True(store.Flush())
s.False(store.Has("test-flush"))
})
}
}

func (s *ApplicationTestSuite) TestGet() {
for _, store := range s.stores {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.Equal("Goravel", store.Get("name", "").(string))
s.Equal("World", store.Get("name1", "World").(string))
s.Equal("World1", store.Get("name2", func() any {
return "World1"
}).(string))
s.True(store.Forget("name"))
s.True(store.Flush())
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.Equal("Goravel", store.Get("name", "").(string))
s.Equal("World", store.Get("name1", "World").(string))
s.Equal("World1", store.Get("name2", func() any {
return "World1"
}).(string))
s.True(store.Forget("name"))
s.True(store.Flush())
})
}
}

func (s *ApplicationTestSuite) TestGetBool() {
for _, store := range s.stores {
s.Equal(true, store.GetBool("test-get-bool", true))
s.Nil(store.Put("test-get-bool", true, 2*time.Second))
s.Equal(true, store.GetBool("test-get-bool", false))
for name, store := range s.stores {
s.Run(name, func() {
s.Equal(true, store.GetBool("test-get-bool", true))
s.Nil(store.Put("test-get-bool", true, 2*time.Second))
s.Equal(true, store.GetBool("test-get-bool", false))
})
}
}

func (s *ApplicationTestSuite) TestGetInt() {
for _, store := range s.stores {
s.Equal(2, store.GetInt("test-get-int", 2))
s.Nil(store.Put("test-get-int", 3, 2*time.Second))
s.Equal(3, store.GetInt("test-get-int", 2))
for name, store := range s.stores {
s.Run(name, func() {
s.Equal(2, store.GetInt("test-get-int", 2))
s.Nil(store.Put("test-get-int", 3, 2*time.Second))
s.Equal(3, store.GetInt("test-get-int", 2))
})
}
}

func (s *ApplicationTestSuite) TestGetString() {
for _, store := range s.stores {
s.Equal("2", store.GetString("test-get-string", "2"))
s.Nil(store.Put("test-get-string", "3", 2*time.Second))
s.Equal("3", store.GetString("test-get-string", "2"))
for name, store := range s.stores {
s.Run(name, func() {
s.Equal("2", store.GetString("test-get-string", "2"))
s.Nil(store.Put("test-get-string", "3", 2*time.Second))
s.Equal("3", store.GetString("test-get-string", "2"))
})
}
}

func (s *ApplicationTestSuite) TestHas() {
for _, store := range s.stores {
s.False(store.Has("test-has"))
s.Nil(store.Put("test-has", "goravel", 5*time.Second))
s.True(store.Has("test-has"))
for name, store := range s.stores {
s.Run(name, func() {
s.False(store.Has("test-has"))
s.Nil(store.Put("test-has", "goravel", 5*time.Second))
s.True(store.Has("test-has"))
})
}
}

func (s *ApplicationTestSuite) TestPull() {
for _, store := range s.stores {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.True(store.Has("name"))
s.Equal("Goravel", store.Pull("name", "").(string))
s.False(store.Has("name"))
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.True(store.Has("name"))
s.Equal("Goravel", store.Pull("name", "").(string))
s.False(store.Has("name"))
})
}
}

func (s *ApplicationTestSuite) TestPut() {
for _, store := range s.stores {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.True(store.Has("name"))
s.Equal("Goravel", store.Get("name", "").(string))
time.Sleep(2 * time.Second)
s.False(store.Has("name"))
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
s.True(store.Has("name"))
s.Equal("Goravel", store.Get("name", "").(string))
time.Sleep(2 * time.Second)
s.False(store.Has("name"))
})
}
}

func (s *ApplicationTestSuite) TestRemember() {
for _, store := range s.stores {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
value, err := store.Remember("name", 1*time.Second, func() any {
return "World"
})
s.Nil(err)
s.Equal("Goravel", value)

value, err = store.Remember("name1", 1*time.Second, func() any {
return "World1"
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
value, err := store.Remember("name", 1*time.Second, func() any {
return "World"
})
s.Nil(err)
s.Equal("Goravel", value)

value, err = store.Remember("name1", 1*time.Second, func() any {
return "World1"
})
s.Nil(err)
s.Equal("World1", value)
time.Sleep(2 * time.Second)
s.False(store.Has("name1"))
s.True(store.Flush())
})
s.Nil(err)
s.Equal("World1", value)
time.Sleep(2 * time.Second)
s.False(store.Has("name1"))
s.True(store.Flush())
}
}

func (s *ApplicationTestSuite) TestRememberForever() {
for _, store := range s.stores {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
value, err := store.RememberForever("name", func() any {
return "World"
for name, store := range s.stores {
s.Run(name, func() {
s.Nil(store.Put("name", "Goravel", 1*time.Second))
value, err := store.RememberForever("name", func() any {
return "World"
})
s.Nil(err)
s.Equal("Goravel", value)

value, err = store.RememberForever("name1", func() any {
return "World1"
})
s.Nil(err)
s.Equal("World1", value)
s.True(store.Flush())
})
s.Nil(err)
s.Equal("Goravel", value)

value, err = store.RememberForever("name1", func() any {
return "World1"
})
s.Nil(err)
s.Equal("World1", value)
s.True(store.Flush())
}
}

Expand Down Expand Up @@ -271,6 +302,18 @@ func getRedisDocker() (*dockertest.Pool, *dockertest.Resource, cache.Store, erro
return pool, resource, store, nil
}

func getMemoryStore() (*Memory, error) {
mockConfig := mock.Config()
mockConfig.On("GetString", "cache.prefix").Return("goravel_cache").Once()

memory, err := NewMemory()
if err != nil {
return nil, err
}

return memory, nil
}

type Store struct {
}

Expand Down
Loading

0 comments on commit 478d992

Please sign in to comment.