DBSIZE
The DBSIZE command returns the number of keys in the currently selected database. This command never fails, has a time complexity of O(1), and is a very fast operation.
Redis Native Command Syntax
Section titled “Redis Native Command Syntax”DBSIZEParameter Description
The DBSIZE command takes no parameters.
Detailed Explanation
Section titled “Detailed Explanation”The DBSIZE command returns the total number of keys in the currently selected Redis database. In Redis, there are 16 databases by default (numbered 0 to 15), and you can switch between them using the SELECT command.
Each database is an independent namespace and can contain keys with the same name without conflict. The DBSIZE command only returns the number of keys in the currently selected database.
Redisun Usage
Section titled “Redisun Usage”In redisun, the DBSIZE command is implemented through the DBSizeCommand class and the dbsize method in the Redisun class.
Basic Usage
Section titled “Basic Usage”Redisun redisun = Redisun.create(options -> { options.setHost("localhost"); options.setPort(6379);});
// Get the number of keys in the current databaselong keyCount = redisun.dbsize();System.out.println("Current database has " + keyCount + " keys");- The DBSIZE command only counts keys in the currently selected database
- Different databases are independent namespaces
- The command has a time complexity of O(1) and executes very quickly