github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_common/cache_control.go (about) 1 package db_common 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/jackc/pgx/v5" 9 "github.com/turbot/steampipe/pkg/constants" 10 ) 11 12 // SetCacheTtl set the cache ttl on the client 13 func SetCacheTtl(ctx context.Context, duration time.Duration, connection *pgx.Conn) error { 14 duration = duration.Truncate(time.Second) 15 seconds := fmt.Sprint(duration.Seconds()) 16 return executeCacheTtlSetFunction(ctx, seconds, connection) 17 } 18 19 // CacheClear resets the max time on the cache 20 // anything below this is not accepted 21 func CacheClear(ctx context.Context, connection *pgx.Conn) error { 22 return executeCacheSetFunction(ctx, "clear", connection) 23 } 24 25 // SetCacheEnabled enables/disables the cache 26 func SetCacheEnabled(ctx context.Context, enabled bool, connection *pgx.Conn) error { 27 value := "off" 28 if enabled { 29 value = "on" 30 } 31 return executeCacheSetFunction(ctx, value, connection) 32 } 33 34 func executeCacheSetFunction(ctx context.Context, settingValue string, connection *pgx.Conn) error { 35 return ExecuteSystemClientCall(ctx, connection, func(ctx context.Context, tx pgx.Tx) error { 36 _, err := tx.Exec(ctx, fmt.Sprintf( 37 "select %s.%s('%s')", 38 constants.InternalSchema, 39 constants.FunctionCacheSet, 40 settingValue, 41 )) 42 return err 43 }) 44 } 45 46 func executeCacheTtlSetFunction(ctx context.Context, seconds string, connection *pgx.Conn) error { 47 return ExecuteSystemClientCall(ctx, connection, func(ctx context.Context, tx pgx.Tx) error { 48 _, err := tx.Exec(ctx, fmt.Sprintf( 49 "select %s.%s('%s')", 50 constants.InternalSchema, 51 constants.FunctionCacheSetTtl, 52 seconds, 53 )) 54 return err 55 }) 56 }