mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
cedee416a8
* General improvements and bug fixes. * Improve tests coverage. * fixup! Improve tests coverage. * Update README.md with latest changes. * Fix the uint32 * Resolve issue with race condition for logging. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * Fix the test of the rate limiter * Add default ratelimit.json file * Update dependencies. * Significant refactor. * fixup! Significant refactor. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025
122 lines
3.1 KiB
Go
122 lines
3.1 KiB
Go
package libpack_cache_redis
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
redis "github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type RedisConfig struct {
|
|
ctx context.Context
|
|
client *redis.Client
|
|
builderPool *sync.Pool
|
|
prefix string
|
|
}
|
|
|
|
func (c *RedisConfig) prependKeyName(key string) string {
|
|
builder := c.builderPool.Get().(*strings.Builder)
|
|
defer c.builderPool.Put(builder)
|
|
builder.Reset()
|
|
builder.WriteString(c.prefix)
|
|
builder.WriteString(key)
|
|
return builder.String()
|
|
}
|
|
|
|
type RedisClientConfig struct {
|
|
RedisServer string
|
|
RedisPassword string
|
|
Prefix string
|
|
RedisDB int
|
|
}
|
|
|
|
func New(redisClientConfig *RedisClientConfig) (*RedisConfig, error) {
|
|
c := &RedisConfig{
|
|
client: redis.NewClient(&redis.Options{
|
|
Addr: redisClientConfig.RedisServer,
|
|
Password: redisClientConfig.RedisPassword,
|
|
DB: redisClientConfig.RedisDB,
|
|
}),
|
|
ctx: context.Background(),
|
|
prefix: redisClientConfig.Prefix,
|
|
builderPool: &sync.Pool{
|
|
New: func() interface{} {
|
|
return &strings.Builder{}
|
|
},
|
|
},
|
|
}
|
|
|
|
_, err := c.client.Ping(c.ctx).Result()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (c *RedisConfig) Set(key string, value []byte, ttl time.Duration) error {
|
|
return c.client.Set(c.ctx, c.prependKeyName(key), value, ttl).Err()
|
|
}
|
|
|
|
func (c *RedisConfig) Get(key string) ([]byte, bool, error) {
|
|
val, err := c.client.Get(c.ctx, c.prependKeyName(key)).Result()
|
|
if err == redis.Nil {
|
|
return nil, false, nil
|
|
}
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return []byte(val), true, nil
|
|
}
|
|
|
|
func (c *RedisConfig) Delete(key string) error {
|
|
return c.client.Del(c.ctx, c.prependKeyName(key)).Err()
|
|
}
|
|
|
|
func (c *RedisConfig) Clear() error {
|
|
return c.client.FlushDB(c.ctx).Err()
|
|
}
|
|
|
|
func (c *RedisConfig) CountQueries() (int64, error) {
|
|
keys, err := c.client.Keys(c.ctx, c.prependKeyName("*")).Result()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int64(len(keys)), nil
|
|
}
|
|
|
|
func (c *RedisConfig) CountQueriesWithPattern(pattern string) (int, error) {
|
|
keys, err := c.client.Keys(c.ctx, c.prependKeyName(pattern)).Result()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return len(keys), nil
|
|
}
|
|
|
|
// GetMemoryUsage returns an approximation of memory usage for Redis
|
|
// For Redis, this is not as accurate as the memory cache implementation
|
|
// as actual memory is managed by Redis server
|
|
func (c *RedisConfig) GetMemoryUsage() int64 {
|
|
// We could attempt to get memory usage from Redis info
|
|
// but for now, we'll just return 0 since Redis manages its own memory
|
|
// and this information would require parsing the INFO command output
|
|
_, err := c.client.Info(c.ctx, "memory").Result()
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
|
|
// Just return 0 as a placeholder since Redis manages its own memory
|
|
// In a production environment, you could parse the Redis INFO command result
|
|
// to extract actual "used_memory" value
|
|
return 0
|
|
}
|
|
|
|
// GetMaxMemorySize returns the configured max memory for Redis
|
|
// In Redis, this would be the 'maxmemory' configuration value
|
|
func (c *RedisConfig) GetMaxMemorySize() int64 {
|
|
// Return a default value as Redis manages its own memory limits
|
|
// In a production environment, you could get this from Redis config
|
|
return 0
|
|
}
|