Use a separate cache db when running tests.
This commit is contained in:
parent
c43f62a570
commit
156e578dd0
@ -910,6 +910,8 @@ The cache functionality within the `CacheClient` is powered by [gocache](https:/
|
||||
|
||||
The built-in usage of the cache is currently only for optional [page caching](#cached-responses) but it can be used for practically anything. See examples below:
|
||||
|
||||
Similar to how there is a separate [test database](#separate-test-database) to avoid writing to your primary database when running tests, the cache supports a separate database as well for tests. Within the `config`, the test database number can be specified at `Config.Cache.TestDatabase`. By default, the primary database is `0` and the test database is `1`.
|
||||
|
||||
### Set data
|
||||
|
||||
**Set data with just a key:**
|
||||
|
@ -81,10 +81,12 @@ type (
|
||||
|
||||
// CacheConfig stores the cache configuration
|
||||
CacheConfig struct {
|
||||
Hostname string `env:"CACHE_HOSTNAME,default=localhost"`
|
||||
Port uint16 `env:"CACHE_PORT,default=6379"`
|
||||
Password string `env:"CACHE_PASSWORD"`
|
||||
Expiration struct {
|
||||
Hostname string `env:"CACHE_HOSTNAME,default=localhost"`
|
||||
Port uint16 `env:"CACHE_PORT,default=6379"`
|
||||
Password string `env:"CACHE_PASSWORD"`
|
||||
Database int `env:"CACHE_DB,default=0"`
|
||||
TestDatabase int `env:"CACHE_DB_TEST,default=1"`
|
||||
Expiration struct {
|
||||
StaticFile time.Duration `env:"CACHE_EXPIRATION_STATIC_FILE,default=4380h"`
|
||||
Page time.Duration `env:"CACHE_EXPIRATION_PAGE,default=24h"`
|
||||
}
|
||||
|
@ -51,16 +51,31 @@ type (
|
||||
)
|
||||
|
||||
// NewCacheClient creates a new cache client
|
||||
func NewCacheClient(cfg config.CacheConfig) (*CacheClient, error) {
|
||||
func NewCacheClient(cfg *config.Config) (*CacheClient, error) {
|
||||
// Determine the database based on the environment
|
||||
db := cfg.Cache.Database
|
||||
if cfg.App.Environment == config.EnvTest {
|
||||
db = cfg.Cache.TestDatabase
|
||||
}
|
||||
|
||||
// Connect to the cache
|
||||
c := &CacheClient{}
|
||||
c.Client = redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port),
|
||||
Password: cfg.Password,
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port),
|
||||
Password: cfg.Cache.Password,
|
||||
DB: db,
|
||||
})
|
||||
if _, err := c.Client.Ping(context.Background()).Result(); err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
// Flush the database if this is the test environment
|
||||
if cfg.App.Environment == config.EnvTest {
|
||||
if err := c.Client.FlushDB(context.Background()).Err(); err != nil {
|
||||
return c, err
|
||||
}
|
||||
}
|
||||
|
||||
cacheStore := store.NewRedis(c.Client, nil)
|
||||
c.cache = cache.New(cacheStore)
|
||||
return c, nil
|
||||
|
@ -69,6 +69,9 @@ func NewContainer() *Container {
|
||||
|
||||
// Shutdown shuts the Container down and disconnects all connections
|
||||
func (c *Container) Shutdown() error {
|
||||
if err := c.Tasks.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.Cache.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -78,9 +81,6 @@ func (c *Container) Shutdown() error {
|
||||
if err := c.Database.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.Tasks.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -117,7 +117,7 @@ func (c *Container) initWeb() {
|
||||
// initCache initializes the cache
|
||||
func (c *Container) initCache() {
|
||||
var err error
|
||||
if c.Cache, err = NewCacheClient(c.Config.Cache); err != nil {
|
||||
if c.Cache, err = NewCacheClient(c.Config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -192,5 +192,5 @@ func (c *Container) initMail() {
|
||||
|
||||
// initTasks initializes the task client
|
||||
func (c *Container) initTasks() {
|
||||
c.Tasks = NewTaskClient(c.Config.Cache)
|
||||
c.Tasks = NewTaskClient(c.Config)
|
||||
}
|
||||
|
@ -36,10 +36,17 @@ type (
|
||||
)
|
||||
|
||||
// NewTaskClient creates a new task client
|
||||
func NewTaskClient(cfg config.CacheConfig) *TaskClient {
|
||||
func NewTaskClient(cfg *config.Config) *TaskClient {
|
||||
// Determine the database based on the environment
|
||||
db := cfg.Cache.Database
|
||||
if cfg.App.Environment == config.EnvTest {
|
||||
db = cfg.Cache.TestDatabase
|
||||
}
|
||||
|
||||
conn := asynq.RedisClientOpt{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port),
|
||||
Password: cfg.Password,
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port),
|
||||
Password: cfg.Cache.Password,
|
||||
DB: db,
|
||||
}
|
||||
|
||||
return &TaskClient{
|
||||
|
Loading…
Reference in New Issue
Block a user