diff --git a/client_options_test.go b/client_options_test.go index 15e73656..42cb2934 100644 --- a/client_options_test.go +++ b/client_options_test.go @@ -5,8 +5,10 @@ import ( "testing" "github.com/BuxOrg/bux/cachestore" + "github.com/BuxOrg/bux/datastore" "github.com/BuxOrg/bux/taskmanager" "github.com/BuxOrg/bux/tester" + "github.com/coocood/freecache" "github.com/go-redis/redis/v8" "github.com/newrelic/go-agent/v3/newrelic" "github.com/stretchr/testify/assert" @@ -237,6 +239,58 @@ func TestWithRedis(t *testing.T) { }) } +// TestWithFreeCache will test the method WithFreeCache() +func TestWithFreeCache(t *testing.T) { + t.Parallel() + + t.Run("using FreeCache", func(t *testing.T) { + tc, err := NewClient( + tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), + WithFreeCache(), + WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory), + WithSQLite(&datastore.SQLiteConfig{Shared: true})) + require.NoError(t, err) + require.NotNil(t, tc) + defer CloseClient(context.Background(), t, tc) + + cs := tc.Cachestore() + require.NotNil(t, cs) + assert.Equal(t, cachestore.FreeCache, cs.Engine()) + }) +} + +// TestWithFreeCacheConnection will test the method WithFreeCacheConnection() +func TestWithMcacheConnection(t *testing.T) { + t.Parallel() + + t.Run("using a nil client", func(t *testing.T) { + tc, err := NewClient( + tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), + WithFreeCacheConnection(nil), + WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory), + WithSQLite(&datastore.SQLiteConfig{Shared: true})) + require.Error(t, err) + require.Nil(t, tc) + }) + + t.Run("using an existing connection", func(t *testing.T) { + fc := freecache.NewCache(cachestore.DefaultCacheSize) + + tc, err := NewClient( + tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), + WithFreeCacheConnection(fc), + WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory), + WithSQLite(&datastore.SQLiteConfig{Shared: true})) + require.NoError(t, err) + require.NotNil(t, tc) + defer CloseClient(context.Background(), t, tc) + + cs := tc.Cachestore() + require.NotNil(t, cs) + assert.Equal(t, cachestore.FreeCache, cs.Engine()) + }) +} + // TestWithRedisConnection will test the method WithRedisConnection() func TestWithRedisConnection(t *testing.T) { // finish test diff --git a/paymail.go b/paymail.go index 7b10fe57..e17b91c8 100644 --- a/paymail.go +++ b/paymail.go @@ -41,7 +41,7 @@ func getCapabilities(ctx context.Context, cs cachestore.ClientInterface, client } // Save to cachestore - if cs != nil { + if cs != nil && !cs.Engine().IsEmpty() { go func() { _ = cs.SetModel( context.Background(), cacheKeyCapabilities+domain, @@ -104,7 +104,7 @@ func resolvePaymailAddress(ctx context.Context, cs cachestore.ClientInterface, c } // Save to cachestore - if cs != nil { + if cs != nil && !cs.Engine().IsEmpty() { go func() { _ = cs.SetModel( ctx, cacheKeyAddressResolution+alias+"-"+domain,