javarate-limitingbucket4j

Export/Import the state of a Bucket in bucket-4j


I am using bucket-4j to limit the number of times a specific request can be made by each user within multiple time windows (one day, one week, and one month). For each user, I create a Bucket and maintain it appropriately in memory.

Now, I need to export (serialize) the state of the Bucket to a suitable format (like JSON) and store this state in a database so that I can later retrieve it and recreate the Bucket.

I do not want to use the existing integrations like jdbc-integration.

Do the Buckets in bucket-4j support such functionality?


Solution

  • Since you do not plan to use distributed facilities, it is legal to always cast bucket to LocalBucket https://github.com/bucket4j/bucket4j/blob/8.13/bucket4j-core/src/main/java/io/github/bucket4j/local/LocalBucket.java

    LocalBucket provides all neccesary functionality for serialization and desiralization from/to both binary form and JSON compatible HashMap.

    Can I ask you what database do you plan to use?