mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 19:44:21 +00:00
250f2344c8
# Which Problems Are Solved Add a cache implementation using Redis single mode. This does not add support for Redis Cluster or sentinel. # How the Problems Are Solved Added the `internal/cache/redis` package. All operations occur atomically, including setting of secondary indexes, using LUA scripts where needed. The [`miniredis`](https://github.com/alicebob/miniredis) package is used to run unit tests. # Additional Changes - Move connector code to `internal/cache/connector/...` and remove duplicate code from `query` and `command` packages. - Fix a missed invalidation on the restrictions projection # Additional Context Closes #8130
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/jonboulle/clockwork"
|
|
"github.com/zitadel/logging"
|
|
)
|
|
|
|
// Pruner is an optional [Cache] interface.
|
|
type Pruner interface {
|
|
// Prune deletes all invalidated or expired objects.
|
|
Prune(ctx context.Context) error
|
|
}
|
|
|
|
type PrunerCache[I, K comparable, V Entry[I, K]] interface {
|
|
Cache[I, K, V]
|
|
Pruner
|
|
}
|
|
|
|
type AutoPruneConfig struct {
|
|
// Interval at which the cache is automatically pruned.
|
|
// 0 or lower disables automatic pruning.
|
|
Interval time.Duration
|
|
|
|
// Timeout for an automatic prune.
|
|
// It is recommended to keep the value shorter than AutoPruneInterval
|
|
// 0 or lower disables automatic pruning.
|
|
Timeout time.Duration
|
|
}
|
|
|
|
func (c AutoPruneConfig) StartAutoPrune(background context.Context, pruner Pruner, purpose Purpose) (close func()) {
|
|
return c.startAutoPrune(background, pruner, purpose, clockwork.NewRealClock())
|
|
}
|
|
|
|
func (c *AutoPruneConfig) startAutoPrune(background context.Context, pruner Pruner, purpose Purpose, clock clockwork.Clock) (close func()) {
|
|
if c.Interval <= 0 {
|
|
return func() {}
|
|
}
|
|
background, cancel := context.WithCancel(background)
|
|
// randomize the first interval
|
|
timer := clock.NewTimer(time.Duration(rand.Int63n(int64(c.Interval))))
|
|
go c.pruneTimer(background, pruner, purpose, timer)
|
|
return cancel
|
|
}
|
|
|
|
func (c *AutoPruneConfig) pruneTimer(background context.Context, pruner Pruner, purpose Purpose, timer clockwork.Timer) {
|
|
defer func() {
|
|
if !timer.Stop() {
|
|
<-timer.Chan()
|
|
}
|
|
}()
|
|
|
|
for {
|
|
select {
|
|
case <-background.Done():
|
|
return
|
|
case <-timer.Chan():
|
|
err := c.doPrune(background, pruner)
|
|
logging.OnError(err).WithField("purpose", purpose).Error("cache auto prune")
|
|
timer.Reset(c.Interval)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *AutoPruneConfig) doPrune(background context.Context, pruner Pruner) error {
|
|
ctx, cancel := context.WithCancel(background)
|
|
defer cancel()
|
|
if c.Timeout > 0 {
|
|
ctx, cancel = context.WithTimeout(background, c.Timeout)
|
|
defer cancel()
|
|
}
|
|
return pruner.Prune(ctx)
|
|
}
|