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

🚀 [Feature]: Adding a deletion function with wildcard for the redis package #1267

Open
3 tasks done
okoseisback opened this issue Mar 4, 2024 · 2 comments
Open
3 tasks done

Comments

@okoseisback
Copy link

okoseisback commented Mar 4, 2024

Feature Description

In the project I developed, there are multiple users, and we store users on the Redis side based on prefixes. And sometimes we need to delete the information we store in Redis.

You can think of an example key like this;
user:{user.guid}:info
user:{user.guid}:preferences
user:{user.guid}:addresses
etc.

I know that this can be done with redis.Keys() and redis.Del(), but it works quite slow in terms of performance. Therefore, I think it would be better to use a function like this based on the package I previously used in NodeJS.

E.g npm pkg:
https://github.com/galanonym/redis-delete-wildcard/blob/master/index.js

The example usage of the function I mentioned would be as follows:

count, err := redis.DelWild("user:6e7c5b62-1d4d-4f6e-bb6d-2e1a2c3d4e5f")
if err != nil {
   fmt.Print(err)
}

fmt.Sprintf("%d records were deleted.", count)

Additional Context (optional)

No response

Code Snippet (optional)

// for redis pkg

func (s *Storage) DelWild(key string) (int64, error) {
	if len(key) <= 0 {
		return 0, nil
	}

	res, err := s.db.Eval(ctx, `
		local keysToDelete = redis.call('keys', ARGV[1])
		if #keysToDelete > 0 then
			return redis.call('del', unpack(keysToDelete))
		else
			return 0
		end
	`, []string{key}).Result()

	if err != nil {
		return 0, err
	}

	return res.(int64), nil
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@gaby
Copy link
Member

gaby commented Mar 12, 2024

@okoseisback You can access the underlaying client by calling Conn().

// Initialize default config
store := redis.New()
client := store.Conn()

// you can Eval() on the client

@gaby
Copy link
Member

gaby commented Mar 19, 2024

@okoseisback Does the above solve your issue?

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

No branches or pull requests

2 participants