1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-03 18:31:14 +03:00
This commit is contained in:
Nedyalko Dyakov
2025-10-31 13:04:46 +02:00
parent 9eaeea1347
commit f672885808
12 changed files with 509 additions and 529 deletions

View File

@@ -208,6 +208,12 @@ func (hs *hooksMixin) processTxPipelineHook(ctx context.Context, cmds []Cmder) e
return hs.current.txPipeline(ctx, cmds)
}
// ------------------------------------------------------------------------------
type AutoPipelinedClient interface {
Cmdable
Close() error
}
//------------------------------------------------------------------------------
type baseClient struct {
@@ -1007,6 +1013,7 @@ func (c *baseClient) txPipelineReadQueued(ctx context.Context, cn *pool.Conn, rd
type Client struct {
*baseClient
cmdable
autopipeliner *AutoPipeliner
}
// NewClient returns a client to the Redis Server specified by Options.
@@ -1083,6 +1090,16 @@ func (c *Client) init() {
})
}
func (c *Client) WithAutoPipeline() AutoPipelinedClient {
if c.autopipeliner != nil {
if !c.autopipeliner.closed.Load() {
return c.autopipeliner
}
}
c.autopipeliner = c.AutoPipeline()
return c.autopipeliner
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
clone := *c
clone.baseClient = c.baseClient.withTimeout(timeout)
@@ -1165,6 +1182,37 @@ func (c *Client) Pipeline() Pipeliner {
return &pipe
}
// AutoPipeline creates a new autopipeliner that automatically batches commands.
// Commands are automatically flushed based on batch size and time interval.
// The autopipeliner must be closed when done to flush pending commands.
//
// Example:
//
// ap := client.AutoPipeline()
// defer ap.Close()
//
// var wg sync.WaitGroup
// for i := 0; i < 1000; i++ {
// wg.Add(1)
// go func(idx int) {
// defer wg.Done()
// ap.Do(ctx, "SET", fmt.Sprintf("key%d", idx), idx)
// }(i)
// }
// wg.Wait()
//
// Note: AutoPipeline requires AutoPipelineConfig to be set in Options.
// If not set, default configuration will be used.
func (c *Client) AutoPipeline() *AutoPipeliner {
if c.autopipeliner != nil {
return c.autopipeliner
}
if c.opt.AutoPipelineConfig == nil {
c.opt.AutoPipelineConfig = DefaultAutoPipelineConfig()
}
return NewAutoPipeliner(c, c.opt.AutoPipelineConfig)
}
func (c *Client) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
return c.TxPipeline().Pipelined(ctx, fn)
}