Use a separate cache db when running tests.

This commit is contained in:
mikestefanello 2022-02-06 10:07:25 -05:00
parent a8bd9f8b2d
commit 5def458946
5 changed files with 41 additions and 15 deletions

View file

@ -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: 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
**Set data with just a key:** **Set data with just a key:**

View file

@ -81,10 +81,12 @@ type (
// CacheConfig stores the cache configuration // CacheConfig stores the cache configuration
CacheConfig struct { CacheConfig struct {
Hostname string `env:"CACHE_HOSTNAME,default=localhost"` Hostname string `env:"CACHE_HOSTNAME,default=localhost"`
Port uint16 `env:"CACHE_PORT,default=6379"` Port uint16 `env:"CACHE_PORT,default=6379"`
Password string `env:"CACHE_PASSWORD"` Password string `env:"CACHE_PASSWORD"`
Expiration struct { 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"` StaticFile time.Duration `env:"CACHE_EXPIRATION_STATIC_FILE,default=4380h"`
Page time.Duration `env:"CACHE_EXPIRATION_PAGE,default=24h"` Page time.Duration `env:"CACHE_EXPIRATION_PAGE,default=24h"`
} }

View file

@ -51,16 +51,31 @@ type (
) )
// NewCacheClient creates a new cache client // 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 := &CacheClient{}
c.Client = redis.NewClient(&redis.Options{ c.Client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port), Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port),
Password: cfg.Password, Password: cfg.Cache.Password,
DB: db,
}) })
if _, err := c.Client.Ping(context.Background()).Result(); err != nil { if _, err := c.Client.Ping(context.Background()).Result(); err != nil {
return c, err 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) cacheStore := store.NewRedis(c.Client, nil)
c.cache = cache.New(cacheStore) c.cache = cache.New(cacheStore)
return c, nil return c, nil

View file

@ -69,6 +69,9 @@ func NewContainer() *Container {
// Shutdown shuts the Container down and disconnects all connections // Shutdown shuts the Container down and disconnects all connections
func (c *Container) Shutdown() error { func (c *Container) Shutdown() error {
if err := c.Tasks.Close(); err != nil {
return err
}
if err := c.Cache.Close(); err != nil { if err := c.Cache.Close(); err != nil {
return err return err
} }
@ -78,9 +81,6 @@ func (c *Container) Shutdown() error {
if err := c.Database.Close(); err != nil { if err := c.Database.Close(); err != nil {
return err return err
} }
if err := c.Tasks.Close(); err != nil {
return err
}
return nil return nil
} }
@ -117,7 +117,7 @@ func (c *Container) initWeb() {
// initCache initializes the cache // initCache initializes the cache
func (c *Container) initCache() { func (c *Container) initCache() {
var err error var err error
if c.Cache, err = NewCacheClient(c.Config.Cache); err != nil { if c.Cache, err = NewCacheClient(c.Config); err != nil {
panic(err) panic(err)
} }
} }
@ -192,5 +192,5 @@ func (c *Container) initMail() {
// initTasks initializes the task client // initTasks initializes the task client
func (c *Container) initTasks() { func (c *Container) initTasks() {
c.Tasks = NewTaskClient(c.Config.Cache) c.Tasks = NewTaskClient(c.Config)
} }

View file

@ -36,10 +36,17 @@ type (
) )
// NewTaskClient creates a new task client // 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{ conn := asynq.RedisClientOpt{
Addr: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port), Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port),
Password: cfg.Password, Password: cfg.Cache.Password,
DB: db,
} }
return &TaskClient{ return &TaskClient{