Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom prefix and key in Redis cache. #20

Open
rts-gordon opened this issue Apr 7, 2024 · 2 comments
Open

Custom prefix and key in Redis cache. #20

rts-gordon opened this issue Apr 7, 2024 · 2 comments
Assignees

Comments

@rts-gordon
Copy link

Describe the feature

Hi @ktsivkov
I queried somethings from DB via gorm-caches, found that the prefix is "gorm-caches" and the keys is a long SQL script, sometimes the long keys have lower performance. Can we customize the prefix and keys? Thank you.

image

@mirusky
Copy link

mirusky commented May 24, 2024

As workaround you could implement somethink like:

func keyer(key string) string {
   hash := md5.Sum([]byte(text))
   return hex.EncodeToString(hash[:])
}

And then in the cacher implementation use it like:

func (c *redisCacher) Get(ctx context.Context, key string, q *caches.Query[any]) (*caches.Query[any], error) {
        key = keyer(key) // hashed key / smaller key
	res, err := c.rdb.Get(ctx, key).Result()
	if err == redis.Nil {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}

	if err := q.Unmarshal([]byte(res)); err != nil {
		return nil, err
	}

	return q, nil
}

@rts-gordon
Copy link
Author

@mirusky thanks a lot, I will try this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants